7 video date time touch file-metadata
在 shell 中,如何使用单个命令(或单个命令行)根据文件中的元数据自动设置 Quicktime 视频文件的修改(或创建)日期和时间?对于 JPG 文件,我们有exiv2 -T,但是对于 .mov 文件有类似的命令吗?
举个例子,让我们从包含以下元数据的文件 video.mov 开始:
\n$ exiftool video.mov\nExifTool Version Number : 12.57\nFile Name : video.mov\nDirectory : .\nFile Size : 64 MB\nFile Modification Date/Time : 2023:07:04 02:53:05+02:00\nFile Access Date/Time : 2023:07:01 11:42:46+02:00\nFile Inode Change Date/Time : 2023:07:04 02:53:05+02:00\nFile Permissions : -rw-r--r--\nFile Type : MOV\nFile Type Extension : mov\nMIME Type : video/quicktime\nMajor Brand : Apple QuickTime (.MOV/QT)\nMinor Version : 0.0.0\nCompatible Brands : qt\nMedia Data Size : 64215615\nMedia Data Offset : 36\nMovie Header Version : 0\nCreate Date : 2023:07:01 11:42:00\nModify Date : 2023:07:01 11:42:46\nTime Scale : 600\nDuration : 0:00:45\nPreferred Rate : 1\nPreferred Volume : 100.00%\nPreview Time : 0 s\nPreview Duration : 0 s\nPoster Time : 0 s\nSelection Time : 0 s\nSelection Duration : 0 s\nCurrent Time : 0 s\nNext Track ID : 6\nTrack Header Version : 0\nTrack Create Date : 2023:07:01 11:42:00\nTrack Modify Date : 2023:07:01 11:42:46\nTrack ID : 1\nTrack Duration : 0:00:45\nTrack Layer : 0\nTrack Volume : 0.00%\nImage Width : 1920\nImage Height : 1080\nClean Aperture Dimensions : 1920x1080\nProduction Aperture Dimensions : 1920x1080\nEncoded Pixels Dimensions : 1920x1080\nGraphics Mode : ditherCopy\nOp Color : 32768 32768 32768\nCompressor ID : hvc1\nSource Image Width : 1920\nSource Image Height : 1080\nX Resolution : 72\nY Resolution : 72\nCompressor Name : HEVC\nBit Depth : 24\nVideo Frame Rate : 29.997\nBalance : 0\nAudio Format : mp4a\nAudio Channels : 2\nAudio Bits Per Sample : 16\nAudio Sample Rate : 44100\nPurchase File Format : mp4a\nWarning : [minor] The ExtractEmbedded option may find more tags in the media data\nMatrix Structure : 1 0 0 0 1 0 0 0 1\nContent Describes : Track 1\nMedia Header Version : 0\nMedia Create Date : 2023:07:01 11:42:00\nMedia Modify Date : 2023:07:01 11:42:46\nMedia Time Scale : 600\nMedia Duration : 0:00:45\nMedia Language Code : und\nGen Media Version : 0\nGen Flags : 0 0 0\nGen Graphics Mode : ditherCopy\nGen Op Color : 32768 32768 32768\nGen Balance : 0\nHandler Class : Data Handler\nHandler Vendor ID : Apple\nHandler Description : Core Media Data Handler\nMeta Format : mebx\nHandler Type : Metadata Tags\nMake : Apple\nModel : iPhone SE (2nd generation)\nSoftware : 16.5.1\nCreation Date : 2023:07:01 13:42:00+02:00\nImage Size : 1920x1080\nMegapixels : 2.1\nAvg Bitrate : 11.3 Mbps\nRotation : 90\nRun Code Online (Sandbox Code Playgroud)\n到目前为止,我自己能想到的最好的方法(设置修改日期)是阅读
\n$ exiftool video.mov | grep "Media Modify Date" | cut -f 19-20 -d \' \'\nRun Code Online (Sandbox Code Playgroud)\n,在我的例子中,
\n2023:07:01 11:42:46\nRun Code Online (Sandbox Code Playgroud)\n(按照 UTC 或 GMT 标准化,这是正确的,因为在现实生活中,视频是在 13:42:\xe2\x80\xa6 CEST 左右拍摄的),将:输出中的日期替换为-,最后发出
$ touch -d "2023-07-01 11:42:46 UTC" video.mov\nRun Code Online (Sandbox Code Playgroud)\n(我的大胆猜测是,这句话UTC比上面说的更好GMT)。正如预期的那样,这会产生
$ ls --full-time video.mov | cut -d \' \' -f 6-8\n2023-07-01 13:42:46.000000000 +0200\nRun Code Online (Sandbox Code Playgroud)\n(机器位于 CEST 时区,因此+0200)。结果就是我们想要的(因为视频本身拍摄的时区也是 CEST),但到达那里的过程是手动的。
如何自动exiftool \xe2\x80\xa6 -d \' \'处理第一个命令序列 ( )中的日期,以便我们可以在单个命令行或脚本中同时发出第一个命令和第二个命令 ( )?touch \xe2\x80\xa6
或者,必须从视频文件中的元数据读取 .mov 视频文件的修改(或创建)时间,并以某种其他方式在操作系统级别进行设置。如何?(顺便说一句:由于某些文件的元数据字段Media Modify Date可能全为零,例如,对于ffmpeg创建的文件,我们可能需要更多的编程逻辑,并在这种情况下尝试切换到其他一些字段的值,例如,添加Date/Time Original和Media Duration是否正确填充。)
也许有人已经完成了这项任务,而我们只需要使用适当的参数运行一个已经可用的程序?
\nexiftool除了检索文件元数据之外,还可以设置大多数文件元数据,因此它应该只是以下问题:
TZ=UTC0 exiftool '-FileModifyDate<MediaModifyDate' ./*.mov
Run Code Online (Sandbox Code Playgroud)
或者:
exiftool -api QuickTimeUTC '-FileModifyDate<MediaModifyDate' ./*.mov
Run Code Online (Sandbox Code Playgroud)
或者递归地(也更新子目录中的文件):
exiftool -api QuickTimeUTC -r -ext mov '-FileModifyDate<MediaModifyDate' .
Run Code Online (Sandbox Code Playgroud)
默认情况下,exiftool将 QuickTime MOV 文件中的媒体时间戳解释为本地时间,即使 QuickTime 规范规定它们应该采用 UTC(大多数相机都是这样做的)。使用TZ=UTC0,我们告诉它本地时间是 UTC(与 UTC 的偏移量为 0),使用-api QuickTimeUTC,我们告诉它这些时间应该被解释为 UTC,就像您的情况一样。两者应该达到相同的结果。
没有 的文件MediaModifyDate最终会带有1904-01-01 00:00:00 +0000时间戳(-2082844800 纪元时间),因为这是 QuickTime 时间戳的原始时间。
要跳过这些,您可以执行以下操作:
TZ=UTC0 exiftool -d %s -if '$MediaModifyDate != -2082844800' \
-r -ext mov '-FileModifyDate<MediaModifyDate' .
Run Code Online (Sandbox Code Playgroud)
(TZ=UTC0当与-api QuickTimeUTC专门将时间格式化为Unix纪元时间时,使用代替后者似乎不起作用-d %s,这看起来像一个错误。使用其他日期格式将取决于时区,因此我们无法与固定字符串进行比较像-2082844800)
如果你想用来touch设置 mtime,你可以这样做:
mtime=$(
exiftool -api QuickTimeUTC -q -d %s -p '$MediaModifyDate' file.mov
) &&
[ "$mtime" != '0000:00:00 00:00:00' ] &&
touch -d "@$mtime" file.mov
Run Code Online (Sandbox Code Playgroud)
(当只是在此处打印该数字时,使用-api QuickTimeUTC似乎确实可以正常工作。是的,当没有时,您确实得到了而不是。我想这将来可能会改变)。-d %s0000:00:00 00:00:00-2082844800MediaModifyDate