Batch copy time attributes and rename in one hit

Started by trovanch, August 15, 2024, 04:39:30 PM

Previous topic - Next topic

trovanch

Firstly a big thank you to Phil for creating ExifTool, it's marvellous! :)

I have a batch of MP4 files that I have compressed using HandBrake. HandBrake does not preserve metadata from original file, so have used -tagsFromFile to copy source tags to the new, compressed file like so:

exiftool -ee -tagsFromFile src.MP4 -All:All cmp.mp4
This works as expected, but how would I go about extending this command to achieve the following:

  • A) rename file based on the time video was created/shot – am unsure which tag is best to use for this purpose and why.
  • B) operate on a batch of MP4 files – source folder (multiple uncompressed originals), destination folder (multiple compressed files with incorrect date/time metadata).

Do the metadata copy step and the rename step have to be done separately, or is it possible to do this together?

I would be most grateful for any help or direction to implement the above process.

Thank you

wywh

#1
You don't say which OS you use. Movie metadata is not properly standardized and the following commands work for Apple's apps.

FWIW I prefer to use ffmpeg for re-encoding. This command tries to preserve metadata with '-movflags use_metadata_tags -map_metadata 0'...

ffmpeg -i input.mp4 -c:v libx265 -crf 28 -preset medium -timecode 00:00:00:00 -tag:v hvc1 -c:a copy -movflags use_metadata_tags -map_metadata 0 output.mp4
...but as a very old ffmpeg bug you must fix that metadata with exiftool so that Apple's apps can read it:

exiftool -m -overwrite_original -api LargeFileSupport=1 -Keys:All= -tagsFromFile @ -Keys:All output.mp4
That said, I usually do not even try to preserve metadata with ffmpeg but use exiftool tagsFromFile  command for that:

exiftool -m -overwrite_original -api QuickTimeUTC=1 -api LargeFileSupport=1 -tagsFromFile input.mp4 -All:All '-FileCreateDate<QuickTime:CreateDate' '-FileModifyDate<QuickTime:CreateDate' output.mp4
That copies input.mp4 metadata to output.mp4:

exiftool -a -G1 -s -n -api QuickTimeUTC=1 -QuickTime:CreateDate -Keys:All .
======== ./input.mp4
[QuickTime]     CreateDate                      : 2005:05:05 12:05:05+03:00
[Keys]          CreationDate                    : 2005:05:05 12:05:05+03:00
[Keys]          GPSCoordinates                  : -36.6101 -66.91515 119.9
[Keys]          Author                          : Keys:Author
[Keys]          Description                     : Keys:Description
[Keys]          DisplayName                     : Keys:DisplayName
[Keys]          Keywords                        : Keys:Keywords 1,Keys:Keywords 2
[Keys]          Title                           : Keys:Title
[Keys]          UserRating                      : 3
[Keys]          LocationName                    : Keys:LocationName
[Keys]          Make                            : Keys:Make
[Keys]          Model                           : Keys:Model
[Keys]          Software                        : Keys:Software
======== ./output.mp4
[QuickTime]     CreateDate                      : 2005:05:05 12:05:05+03:00
[Keys]          CreationDate                    : 2005:05:05 12:05:05+03:00
[Keys]          GPSCoordinates                  : -36.6101 -66.91515 119.9
[Keys]          Author                          : Keys:Author
[Keys]          Description                     : Keys:Description
[Keys]          DisplayName                     : Keys:DisplayName
[Keys]          Keywords                        : Keys:Keywords 1,Keys:Keywords 2
[Keys]          Title                           : Keys:Title
[Keys]          UserRating                      : 3
[Keys]          LocationName                    : Keys:LocationName
[Keys]          Make                            : Keys:Make
[Keys]          Model                           : Keys:Model
[Keys]          Software                        : Keys:Software
[Keys]          CompatibleBrands                : isom, mp41, mp42
[Keys]          MajorBrand                      : MP4 v2 [ISO 14496-14]
[Keys]          MinorVersion                    : 0.0.1

I have already renamed all such input movies as YYYY-MMDD-hhmm-ss.* by copying movie metadata dates to filenames:

exiftool -m -P -fileOrder5 FileName -api QuickTimeUTC=1 '-FileName<QuickTime:CreateDate' -d '%Y-%m%d-%H%M-%S%%+2nc.%%e' .
So, in the opposite direction it is a breeze to copy filenames to metadata dates (or use the previous command if there are extra characters like _converted in the filename -- see below):

exiftool -m -overwrite_original -wm w -api LargeFileSupport=1 -api QuickTimeUTC=1 '-AllDates<FileName' '-Track*Date<FileName' '-Media*Date<FileName' '-Keys:CreationDate<FileName' '-FileCreateDate<FileName' '-FileModifyDate<FileName' .
For ffmpeg batches I use something like (the re-encoded movie has the same name with _converted at the end):

for i in *.mp4; do ffmpeg -i "$i" -c:v libx265 -crf 28 -preset medium -timecode 00:00:00:00 -tag:v hvc1 -c:a copy "${i%.*}_converted.mp4"; done
Copy all metadata from movies to all same name and same suffix movies in a different folder and set file dates:

exiftool -m -overwrite_original -api QuickTimeUTC=1 -api LargeFileSupport=1 -tagsFromFile '/Users/matti/Desktop/source_folder/%-.0f.%e' -All:All '-FileCreateDate<QuickTime:CreateDate' '-FileModifyDate<QuickTime:CreateDate' '/Users/matti/Desktop/destination_folder'
...but as to your original question, I guess you must break the commands with -execute (when -execute is used, then all arguments following '-common_args' option are common to all executed commands. '-common_args' option and its arguments MUST come after all other options on the command line) with something like (change the paths accordingly):

exiftool -overwrite_original -api LargeFileSupport=1 -tagsFromFile '/Users/matti/Desktop/source_folder/input.mp4' -All:All '-FileCreateDate<QuickTime:CreateDate' '-FileModifyDate<QuickTime:CreateDate' '/Users/matti/Desktop/destination_folder/output.mp4' -execute -P '-FileName<QuickTime:CreateDate' -d '%Y-%m%d-%H%M-%S%%+2nc.%%e' -common_args -m -api QuickTimeUTC=1 '/Users/matti/Desktop/destination_folder/output.mp4'
Or cd to the destination_folder and then rename all movies there:

exiftool -overwrite_original -api LargeFileSupport=1 -tagsFromFile '/Users/matti/Desktop/source_folder/input.mp4' -All:All '-FileCreateDate<QuickTime:CreateDate' '-FileModifyDate<QuickTime:CreateDate' '/Users/matti/Desktop/destination_folder/output.mp4' -execute -P '-FileName<QuickTime:CreateDate' -d '%Y-%m%d-%H%M-%S%%+2nc.%%e' -common_args -m -api QuickTimeUTC=1 .
I let others chime in for a batch that might handle different file names like input.mov vs output.mp4. But to copy all metadata from movies to all same name and same suffix movies in a different folder and set file dates you could use:

exiftool -m -overwrite_original -api QuickTimeUTC=1 -api LargeFileSupport=1 -tagsFromFile '/Users/matti/Desktop/source_folder/%-.0f.%e' -All:All '-FileCreateDate<QuickTime:CreateDate' '-FileModifyDate<QuickTime:CreateDate' '/Users/matti/Desktop/destination_folder'- Matti



StarGeek

Quote from: trovanch on August 15, 2024, 04:39:30 PMI have a batch of MP4 files that I have compressed using HandBrake. HandBrake does not preserve metadata from original file

You should also know that there will be some metadata that cannot be copied. EXIF data in a video is non-standard, but camera companies shove the data into the file in multiple, different, ways. The best that can be done in this case is to copy the EXIF data to the corresponding XMP tags using the exif2xmp.args file.

GPS tracks also cannot be copied. The best option in this case is to save the data to a GPS track file, such as GPX or KML file. See Inverse Geotagging.
"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

trovanch

#3
Thank you for your detailed response here, Matti.

Quote from: wywh on August 16, 2024, 05:11:35 AMYou don't say which OS you use.
Apologies, it's macOS Sonoma (14.4.1).

Quote from: wywh on August 16, 2024, 05:11:35 AMFWIW I prefer to use ffmpeg for re-encoding.
Indeed, Handbrake is powered by ffmpeg. So, if I understand you correctly, if not using ffmpeg can disregard your first two commands.

Am unsure of difference between mine/your approaches, as inspection appears to yield same result:

exiftool -ee -tagsFromFile src.MP4 -All:All cmp.mp4
exiftool -m -overwrite_original -api QuickTimeUTC=1 -api LargeFileSupport=1 -tagsFromFile input.mp4 -All:All '-FileCreateDate<QuickTime:CreateDate' '-FileModifyDate<QuickTime:CreateDate' output.mp4
This rename command works like a charm:

exiftool -m -P -fileOrder5 FileName -api QuickTimeUTC=1 '-FileName<QuickTime:CreateDate' -d '%Y-%m%d-%H%M-%S%%+2nc.%%e' .
@StarGeek thanks for warnings and considerations with regards to additional metadata.

Beyond that, my brain hurts, but will experiment and have a play.

Thanks again.

Edit: PS, any idea why the Created Date field of web-based file manager (File Station – Synology) shows date modified and how to fix this?