Read metadata and write it to XMP plus correct rec time with just one command

Started by Wladimir, October 06, 2021, 11:11:08 AM

Previous topic - Next topic

Wladimir

Hi,
first of all thanks to Phil for this ingenious tool!! I was able to solve many problems with Exiftool. But now I am facing the following problem and ask for your support:

I use a photo / video server that reads the video metadata from the XMP. For example, embedded GPS data is not visible. If they are in the XMP, they are.

Is there a possibility with Exiftool to read out all metadata contained in a video file (mp4) and to write it into the XMP segment with just one command (title, tags, description, recording date, recording time, GPS and rating)?

I was able to solve individual topics as follows (but only ever with individual commands):

I was able to solve the problem with the embedded GPS data with the following command to write it into the XMP segment:
exiftool "-xmp: GPSLongitude <GPSLongitude" "-xmp: GPSLatitude <GPSLatitude" -overwrite_original ....

In the case of video, there is also the problem that the time difference is not taken into account in the recording time. However, the file name contains the correct date and time including the time shift. I made this correction with the following command, which I found in this way in internet:
exiftool -if "$filename =~ /^VID-\d{8}-WA\d{4}\.\w*/" "-alldates<filename" -overwrite_original ....

Is there a command with which everything is read out and copied to XMP and at the same time the recording time is corrected according to the file name?

Thanks in advance for any help.

Regards, Wladimir

StarGeek

To be clear, you want to copy the GPS coordinates on all files and conditionally copy the filename into AllDates.

The answer is, sort of.

You can combine the two commands using the -execute option -Common_Args option.  But there is no difference between doing this and running two separate commands, as it will still require two separate passes over all the files.  IMO, this adds unnecessary complications and makes troubleshooting problems more difficult, especially if you are still learning how to use exiftool.

An example of such a command would be
exiftool -if "$filename=~/^VID-\d{8}-WA\d{4}\.\w*/" "-alldates<filename" -execute "-xmp:GPSLongitude<GPSLongitude" "-xmp:GPSLatitude<GPSLatitude" -common_args -overwrite_original /path/to/files/

Quote from: Wladimir on October 06, 2021, 11:11:08 AM
In the case of video, there is also the problem that the time difference is not taken into account in the recording time. However, the file name contains the correct date and time including the time shift.

One thing to take into account here is the fact that some video time stamps are supposed to be set to UTC, not local time.  See the fourth paragraph on the Quicktime tags page.  If you change the Quicktime:CreateDate/Quicktime:ModifyDate to local time, Windows will display the incorrect time under "Media Created" in the Properties window.  I believe Mac will also do something similar.
"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

Wladimir

Thank you for the quick and detailed answer, which helps me a lot.

Sorry, I chose the examples unhappily. I want to correct the time as well as copy the GPS data to the XMP. Both are only required for videos.

Since I'm not sure whether other relevant data is embedded in the video besides GPS, I have to ask the additional question:

is there a command that reads out all available data and copies it to XMP?

Thanks also for the hint regarding UTC. In my case it is so, that my mobile phone (Samsung S10+) saves the creation time with the local time and the recording time is saved in the Exif data in UTC without time shift. Since the photo/video server takes the Exif-recording time data, the videos are classified incorrectly in terms of time. Pictures are displayed correctly and the videos are not, depending on the respective time difference.

Is there possibly another / better solution instead of using the Alldates command?

StarGeek

Quote from: Wladimir on October 06, 2021, 12:02:56 PM
is there a command that reads out all available data and copies it to XMP?

You can try using something like
exiftool -TagsFromFile @ -All /path/to/files/

What this command will do is copy all the tags back into the file, but in their preferred location, which will be the XMP tags.  But it will only work for tags that have the same name in either the Quicktime or Composite groups as the name in the XMP group.  There are quite likely to be some tags that you may want to copy but don't have the same name in XMP.  You would have to add those in specifically copying from one tag to the other.

QuoteIs there possibly another / better solution instead of using the Alldates command?

AllDates isn't necessarily a bad solution, but you do have to understand what it is composed of.  AllDates is a Shortcut tag which is comprised of three common date/time tags, CreateDate, DateTimeOriginal, and ModifyDate.  All three of these have matching tags in Quicktime and XMP, so when you write to AllDates in a video file, it will write to all six tags.

The very best option would be to make sure there's a time zone available when you copy and you use the -api QuickTimeUTC option.  That way all tags will get adjusted properly and include the time zone if allowed.

Run this command to view all the time related tags in the file
exiftool -time:all -G1 -a -s File.mp4

Look over the results and see if there's a tag that has the correct time and includes the time zone.  If there is, then you would use
"-AllDates<TAG" -api QuickTimeUTC
replacing TAG with the name of the tag you found.

If there isn't one that includes the time zone, then you would include the time zone manually with this
"-AllDates<$TAG ±00:00" -api QuickTimeUTC
replacing ±00:00 with the actual time zone.
"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

Wladimir

Thank you again for your answer and support. Now the whole subject becomes clearer to me  ::)

In the next few days I will analyze my files with the command you mentioned and test the whole thing.
In any case, now I know the most essential things to be able to delve deeper into the subject. At least that's what I hope  :)

Regards, Wladimir

StarGeek

I also just realized that there may be actual EXIF data in the file since it comes from a camera.  If that's the case, then there is something you can do to copy that EXIF data into XMP more easily.

Go to Github's exiftool repository and save the exif2xmp.args file on your computer, preferably in the same directory as exiftool.  You could then run this command and all the EXIF data will be copied to the matching locations in XMP
exiftool -TagsFromFile @ -@ exif2xmp.args /path/to/files/

Use the full path to exif2xmp.args if it's not in the same directory as exiftool.
"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

Wladimir

Hi ,
thank you for your additional answer. This way I managed to copy the GPS data, the title and the tags / categories to XMP.

At first I accidentally tried without the file exif2xmp.args, then I tried a second time with the file, but it made no difference.
However, this is probably due to the fact that the data are not stored in Exif, but elsewhere in the file. When I analyze the file in the Exiftool GUI, the EXIF ​​and XMP sections of the original file are basically empty. Only the ALL-tab shows the whole data.

The analysis of the times showed, that the time difference/time shift is only taken into account for the system creation date. See the screenshot in the appendix.

StarGeek

Quote from: Wladimir on October 07, 2021, 04:41:10 AM
The analysis of the times showed, that the time difference/time shift is only taken into account for the system creation date. See the screenshot in the appendix.

Based upon the filename and assuming the +02:00 time zone, the various Quicktime and Track* times are correct as those are all properly set to UTC, though off by a few seconds with 04 seconds in the filename and 09 in the file.  Minor time differences in video files are not uncommon, as different camera models use different criteria for deciding when a video starts.
"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

Wladimir

Quote from: StarGeek on October 07, 2021, 10:48:18 AM
Quote from: Wladimir on October 07, 2021, 04:41:10 AM
The analysis of the times showed, that the time difference/time shift is only taken into account for the system creation date. See the screenshot in the appendix.

Based upon the filename and assuming the +02:00 time zone, the various Quicktime and Track* times are correct as those are all properly set to UTC, though off by a few seconds with 04 seconds in the filename and 09 in the file.  Minor time differences in video files are not uncommon, as different camera models use different criteria for deciding when a video starts.

Yes, the time difference is absolutely ok. I thought that it has to do with the duration of the video. But that is not essential. The important insight for me is, that I now know, that I can correct the recording time only via the file name. Since all other information is entered in UTC and the specification of how many hours the time difference is, is not specified anywhere in the file itself.

Thanks again for your support! It is not a given that a newbie receives such great support!

Wladimir

Hello StarGeek,
may I ask you again for your support: when trying to automate my topic with the creation date of video files and copying the GPS data to XMP, I came across a topic with the if-statement. It is about executing a command depending on the format of the file name.

My goal is this:

I only want to change the creation date if the correct data is actually given in the file name. On Samsung phones, the video filename is in the format "yyyymmdd_hhmmss.mp4". With another mobile phone the format is "VID-yyyymmdd_hhmmss.mp4"

The creation time is also deleted for Whatsapp images, but at least the date of the dispatch is included in the file name and in this case I would use that. Whatsapp images have the format "IMG-yyyymmdd-WA000n.jpg". Whatsapp videos, on the other hand, are in the VID-yyyymmdd-WA000n.mp4 format.

Although I have found working examples on the Internet that execute a command depending on the file name, but I want to understand these so that I can assemble appropriate commands myself if necessary.

Example from Internet: tag "Whatsapp" on a Whatsapp file:

exiftool -if "$filename=~/^IMG-\d{8}-WA\d{4}\.\w*/" -if "not defined $Keywords or $Keywords!~/WhatsApp/i" "-Keywords+=WhatsApp" -overwrite_original C:\path\to\file -v -r -i "@eaDir" -ext JPG . -fast2

What I don't understand is the structure and syntax of the two IF statements. And I wasn't aware that you can apparently combine two IF commands !?

Can you perhaps explain to me what the following two parts of the command mean exactly? Or can you give me a hint where I can look up the syntax used?

"-if "$filename=~/^IMG-\d{8}-WA\d{4}\.\w*/"

-if "not defined $Keywords or $Keywords!~/WhatsApp/i"

And can you actually combine several if-statements?

Or am I completely on the wrong track with the whole topic anyway?

My idea was that these commands should only be executed on those files for which the command is actually intended. In addition, the commands should only be made once for each file. So I might write a keyword that shows that the command has already been carried out on the corresponding file.

But it may well be that I think too complicated and make my life unnecessarily difficult.

Regards, Wladimir

Phil Harvey

Hi Wladimir,

Quote from: Wladimir on October 26, 2021, 12:22:31 PM
What I don't understand is the structure and syntax of the two IF statements. And I wasn't aware that you can apparently combine two IF commands !?

Multiple -if options are combined with a logical "and":

-if "COND1" -if "COND2" is the same as -if "(COND1) and (COND2)"

QuoteCan you perhaps explain to me what the following two parts of the command mean exactly? Or can you give me a hint where I can look up the syntax used?

"-if "$filename=~/^IMG-\d{8}-WA\d{4}\.\w*/"

StarGeek may likely explain this in more detail, but this is a regular expression which is true if the FileName starts with "IMG-" followed by 8 digits then "-WA" then 4 more digits, then a decimal and some more alphanumeric characters.  Google for Perl regular expressions if you want to learn more about this.

- 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: Wladimir on October 26, 2021, 12:22:31 PM
Example from Internet: tag "Whatsapp" on a Whatsapp file:

exiftool -if "$filename=~/^IMG-\d{8}-WA\d{4}\.\w*/" -if "not defined $Keywords or $Keywords!~/WhatsApp/i" "-Keywords+=WhatsApp" -overwrite_original C:\path\to\file -v -r -i "@eaDir" -ext JPG . -fast2

What I don't understand is the structure and syntax of the two IF statements. And I wasn't aware that you can apparently combine two IF commands !?

From the docs on the -if option"
     When multiple -if options are used, all conditions must be satisfied to process the file.
Personally, I tend to combine them to make a single -if but that's just a manner of style.

In the case of -if "$filename=~/^IMG-\d{8}-WA\d{4}\.\w*/", this is comparing the Filename to a regex pattern.  The pattern is that it must start ^ with the characters IMG- (case sensitive), 8 numbers \d{8}, a dash, the characters WA (case sensitive) then 4 numbers \d{4}, a literal dot, as the dot is normally a single character wild card \. followed by 0 or more non-whitespace (space, tab, linefeed, etc) characters.

Here's some examples of this on RegEx101, which also gives a step by step breakdown in the upper right under "Explanation".

The second -if statement, -if "not defined $Keywords or $Keywords!~/WhatsApp/i" first checks to make sure that there are actual Keywords defined. This is more precise than just using -if "not $Keywords" because the latter just check true or false, and if there was a single keyword of the number 0, then that would be "not 0" -> True.  However, since this really isn't necessary in this case because a simple check for $Keywords!~/WhatsApp/i is really all that's needed.  For that part, it is checking to make sure the string "WhatsApp" (case INsensitive) is not among the keywords.  This is a NOT because it is using !~ instead of =~.

QuoteOr can you give me a hint where I can look up the syntax used?

As for where to look up the how this is done, search for tutorials on Regular Expressions (RexEx).  Overall, most tutorials will work but the exact type of regex would be for Perl.  You can use the RegEx101 site linked above to test regular expressions to see how they work.

QuoteAnd can you actually combine several if-statements?

To combine them, you could use this
-if ""$filename=~/^IMG-\d{8}-WA\d{4}\.\w+/i and $Keywords!~/WhatsApp/i"
Here, I changed the \w* to \w+.  Minor, but this makes sure that there is an extension.  Also added the i after the slash to make the regex case insensitive.  Then combined using and so that the filename must match and there can't already exist a "WhatsApp" keyword.
"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

Wladimir

Thank you Phil and StarGeek for your quick help. And you StarGeek for your very detailed explanation.

Regards, Wladimir

Wladimir

Hi there,
as described above was my idea, that the commands (copy GPS to XMP, correct create time according to file name) should only be made once for each file, even the command is executed several times. The background is, that I want to start the Exiftool command automatically once a day so that all newly added video files are corrected accordingly. But it doesn't make sense to edit files that have already been corrected over and over again.

So my idea was to add a specific keyword, or in case of video a xmp-subject "Exiftool" after first execution of the command. And if next time this xmp-subject is present, then the command will not be executed again.

exiftool -if "$-xmp:subject!~/Exiftool/i" "-alldates<filename" -execute -if "$-xmp:subject!~/Exiftool/i" "-xmp:GPSLongitude<GPSLongitude" "-xmp:GPSLatitude<GPSLatitude" -execute -if "$-xmp:subject!~/Exiftool/i" "-xmp:subject+=Exiftool" -common_args -overwrite_original C:\Path\To\Folder\ -r -ext mp4 -fast2

So much for the theory. In practice this does not work with the query of the xmp-subject and the command is carried out over and over again. And the subject "Exiftool " is also rewritten again and again.

In order to find out whether all three commands are actually executed again and again or whether only the writing of the tag does not work correctly, I took the third part out and tried it like this:

exiftool -if "$-xmp:subject!~/Exiftool/i" "-alldates<filename" -execute -if "$-xmp:subject!~/Exiftool/i" "-xmp:GPSLongitude<GPSLongitude" "-xmp:GPSLatitude<GPSLatitude" -common_args -overwrite_original C:\Path\To\Folder\ -r -ext mp4 -fast2

In this case, too, the GPS data was copied and the create time corrected, although the tag "Exiftool " already existed.

During the test there was only one video file in the folder and there are no sub-folders in this folder. The output was a bit confusing:

======== C:/Downloads/Folder/USA_Test/20170813_111724.mp4
======== C:/Downloads/Folder/USA_Test/20170813_111724.mp4
======== ./20170813_111724.mp4
    2 directories scanned
    3 image files updated
    2 directories scanned
    3 image files updated
Error: File not found - exiftool
<-END-

The message "Error: File not found - exiftool" is also confusing, as there is only one file in this folder and it has also been processed.

Is there any other way of querying that a file is only processed if it has not yet been processed.

In the case of GPS data, it would be probably possible to check whether the XMP already contains GPS data and if not, then they should be copied.

Is it possible to compare somehow with the create date whether it already corresponds to the information in the file name?

Thanks in advance for any tips. Regards, Wladimir

StarGeek

If you're using PowerShell instead of CMD, then you need to swap double/single quotes.  Double quotes around the dollar sign $ indicate the start of a shell variable.

Also be aware that PowerShell can cause problems with exiftool.  For example, redirecting/piping binary data will be corrupted in PS.  There are several other instances of problems with PS on these forums.
"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