[英]Set video's metadata by Python
我需要將視頻上傳到 Google 相冊。 無法在請求中設置日期,只能通過元數據設置。 我通過手動設置日期(windows、文件選項)進行了測試,它可以在 Google Photos 中使用。 所以,我不明白如何通過 Python 設置它。 我找到了所需的值,但我無法設置它。
import ffmpeg
file_path = r"test.MP4"
vid = ffmpeg.probe(file_path)
vid['streams'][0]['tags']['creation_time']
結果'2020-11-01T20:07:09.000000Z'
我試過vid['streams'][0]['tags']['creation_time'] = '2015-11-01T20:07:09.000000Z'
但沒有任何改變。 請幫忙。
請注意,這是關於視頻文件的元數據,而不是 WINDOWS DATA
據我所知ffmpeg,
編寫元數據。 你檢查過誘變劑嗎?
我們不能簡單地修改標簽。
為了使用 FFmpeg 添加/更改標簽,我們必須創建一個新的視頻文件,該文件可能與原始文件相同,但具有修改后的標簽。
我在以下答案中找到了一個使用 FFmpeg CLI 的示例。
命令行示例:
ffmpeg -y -i in.mp4 -metadata creation_time="2015-10-21 07:28:00" -map 0 -c copy out.mp4
。
等效的 ffmpeg-python 代碼是:
ffmpeg.input('in.mp4').output('out.mp4', metadata='creation_time=2015-10-21 07:28:00', map=0, c='copy').overwrite_output().run()
metadata='creation_time=2015-10-21 07:28:00'
- 添加標簽creation_time
和標簽值2015-10-21 07:28:00
。map=0
- 將所有流和數據從輸入in.mp4
到 output out.mp4
。c='copy'
- 將所有流(視頻、音頻、字幕...)從in.mp4
到out.mp4
而不重新編碼。 正如 Kesh 所評論的, metadata
是一個 output 選項,所以我們必須將它放在.output(...)
中。
測試:
在in.mp4
中創建合成視頻文件(用於測試):
ffmpeg.input('testsrc=size=192x108:rate=1:duration=10', f='lavfi').output('in.mp4').overwrite_output().run()
使用ffmpeg.probe
檢查in.mp4
的creation_time
的值:
p = ffmpeg.probe('in.mp4') if 'creation_time' in p['streams'][0]['tags']: print(p['streams'][0]['tags']['creation_time']) # Not enter here, because in.mp4 has no 'creation_time' tag.
將值為2015-10-21 07:28:00
的creation_time
標簽添加到in.mp4
,創建一個新文件out.mp4
:
ffmpeg.input('in.mp4').output('out.mp4', metadata='creation_time=2015-10-21 07:28:00', map=0, c='copy').overwrite_output().run()
使用ffmpeg.probe
檢查in.mp4
的creation_time
的值:
p = ffmpeg.probe('out.mp4') print(p['streams'][0]['tags']['creation_time']) # Prints: 2015-10-21T04:28:00.000000Z
完整樣例(測試現有標簽的修改):
import ffmpeg
# ffmpeg -y -f lavfi -i testsrc=size=192x108:rate=1:duration=10 in.mp4
ffmpeg.input('testsrc=size=192x108:rate=1:duration=10', f='lavfi').output('in.mp4').overwrite_output().run()
# ffprobe -print_format json -show_format in.mp4
p = ffmpeg.probe('in.mp4')
if 'creation_time' in p['streams'][0]['tags']:
print(p['streams'][0]['tags']['creation_time']) # Not enter here, because in.mp4 has no 'creation_time' tag.
# ffmpeg -y -i in.mp4 -metadata creation_time="2020-11-01T20:07:09.000000Z" -map 0 -c copy test.mp4
ffmpeg.input('in.mp4').output('test.mp4', metadata='creation_time=2020-11-01T20:07:09.000000Z', map=0, c='copy').overwrite_output().run()
# ffprobe -print_format json -show_format test.mp4
p = ffmpeg.probe('test.mp4')
print(p['streams'][0]['tags']['creation_time']) # Prints: 2020-11-01T20:07:09.000000Z
# ffmpeg -y -i test.mp4 -metadata creation_time="2015-10-21 07:28:00" -map 0 -c copy out.mp4
ffmpeg.input('test.mp4').output('out.mp4', metadata='creation_time=2015-11-01T20:07:09.000000Z', map=0, c='copy').overwrite_output().run()
# ffprobe -print_format json -show_format out.mp4
p = ffmpeg.probe('out.mp4')
print(p['streams'][0]['tags']['creation_time']) # Prints: 2015-11-01T20:07:09.000000Z
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.