copy GPS data and time from jpg to mp4

Started by James Maughan, July 19, 2024, 03:30:13 PM

Previous topic - Next topic

James Maughan

New to this but impressed so far and making progress.

I'm converting and compressing live photos from my iPhone. This leaves me a directory of files like IMG_1.jpg with the correct time and GPS data and IMG_1.mp4 with the time of conversion/compression and no location data.

I'm using EXIFTOOL to manually read the GPS data in the .jpg file and then manually write to the mp4.

exiftool -Keys:GPSCoordinates="43.12345, -73.12345, 123.1" IMG_1.mp4

This works great. And I simply change the media creation time using Windows Explorer.

But I'm betting there's a way to directly copy the GPS and time data from IMG_1.jpg to IMG_1.mp4, and maybe even do a batch?

I don't see an answer and haven't quite been able to figure it out on my own.

Thanks.

Phil Harvey

#1
Hi James,

Something like this may do it:

exiftool -tagsfromfile %d%f.jpg "-keys:gpscoordinates<$gpslatitude#,$gpslongitude#,$gpsaltitude#" -ext mp4 DIR

The "#" at the end of the tag name gives the numerical value.  I don't know if this is strictly necessary, but I did it just to be safe.

- 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 ($).

James Maughan

#2
Well, that does copy all the GPS data like magic from the image files to the movie files! I see it when I inspect the exif data and elsewhere.

Sadly, I've learned here and seen for myself that Google Photos is particular about the format, and will only work when I manually write no more than 5 decimal places. I added the formatting that I thought would do that but it didn't seem to work no matter where or how I put the -c option.

C:\exiftool>exiftool  -tagsfromfile %d%f.jpg -c %.5f "-keys:gpscoordinates<$gpslatitude#,$gpslongitude#,$gpsaltitude#" -ext mp4 \exiftool

Not sure what my next step is but I appreciate your answer above. Maybe I can write a script with a formatted data file.

Thanks again.

James
 

StarGeek

To use the -c (-coordFormat) option, you need to remove the hashtags.  The hashtag is a shortcut for the -n (--printConv) option and that forces the use of the raw numbers, skipping the alterations made by the -c option.

exiftool -tagsfromfile %d%f.jpg -c %.5f "-keys:gpscoordinates<$gpslatitude,$gpslongitude,$gpsaltitude" -ext mp4 \exiftool
* 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).

wywh

Notice that for some reason (?) this command truncates copied altitude to 1 decimal:

exiftool -m -P -overwrite_original -ext mp4 -tagsFromFile %d%f.jpg -c %.5f '-Keys:GPSCoordinates<$GPSLatitude, $GPSLongitude, $GPSAltitude' .

exiftool -a -G1 -s -n -Location:All .
======== ./a.mp4
[Keys]          GPSCoordinates                  : 11.12346 11.12346 11.1
======== ./a.jpg
[GPS]           GPSLatitudeRef                  : N
[GPS]           GPSLatitude                     : 11.123456789
[GPS]           GPSLongitudeRef                 : E
[GPS]           GPSLongitude                    : 11.123456789
[GPS]           GPSAltitudeRef                  : 0
[GPS]           GPSAltitude                     : 11.12345679

The -c option handles latitude/longitude coordinates, not altitude. So adding # just for the altitude preserves the decimals there:

exiftool -m -P -overwrite_original -ext mp4 -tagsFromFile %d%f.jpg -c %.5f '-Keys:GPSCoordinates<$GPSLatitude, $GPSLongitude, $GPSAltitude#' .

exiftool -a -G1 -s -n -Location:All .
======== ./a.mp4
[Keys]          GPSCoordinates                  : 11.12346 11.12346 11.12345679
======== ./a.jpg
[GPS]           GPSLatitudeRef                  : N
[GPS]           GPSLatitude                     : 11.123456789
[GPS]           GPSLongitudeRef                 : E
[GPS]           GPSLongitude                    : 11.123456789
[GPS]           GPSAltitudeRef                  : 0
[GPS]           GPSAltitude                     : 11.12345679

But >4 decimals in the altitude chokes Google Photos. To fix that, truncate all GPS decimals to 4:

exiftool -m -P -overwrite_original -ext mp4 '-Keys:GPSCoordinates#<${GPSCoordinates#;my @a=map {sprintf("%.4f",$_)} split " ";$_="@a"}' .

exiftool -a -G1 -s -n -Location:All .
======== ./a.mp4
[Keys]          GPSCoordinates                  : 11.1235 11.1235 11.1235

https://exiftool.org/forum/index.php?topic=15140.msg86099#msg86099

- Matti




Phil Harvey

You could also truncate altitude to 4 decimals in the first command

exiftool -m -P -overwrite_original -ext mp4 -tagsFromFile %d%f.jpg -c %.5f '-Keys:GPSCoordinates<$GPSLatitude, $GPSLongitude, ${GPSAltitude#;$_=sprintf("%.4f",$_)}' .
- 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 ($).

wywh

Quote from: Phil Harvey on July 21, 2024, 07:40:28 AMYou could also truncate altitude to 4 decimals in the first command

Thanks! Google Photos displays movie location with its best precision when copied with that command. Latitude and Longitude truncated to 5 and Altitude to 4 decimals.

- Matti

James Maughan

Removing the hash tags from the command did it, and it now does just what I want!

C:\exiftool>exiftool  -tagsfromfile %d%f.jpg -c %.5f "-keys:gpscoordinates<$gpslatitude,$gpslongitude,$gpsaltitude" -ext mp4 \exiftool

Thanks everyone for the help. I feel a little bad for not being able to figure it out and bothering you all for help, though I'm sure you're happy to do it (as I guess I am in other forums where I know more).

Thanks again.

- James