Rename MP4 files based on shifted date/time but retaining original date tags

Started by John Derbyshire, June 02, 2024, 07:08:45 PM

Previous topic - Next topic

John Derbyshire

Hi all, I'm trying to rename a folder full of MP4 files to reflect FileModifyDate but with the date/time shifted back 6 hours to reflect the different time zone at the time the footage was taken.

I know I can use the following to set the filenames based on the current value of FileModifyDate:

   exiftool '-FileName<FileModifyDate' -d '%y%m%d %H%M%S%%-c.%%e' .
   
But how do I shift the time, ideally without overwriting the current value of FileModifyDate (so that the rename process is repeatable)?

StarGeek

#1
You could use the GlobalTimeShift option
exiftool '-FileName<FileModifyDate' -globalTimeShift -6 -d '%y%m%d %H%M%S%%-c.%%e' .

or the ShiftTime helper function
exiftool '-FileName<${FileModifyDate;ShiftTime("-6")}' -d '%y%m%d %H%M%S%%-c.%%e' .

Note that, as the name says, the -globalTimeShift affects all date/time tags, so you don't want to use it if you are doing other operations at the same time.
* 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).

John Derbyshire

Firstly, many thanks StarGeek for taking the time to provide the two suggestions.

The first option works fine, and by following your link to the globalTimeShift option I was then able to find https://exiftool.org/Shift.html, from which I could see that I could make a more fine-grained shift (subtracting 6 hours, 1 minute and 47 seconds for example, which I realised I needed to do to bring my camera MP4 footage into line with network time from the photos from my phone camera). So this is what I am using for now:

   exiftool '-FileName<FileModifyDate' -globalTimeShift '-0:0:0 6:1:47' -d '%y%m%d %H%M%S%%-c.%%e' .
   
I have had less success with the second option though. It doesn't report any issues, but it looks like globalTimeShift is doing the work there too, because it doesn't matter what I put in as the ShiftTime value it just seems to do the "-6" from globalTimeShift. So the "-12" appears to be ignored in the following:

   exiftool '-FileName<${FileModifyDate;ShiftTime("-12")}' -globalTimeShift -6 -d '%y%m%d %H%M%S%%-c.%%e' .

I am using the Windows version by the way, in case that makes a difference.

The reason I'd like to persevere with the second option is that it looks similar to a solution that would also shift the timestamp in the filename to reflect the start date/time of the footage rather than the end date/time (by subtracting the clip duration). See https://exiftool.org/forum/index.php?topic=10683.0, although I have not been able to get this to work either.

Ideally I am looking for something that will (a) take the FileModifyDate and subtract e.g. 6h 1m 47s from that then (b) subtract the clip duration from that to give a filename that represents the start date/time of the clip.

My best attempt would be:

   exiftool "-filename<${FileModifyDate;ShiftTime('-0:0:'.$self->GetValue('Duration','ValueConv'))}" -globalTimeShift '-0:0:0 6:1:47' -d '%y%m%d %H%M%S%%-c.%%e' .

But this results in the following:
   
exiftool : Warning: No writable tags set from ./240411 204312.MP4
At line:1 char:1
+ exiftool "-filename<${FileModifyDate;ShiftTime('-0:0:'.$self->GetValu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Warning: No wri...0411 204312.MP4:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
Warning: No writable tags set from ./240411 204442.MP4
    1 directories scanned
    0 image files updated
    2 image files unchanged

Would be very grateful for any further suggestions.

StarGeek

Quote from: John Derbyshire on June 05, 2024, 07:15:28 PMI have had less success with the second option though.

That's because I made a copy/paste error.  It's supposed to be either -globalTimeShift or ShiftTime, not both.  I've corrected my post.

QuoteI am using the Windows version by the way, in case that makes a difference.

It does.  I had assumed by your quotes that you were on Mac/Linux. But you appear to be using Powershell. I can't give you the correct command, since I've given up trying to figure out PS's strange quoting rules. I can only suggest that you swap the double/single quotes and use CMD.

QuoteMy best attempt would be:

    exiftool "-filename<${FileModifyDate;ShiftTime('-0:0:'.$self->GetValue('Duration','ValueConv'))}" -globalTimeShift '-0:0:0 6:1:47' -d '%y%m%d %H%M%S%%-c.%%e' .

Try this in CMD, except change the single quotes into double quotes for this part of the command
-globalTimeShift "-0:0:0 6:1:47" -d "%y%m%d %H%M%S%%-c.%%e"
* 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).

John Derbyshire

I am indeed using PowerShell, but will switch to CMD while I try to get this working.

When I run the following it does the globalTimeShift but it doesn't subtract the duration from the shifted time:

   exiftool "-filename<${FileModifyDate;ShiftTime('-0:0:'.$self->GetValue('Duration','ValueConv'))}" -globalTimeShift "-0:0:0 6:1:47" -d "%y%m%d %H%M%S%%-c.%%e" .

When I remove the globalTimeShift and run the following, it sets the filename back to the original FileModifyDate but again does nothing with the duration:

   exiftool "-filename<${FileModifyDate;ShiftTime('-0:0:'.$self->GetValue('Duration','ValueConv'))}" -d "%y%m%d %H%M%S%%-c.%%e" .

Stripping it back to your second suggestion, it runs without errors but also doesn't change anything:

   exiftool "-FileName<${FileModifyDate;ShiftTime('-6')}" -d "%y%m%d %H%M%S%%-c.%%e" .

So it feels like in all cases ShiftTime is not operating on the FileModifyDate for some reason.

Again, very grateful for any suggestions as to how to get this working.

Phil Harvey

The problem is that ShiftTime only works for standard-format date/time values, so it won't work with a value formatted arbitrarily using the -d option.

You may do what you want in separate steps, but it requires applying the shifts to some date/time value before copying them to the file name in the last step.  You can use a date/time tag in a sidecar file if you won't want to modify the originals.

- 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

I think that's correct.  Changed from the -d (-dateFormat) option to the DateFmt helper function, changed the -globalTimeShift into another ShiftTime.

C:\>exiftool -G1 -a -s -FileModifyDate -Duration Y:\!temp\Test1.mp4
[System]        FileModifyDate                  : 2024:06:06 12:00:00-07:00
[QuickTime]     Duration                        : 5.27 s

C:\>exiftool -G1 -a -s "-testname<${FileModifyDate;ShiftTime('-0:0:'.$self->GetValue('Duration','ValueConv'));ShiftTime('-6:1:47');DateFmt('%y%m%d %H%M%S%%-c.%%e')}"  Y:\!temp\Test1.mp4
'Y:/!temp/Test1.mp4' --> 'Y:/!temp/240606 055808.mp4'
    0 image files updated
    1 image files unchanged

I was getting weird numbers when I kept the -globalTimeShift and I faintly recall something about it, but can't find the post atm.
* 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).

Phil Harvey

Ah yes.  Using DateFmt does the trick, but the expression gets very complicated.

- 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: Phil Harvey on June 07, 2024, 07:47:25 AMAh yes.  Using DateFmt does the trick, but the expression gets very complicated.

Yes, and I hate messy commands like this. It becomes too easy to make a mistake or type and gets hard to figure out if you forget about it and then need to edit it.
* 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).

John Derbyshire

Thank you both so much for your help on this and actually getting it working! I was looking into the sidecar file option in the meantime but I much prefer this one-step approach :)

So the final Command Prompt command that I used (i.e. to actually do the renames) is as follows:

exiftool -G1 -a -s "-Filename<${FileModifyDate;ShiftTime('-0:0:'.$self->GetValue('Duration','ValueConv'));ShiftTime('-6:1:47');DateFmt('%y%m%d %H%M%S%%-c.%%e')}" *.MP4