How to rename recovered video files based on date and duration?

Started by quickshot, January 23, 2021, 07:11:56 PM

Previous topic - Next topic

quickshot

(This is a duplicate asked in https://video.stackexchange.com/)
Basically for some recovered video files I'd like to rename using a common pattern like YYYY-MM-DD_HHMMSS@HHMMSS (year, month, day, hour, minute, second plus duration) or YYYY-MM-DD_HHMMSS-HHMMSS using creation date and time, as well as ending time (starting time plus duration).
Is that possible?

StarGeek

Direct link to question

Run this command on one of the files
exiftool -g1 -a -s -time:all file.mp4

That will list all the time based tags in the file.  There is the possibility that some of the tags, such as ModifyDate and CreateDate, will be set to 0000:00:00 00:00:00.

Pick one of the tags and you replace TAG in the below command with the name of the tag you picked
exiftool -d "%Y-%m-%d_%H%M%S.%%e" "-filename<TAG" /path/to/files/

One thing to watch for is if the time stamp seems to be off a number of hours equal to your time zone.  If that appears to be the case, add -api QuickTimeUTC to the command.

That would be a start, adding the duration would be a bit more complex.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

quickshot

Quote from: StarGeek on January 23, 2021, 08:27:23 PM
Direct link to question

Run this command on one of the files
exiftool -g1 -a -s -time:all file.mp4

For one file I get this output:
V:\recovered>exiftool -g1 -a -s -time:all File0168.MP4
exiftool -g1 -a -s -time:all File0168.MP4
---- System ----
FileModifyDate                  : 2021:01:19 22:55:56+01:00
FileAccessDate                  : 2021:01:22 22:10:48+01:00
FileCreateDate                  : 2021:01:11 23:34:08+01:00
---- QuickTime ----
CreateDate                      : 2018:06:21 17:53:25
ModifyDate                      : 2018:06:21 17:53:25
---- Track1 ----
TrackCreateDate                 : 2018:06:21 17:53:25
TrackModifyDate                 : 2018:06:21 17:53:25
MediaCreateDate                 : 2018:06:21 17:53:25
MediaModifyDate                 : 2018:06:21 17:53:25
---- Track2 ----
TrackCreateDate                 : 2018:06:21 17:53:25
TrackModifyDate                 : 2018:06:21 17:53:25
MediaCreateDate                 : 2018:06:21 17:53:25
MediaModifyDate                 : 2018:06:21 17:53:25
---- IFD0 ----
ModifyDate                      : 0000:00:00 00:00:00
---- ExifIFD ----
DateTimeOriginal                : 2018:06:21 17:53:25
CreateDate                      : 2018:06:21 17:53:25

And for another file I get only:
V:\recovered>exiftool -g1 -a -s -time:all File0174.MTS
exiftool -g1 -a -s -time:all File0174.MTS
---- System ----
FileModifyDate                  : 2021:01:19 22:55:56+01:00
FileAccessDate                  : 2021:01:22 20:35:35+01:00
FileCreateDate                  : 2021:01:11 23:34:55+01:00
---- H264 ----
DateTimeOriginal                : 2018:07:27 21:47:59+00:00

So I have no idea how to get the duration.

StarGeek

Duration is not a time tag. You would use simply Duration for a video.  The problem is in the addition to the starting timestamp as well as dealing with the formatting.  I think I have something bookmarked...  Give me an hour...

* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

StarGeek

Using the example of CreateDate, try this.  If the output looks right, change TestName to Filename
exiftool "-TestName<${CreateDate;DateFmt('%Y-%m-%d_%H%M%S'}@${CreateDate;ShiftTime('0:0:'.$self->GetValue('Duration','ValueConv'));DateFmt('%H%M%S')}.%e" /path/to/files/

For some reason, the forum was breaking when I typed that command, so I had to add a space between $ and {CreateDate.  You'll need to remove that space.
Edit: corrected command

C:\>exiftool -g1 -a -s -duration -CreateDate Y:\!temp\ccccc\b\Testfile.mp4
---- QuickTime ----
CreateDate                      : 2019:12:07 02:15:00
Duration                        : 0:03:17

C:\>exiftool "-Testname<${CreateDate;DateFmt('%Y-%m-%d_%H%M%S')}@${CreateDate;ShiftTime('0:0:'.$self->GetValue('Duration','ValueConv'));DateFmt('%H%M%S')}.%e" Y:\!temp\ccccc\b\Testfile.mp4
'Y:/!temp/ccccc/b/Testfile.mp4' --> 'Y:/!temp/ccccc/b/2019-12-07_021500@021817.mp4'
    0 image files updated
    1 image files unchanged
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

quickshot

Quote from: StarGeek on January 24, 2021, 04:00:53 PM
C:\>exiftool "-Testname<$ {CreateDate;DateFmt('%Y-%m-%d_%H%M%S')}@$ {CreateDate;ShiftTime('0:0:'.$self->GetValue('Duration','ValueConv'));DateFmt('%H%M%S')}.%e" Y:\!temp\ccccc\b\Testfile.mp4
That command seems to do it, but can you explain a bit the magic it does?

StarGeek

The -d (dateFormat) option can't be used here, because you want two different times formats, one with the date and the time, the other with just the time.  So the DateFmt helper function is used to format the dates.

For the second half is more complex because you want the duration added to the starting timestamp. You can't just add tags together on the command line, so first, from within the tag, you have to get the value of Duration.  To do so, you call the GetValue command with $self->GetValue.  There's some other stuff there that's needed to get the raw value of the duration in seconds.  Then the ShiftTime helper function is used to add those seconds to the tag.  Finally, the DateFmt helper function is called to just use the time portion of the timestamp.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).