Renaming MP4 file based on ModifyDate, TimeZone shift and Duration

Started by basilh, December 19, 2019, 01:44:16 AM

Previous topic - Next topic

basilh

Hello,
First-time poster. Been wandering the corridors of the ExifTool forum for a while now. Totally in awe at the power of this tool and the collective intelligence here. I'm using the Windows version of ExifTool.

From what I've read in various posts, date tags tend to be ho-hum for MP4 files. I have noticed for the MP4 files I'm working with (all from smart phones), that the original filename can be recreated from ModifyDate plus the timezone shift, less the Duration of the clip.  Is it possible to rename an MP4 file and recreate this date? e.g. VID_20140417_212408.mp4 -> 2014-04-17 21.24.08

This is how far I've got, but I'm struggling with the syntax for the final bit (assuming it is possible)...

exiftool -r -P -d "%Y-%m-%d %H.%M.%S%%-c.%%e" "-TestName<?" -ext mp4 -GlobalTimeShift "8:0:0" .

where ? = ModifyDate - Duration

Windows 10 - ExifTool 11.80

StarGeek

The date/time tags in video files are supposed to set to UTC time according to the spec.  Exiftool has a specific option to deal with this so you don't have to figure out the time zone differences by yourself.  So instead of using the GlobalTimeShift option, add -api QuickTimeUTC to your command.

As for the difference between the modify date and the actual time stamp being different by the duration, that seems very odd to me and I can't really think of a good solution for that.  I would advise checking the other time related tags in the file to see if there's a better one.  Run this command on the file
exiftool -time:all -api QuicktimeUTC -g1 -a -s file.mp4

That will list all the time tags and their names (not their descriptions).  If there's a more correct one, just use that name in place of the question mark in your example and add -api QuicktimeUTC to the command.  The most likely tag to use should be CreateDate.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

basilh

QuoteExiftool has a specific option to deal with this so you don't have to figure out the time zone differences by yourself.

Thank you! That's much more elegant.

QuoteAs for the difference between the modify date and the actual time stamp being different by the duration, that seems very odd to me and I can't really think of a good solution for that.

I stand corrected. I checked a few other files and the logic doesn't hold. I think I was lucky to stumble on a file where it appeared to be true.

QuoteI would advise checking the other time related tags in the file to see if there's a better one.

I:\Input>exiftool -time:all -api QuicktimeUTC -g1 -a -s VID_20140417_212408.mp4
---- System ----
FileModifyDate                  : 2014:04:17 21:24:13+08:00
FileAccessDate                  : 2019:12:19 13:28:14+08:00
FileCreateDate                  : 2019:12:14 17:04:51+08:00
---- QuickTime ----
CreateDate                      : 2014:04:17 21:24:13+08:00
ModifyDate                      : 2014:04:17 21:24:13+08:00
---- Track1 ----
TrackCreateDate                 : 2014:04:17 21:24:13+08:00
TrackModifyDate                 : 2014:04:17 21:24:13+08:00
MediaCreateDate                 : 2014:04:17 21:24:13+08:00
MediaModifyDate                 : 2014:04:17 21:24:13+08:00
---- Track2 ----
TrackCreateDate                 : 2014:04:17 21:24:13+08:00
TrackModifyDate                 : 2014:04:17 21:24:13+08:00
MediaCreateDate                 : 2014:04:17 21:24:13+08:00
MediaModifyDate                 : 2014:04:17 21:24:13+08:00


It remains a mystery to me how the original filename was constructed.
Windows 10 - ExifTool 11.80

Phil Harvey

Is is not uncommon for the ModifyDate to be CreateDate + Duration.  IMHO this is the correct thing to do.

In this case you can name the files according to the creation date/time like this:

exiftool "-filename<${modifydate;ShiftTime('-0:0:'.$self->GetValue('Duration','ValueConv'))}" ...

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

StarGeek

Quote from: basilh on December 19, 2019, 02:36:41 AMIt remains a mystery to me how the original filename was constructed.

Strange.  It looks correct up until the seconds.

If you would like to keep track of the original filename, you might save it into the file with
exiftool "-XMP:PreservedFileName<Filename" <FileOrDir>

That way, if everything goes sideways, you can always restore the original names with
exiftool "-Filename<XMP:PreservedFileName" <FileOrDir>
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

basilh

Quote from: Phil Harvey on December 19, 2019, 07:11:49 AM
Is is not uncommon for the ModifyDate to be CreateDate + Duration.  IMHO this is the correct thing to do.

In this case you can name the files according to the creation date/time like this:

exiftool "-filename<${modifydate;ShiftTime('-0:0:'.$self->GetValue('Duration','ValueConv'))}" ...

- Phil

I think I would have had a better chance of winning the lottery than figuring that out at this stage on my learning curve! I've studied the construct and have a couple of questions:

1. $self-> Do I interpret this as 'Takes on the value of'?
2. GetValue() Where do I find out about functions available to ExifTool? They don't seem to form a part of the application documentation.
3. I'm still missing something...

Considering the following metadata...

I:\Input\x>exiftool -s VID_20140417_212408.mp4
ExifToolVersion                 : 11.80
FileName                        : VID_20140417_212408.mp4
Directory                       : .
FileSize                        : 4.3 MB
FileModifyDate                  : 2014:04:17 21:24:13+08:00
FileAccessDate                  : 2019:12:20 09:52:22+08:00
FileCreateDate                  : 2019:12:20 09:52:21+08:00
FilePermissions                 : rw-rw-rw-
FileType                        : MP4
FileTypeExtension               : mp4
MIMEType                        : video/mp4
MajorBrand                      : MP4  Base Media v1 [IS0 14496-12:2003]
MinorVersion                    : 0.0.0
CompatibleBrands                : isom, 3gp4
MovieHeaderVersion              : 0
CreateDate                      : 2014:04:17 13:24:13
ModifyDate                      : 2014:04:17 13:24:13
TimeScale                       : 1000
Duration                        : 2.90 s
PreferredRate                   : 1
PreferredVolume                 : 100.00%
PreviewTime                     : 0 s
PreviewDuration                 : 0 s
PosterTime                      : 0 s
SelectionTime                   : 0 s
SelectionDuration               : 0 s
CurrentTime                     : 0 s
NextTrackID                     : 3
GPSCoordinates                  : 31 deg 51' 40.32" S, 115 deg 48' 30.60" E
TrackHeaderVersion              : 0
TrackCreateDate                 : 2014:04:17 13:24:13
TrackModifyDate                 : 2014:04:17 13:24:13
TrackID                         : 1
TrackDuration                   : 2.67 s
TrackLayer                      : 0
TrackVolume                     : 0.00%
ImageWidth                      : 1920
ImageHeight                     : 1080
GraphicsMode                    : srcCopy
OpColor                         : 0 0 0
CompressorID                    : avc1
SourceImageWidth                : 1920
SourceImageHeight               : 1080
XResolution                     : 72
YResolution                     : 72
BitDepth                        : 24
PixelAspectRatio                : 65536:65536
VideoFrameRate                  : 30.326
MatrixStructure                 : 1 0 0 0 1 0 0 0 1
MediaHeaderVersion              : 0
MediaCreateDate                 : 2014:04:17 13:24:13
MediaModifyDate                 : 2014:04:17 13:24:13
MediaTimeScale                  : 48000
MediaDuration                   : 2.90 s
HandlerType                     : Audio Track
HandlerDescription              : SoundHandle
Balance                         : 0
AudioFormat                     : mp4a
AudioChannels                   : 1
AudioBitsPerSample              : 16
AudioSampleRate                 : 48000
MovieDataSize                   : 4067398
MovieDataOffset                 : 405032
ImageSize                       : 1920x1080
Megapixels                      : 2.1
AvgBitrate                      : 11.2 Mbps
GPSLatitude                     : 31 deg 51' 40.32" S
GPSLongitude                    : 115 deg 48' 30.60" E
Rotation                        : 180
GPSPosition                     : 31 deg 51' 40.32" S, 115 deg 48' 30.60" E


If I check the math (ModifyDate + TimeZoneOffset - Duration) ...
 
I:\Input\x>exiftool -p "${modifydate;ShiftTime('-0:0:'.$self->GetValue('Duration','Valueconv'))}" "VID_20140417_212408.mp4" -api QuicktimeUTC
2014:04:17 21:24:10+08:00


...I get the expected result. However, if I then include that in the rename construct, it doesn't produce the desired result...
I:\Input\x>exiftool "-TestName<${modifydate;ShiftTime('-0:0:'.$self->GetValue('Duration','Valueconv'))}" "VID_20140417_212408.mp4" -api QuicktimeUTC -d "%Y-%m-%d %H.%M.%S.%%e"
'VID_20140417_212408.mp4' --> '2014-04-17 21.0014.00.mp4'
    0 image files updated
    1 image files unchanged


Quote from: StarGeek on December 19, 2019, 11:22:54 AM
Quote from: basilh on December 19, 2019, 02:36:41 AMIt remains a mystery to me how the original filename was constructed.

Strange.  It looks correct up until the seconds.

If you would like to keep track of the original filename, you might save it into the file with
exiftool "-XMP:PreservedFileName<Filename" <FileOrDir>

That way, if everything goes sideways, you can always restore the original names with
exiftool "-Filename<XMP:PreservedFileName" <FileOrDir>

Thank you! That is really useful to know!

Interestingly, for the example file I've referred to in this post, using only CreateDate...

I:\Input\x>exiftool "-TestName<CreateDate" "VID_20140417_212408.mp4" -api QuicktimeUTC -d "%Y-%m-%d %H.%M.%S.%%e"
'VID_20140417_212408.mp4' --> '2014-04-17 21.24.13.mp4'
    0 image files updated
    1 image files unchanged


With the 'correction' Phil has suggested...

I:\Input\x>exiftool -p "${modifydate;ShiftTime('-0:0:'.$self->GetValue('Duration','Valueconv'))}" "VID_20140417_212408.mp4" -api QuicktimeUTC
2014:04:17 21:24:10+08:00


...it's closer to the date in the original filename, but still not the same. Wierd! Anyway, including the original filename in the metadata is a good backup plan.
Windows 10 - ExifTool 11.80

StarGeek

Quote from: basilh on December 19, 2019, 09:38:05 PM
1. $self-> Do I interpret this as 'Takes on the value of'?
2. GetValue() Where do I find out about functions available to ExifTool? They don't seem to form a part of the application documentation.

This is getting in to the Perl part of the program.  You can find details on the Image::ExifTool Perl Library Module


"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

basilh

Windows 10 - ExifTool 11.80

basilh

Quote from: basilh on December 19, 2019, 09:38:05 PM
Quote from: Phil Harvey on December 19, 2019, 07:11:49 AM
Is is not uncommon for the ModifyDate to be CreateDate + Duration.  IMHO this is the correct thing to do.

In this case you can name the files according to the creation date/time like this:

exiftool "-filename<${modifydate;ShiftTime('-0:0:'.$self->GetValue('Duration','ValueConv'))}" ...

- Phil

Considering the following metadata...

I:\Input\x>exiftool -s VID_20140417_212408.mp4
ExifToolVersion                 : 11.80
FileName                        : VID_20140417_212408.mp4
Directory                       : .
FileSize                        : 4.3 MB
FileModifyDate                  : 2014:04:17 21:24:13+08:00
FileAccessDate                  : 2019:12:20 09:52:22+08:00
FileCreateDate                  : 2019:12:20 09:52:21+08:00
FilePermissions                 : rw-rw-rw-
FileType                        : MP4
FileTypeExtension               : mp4
MIMEType                        : video/mp4
MajorBrand                      : MP4  Base Media v1 [IS0 14496-12:2003]
MinorVersion                    : 0.0.0
CompatibleBrands                : isom, 3gp4
MovieHeaderVersion              : 0
CreateDate                      : 2014:04:17 13:24:13
ModifyDate                      : 2014:04:17 13:24:13
TimeScale                       : 1000
Duration                        : 2.90 s
PreferredRate                   : 1
PreferredVolume                 : 100.00%
PreviewTime                     : 0 s
PreviewDuration                 : 0 s
PosterTime                      : 0 s
SelectionTime                   : 0 s
SelectionDuration               : 0 s
CurrentTime                     : 0 s
NextTrackID                     : 3
GPSCoordinates                  : 31 deg 51' 40.32" S, 115 deg 48' 30.60" E
TrackHeaderVersion              : 0
TrackCreateDate                 : 2014:04:17 13:24:13
TrackModifyDate                 : 2014:04:17 13:24:13
TrackID                         : 1
TrackDuration                   : 2.67 s
TrackLayer                      : 0
TrackVolume                     : 0.00%
ImageWidth                      : 1920
ImageHeight                     : 1080
GraphicsMode                    : srcCopy
OpColor                         : 0 0 0
CompressorID                    : avc1
SourceImageWidth                : 1920
SourceImageHeight               : 1080
XResolution                     : 72
YResolution                     : 72
BitDepth                        : 24
PixelAspectRatio                : 65536:65536
VideoFrameRate                  : 30.326
MatrixStructure                 : 1 0 0 0 1 0 0 0 1
MediaHeaderVersion              : 0
MediaCreateDate                 : 2014:04:17 13:24:13
MediaModifyDate                 : 2014:04:17 13:24:13
MediaTimeScale                  : 48000
MediaDuration                   : 2.90 s
HandlerType                     : Audio Track
HandlerDescription              : SoundHandle
Balance                         : 0
AudioFormat                     : mp4a
AudioChannels                   : 1
AudioBitsPerSample              : 16
AudioSampleRate                 : 48000
MovieDataSize                   : 4067398
MovieDataOffset                 : 405032
ImageSize                       : 1920x1080
Megapixels                      : 2.1
AvgBitrate                      : 11.2 Mbps
GPSLatitude                     : 31 deg 51' 40.32" S
GPSLongitude                    : 115 deg 48' 30.60" E
Rotation                        : 180
GPSPosition                     : 31 deg 51' 40.32" S, 115 deg 48' 30.60" E


If I check the math (ModifyDate + TimeZoneOffset - Duration) ...
 
I:\Input\x>exiftool -p "${modifydate;ShiftTime('-0:0:'.$self->GetValue('Duration','Valueconv'))}" "VID_20140417_212408.mp4" -api QuicktimeUTC
2014:04:17 21:24:10+08:00


...I get the expected result. However, if I then include that in the rename construct, it doesn't produce the desired result...
I:\Input\x>exiftool "-TestName<${modifydate;ShiftTime('-0:0:'.$self->GetValue('Duration','Valueconv'))}" "VID_20140417_212408.mp4" -api QuicktimeUTC -d "%Y-%m-%d %H.%M.%S.%%e"
'VID_20140417_212408.mp4' --> '2014-04-17 21.0014.00.mp4'
    0 image files updated
    1 image files unchanged


Windows 10 - ExifTool 11.80

Phil Harvey

ShiftTime() only works on unformatted date/time values, so you can't use it with -d.  Instead, try this:

exiftool "-TestName<${modifydate;ShiftTime('-0:0:'.$self->GetValue('Duration','Valueconv'));DateFmt('%Y-%m-%d %H.%M.%S.%%e')}" "VID_20140417_212408.mp4" -api QuicktimeUTC

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

basilh

Quote from: Phil Harvey on December 26, 2019, 01:24:43 PM
ShiftTime() only works on unformatted date/time values, so you can't use it with -d.  Instead, try this:

exiftool "-TestName<${modifydate;ShiftTime('-0:0:'.$self->GetValue('Duration','Valueconv'));DateFmt('%Y-%m-%d %H.%M.%S.%%e')}" "VID_20140417_212408.mp4" -api QuicktimeUTC

- Phil

:) Thank you Phil. Wishing the Exiftool team a Happy New Year. Thank you to you and StarGeek for your guidance in 2019.
Windows 10 - ExifTool 11.80