Appending all video filenames with duration

Started by ifihadahammer, October 28, 2022, 03:53:26 AM

Previous topic - Next topic

ifihadahammer

First, a big thanks for exiftool.

I'm trying to use it for something that seems incredibly simple, so I'm hoping I am just being myopic and not seeing how to do something obvious.

I want to append all video filenames with the duration of each video in the standard hh:mm:ss format.

I tried:

exiftool.exe "-testname<duration" .
I'm still using Windows (yes, need to move to Linux), so it balks at having any filenames that include colons.  Okay, no problem, I thought.  So I tried getting exiftool to use a different character to delineate the hours from the minutes from the seconds (my preference is an underscore).  After an hour of effort and reading all the exiftool help, I still have no solution.

How can this be done?

(BTW, I would prefer to omit the hours if the video is under 1 hour, but beggars can't be choosers.  Also, if the video is under 10 hours, but at least 1 hour, I would prefer to not have a leading zero for the hours.  But again, beggars can't be choosers, so I'll gratefully accept good olde hh_mm_ss.  Oh, and please, please, please, don't tell me we can only use seconds... I don't want to have to break out a calculator to convert seconds to hh_mm_ss!)

Phil Harvey

Maybe something like this?:

exiftool.exe "-testname<%f-${duration;tr/:/_/}.%e" DIR

This will add "-#.## s" for lengths under 30 seconds, then "-#_##_##" for lengths greater than that.

- Phil

To do exactly what you ask is more complicted:

exiftool.exe "-testname<%f-${duration;tr/:/_/;s/^0_//;s/^0//;s/^(\d{2})\.\d+ s/0_$1/;s/^(\d)\.\d+ s/0_0$1/}.%e" DIR
...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 ($).

ifihadahammer

Quote from: Phil Harvey on October 28, 2022, 01:58:03 PMMaybe something like this?:

exiftool.exe "-testname<%f-${duration;tr/:/_/}.%e" DIR

This will add "-#.## s" for lengths under 30 seconds, then "-#_##_##" for lengths greater than that.

- Phil

To do exactly what you ask is more complicted:

exiftool.exe "-testname<%f-${duration;tr/:/_/;s/^0_//;s/^0//;s/^(\d{2})\.\d+ s/0_$1/;s/^(\d)\.\d+ s/0_0$1/}.%e" DIR


Thanks so very much Phil!

I'm going to go through the command lines you provided, character by character, to understand them completely.  I'm skilled with regex, so much of what you created is familiar, but I'm not skilled with exiftool, so some of it looks quite confusing to me (even after reading all the exiftool help).

StarGeek

Quote from: ifihadahammer on October 29, 2022, 04:43:53 AMI'm skilled with regex, so much of what you created is familiar, but I'm not skilled with exiftool, so some of it looks quite confusing to me (even after reading all the exiftool help).

It's actually skill with Perl that is necessary, as it's basically Perl code executed inside the tag name.  So in this case, in a Perl script it would look like
tr/:/_/;
s/^0_//;
s/^0//;
s/^(\d{2})\.\d+ s/0_$1/;
s/^(\d)\.\d+ s/0_0$1/;
which would all use the Perl default variable $_, which holds the value of the tag.
"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