7

In a shell, how to automatically set the modification (or creation) date and time of a Quicktime video file based on the metadata in the file with a single command (or a single command line)? For JPG files, we have exiv2 -T, but is there a similar command for .mov files?

To give an example, let's start with a file video.mov with the following metadata:

$ exiftool video.mov
ExifTool Version Number         : 12.57
File Name                       : video.mov
Directory                       : .
File Size                       : 64 MB
File Modification Date/Time     : 2023:07:04 02:53:05+02:00
File Access Date/Time           : 2023:07:01 11:42:46+02:00
File Inode Change Date/Time     : 2023:07:04 02:53:05+02:00
File Permissions                : -rw-r--r--
File Type                       : MOV
File Type Extension             : mov
MIME Type                       : video/quicktime
Major Brand                     : Apple QuickTime (.MOV/QT)
Minor Version                   : 0.0.0
Compatible Brands               : qt
Media Data Size                 : 64215615
Media Data Offset               : 36
Movie Header Version            : 0
Create Date                     : 2023:07:01 11:42:00
Modify Date                     : 2023:07:01 11:42:46
Time Scale                      : 600
Duration                        : 0:00:45
Preferred Rate                  : 1
Preferred Volume                : 100.00%
Preview Time                    : 0 s
Preview Duration                : 0 s
Poster Time                     : 0 s
Selection Time                  : 0 s
Selection Duration              : 0 s
Current Time                    : 0 s
Next Track ID                   : 6
Track Header Version            : 0
Track Create Date               : 2023:07:01 11:42:00
Track Modify Date               : 2023:07:01 11:42:46
Track ID                        : 1
Track Duration                  : 0:00:45
Track Layer                     : 0
Track Volume                    : 0.00%
Image Width                     : 1920
Image Height                    : 1080
Clean Aperture Dimensions       : 1920x1080
Production Aperture Dimensions  : 1920x1080
Encoded Pixels Dimensions       : 1920x1080
Graphics Mode                   : ditherCopy
Op Color                        : 32768 32768 32768
Compressor ID                   : hvc1
Source Image Width              : 1920
Source Image Height             : 1080
X Resolution                    : 72
Y Resolution                    : 72
Compressor Name                 : HEVC
Bit Depth                       : 24
Video Frame Rate                : 29.997
Balance                         : 0
Audio Format                    : mp4a
Audio Channels                  : 2
Audio Bits Per Sample           : 16
Audio Sample Rate               : 44100
Purchase File Format            : mp4a
Warning                         : [minor] The ExtractEmbedded option may find more tags in the media data
Matrix Structure                : 1 0 0 0 1 0 0 0 1
Content Describes               : Track 1
Media Header Version            : 0
Media Create Date               : 2023:07:01 11:42:00
Media Modify Date               : 2023:07:01 11:42:46
Media Time Scale                : 600
Media Duration                  : 0:00:45
Media Language Code             : und
Gen Media Version               : 0
Gen Flags                       : 0 0 0
Gen Graphics Mode               : ditherCopy
Gen Op Color                    : 32768 32768 32768
Gen Balance                     : 0
Handler Class                   : Data Handler
Handler Vendor ID               : Apple
Handler Description             : Core Media Data Handler
Meta Format                     : mebx
Handler Type                    : Metadata Tags
Make                            : Apple
Model                           : iPhone SE (2nd generation)
Software                        : 16.5.1
Creation Date                   : 2023:07:01 13:42:00+02:00
Image Size                      : 1920x1080
Megapixels                      : 2.1
Avg Bitrate                     : 11.3 Mbps
Rotation                        : 90

The best approach (to set the modification date) I could come up myself with so far is reading the output of

$ exiftool video.mov | grep "Media Modify Date" | cut -f 19-20 -d ' '

, which is, in my example,

2023:07:01 11:42:46

(which is correct as normalized to UTC or GMT because in real life, the video was taken at around 13:42:… CEST), replacing : in the date in output with -, and finally issuing

$ touch -d "2023-07-01 11:42:46 UTC" video.mov

(my wild guess is that saying UTC is better than saying GMT above). This yields, as expected,

$ ls --full-time video.mov | cut -d ' ' -f 6-8
2023-07-01 13:42:46.000000000 +0200

(the machine is in the time zone CEST, hence +0200). The result is what we want (because the time zone in which the video itself was taken was also CEST), but the process of getting there was manual.

How to process the date from the first command sequence (exiftool … -d ' ') automatically, so that we can issue both the first command and the second command (touch …) in a single command line or in a script?

Alternatively, the modification (or creation) time of the .mov video file has to be read from the metadata in the video file and set on the operating system level in some other way. How? (An aside: as the meta-data field Media Modify Date may be all-zeros for some files, e.g., for a file created by ffmpeg, we might need some more programming logic and try to switch to the values of some other fields in such a case, e.g., adding Date/Time Original and Media Duration if they are properly filled.)

Has anyone already done this task, perhaps, and we just need to run an already available program with appropriate parameters?

0

2 Answers 2

5

exiftool can set most of files metadata in addition to retrieving it, so it should just be a matter of:

TZ=UTC0 exiftool '-FileModifyDate<MediaModifyDate' ./*.mov

Or:

exiftool -api QuickTimeUTC '-FileModifyDate<MediaModifyDate' ./*.mov

Or recursively (also updating files in subdirectories):

exiftool -api QuickTimeUTC -r -ext mov '-FileModifyDate<MediaModifyDate' .

By default, exiftool interprets those Media timestamps in QuickTime MOV files as local time even though the QuickTime specification says they're meant to be in UTC as that's what most cameras do. With TZ=UTC0, we tell it the local time is UTC (is 0 offset from UTC), with -api QuickTimeUTC, we tell it those times are meant to be interpreted as UTC as that seems to be the case for you. Both should achieve the same result.

Files without a MediaModifyDate would end up with a 1904-01-01 00:00:00 +0000 timestamp (-2082844800 epoch time) as that's the origin time for QuickTime timestamps.

To skip those, you can do:

TZ=UTC0 exiftool -d %s -if '$MediaModifyDate != -2082844800' \
  -r -ext mov '-FileModifyDate<MediaModifyDate' .

(using TZ=UTC0 instead of -api QuickTimeUTC as the latter doesn't seem to work when combined with -d %s specifically to format the time as Unix epoch time, which looks like a bug. Using other date formats would be timezone dependant so we couldn't compare to a fixed string like -2082844800)

If you wanted to use touch to set the mtime, you'd do:

mtime=$(
  exiftool -api QuickTimeUTC -q -d %s -p '$MediaModifyDate' file.mov
) &&
  [ "$mtime" != '0000:00:00 00:00:00' ] &&
  touch -d "@$mtime" file.mov

(using -api QuickTimeUTC does seem to work OK with -d %s when it's just about printing that number here. And yes, you do get 0000:00:00 00:00:00 and not -2082844800 when there's no MediaModifyDate. I guess that could change in the future).

1
  • You're right about -api QuickTimeUTC not working with that -d %s and -FileModifyDate<MediaModifyDate. It looks a bit buggy to me. Jul 5, 2023 at 6:16
2

For all .mov files in current folder:

#!/usr/bin/env bash
for file in *.mov;
do
    t=$(exiftool "$file" | grep "Media Modify Date" | cut -f 19-20 -d ' ')
    t="${t/:/-}"
    t="${t/:/-}"
    touch -d "$t UTC" "$file"
done
0

You must log in to answer this question.