Extracting GPS data from MP4 video file

Started by fakej, December 31, 2017, 11:58:10 AM

Previous topic - Next topic

fakej

Hi Phil,

This is really cool, thank you!
I've just noticed that GPX file contains only basic GPS coordinates (latitude, longtitude). I'm assuming the video file does not have any other data like altitude or speed, correct?

fakej

Phil,
Actually one more comment. I noticed it takes for the exiftool on my laptop around 5s to produce GPX file from ~700MB MP4 video file, while for the python script it takes around 0.5s. I'm guessing exiftool may read the whole file while the nvtk_mp42gpx.py may possibly know where to look for the GPS data. Do you think there is a chance for optimization?

Phil Harvey

#17
I'm glad it works.  ExifTool shouldn't be reading the whole file, but I will look into this.

There is no altitude information, but there is speed and direction as well as lat/lon and time.  But you will have to modify the format file if you want the speed/direction added to the output gpx file.

- Phil

Edit:  But I don't ever expect ExifTool to be as fast as the dedicated script because it does parse all the rest of the metadata in the file.  However, I will check to see if it is loading any of the movie data that it shouldn't be.
...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 ($).

fakej

Phil,

I understand that reading file with a known structure (what python script does) must be faster than discovering structure and reading afterwards (what exiftool does). Anyway thank you for looking into this problem.

Could you possibly advise regarding the gpx.fmt modification?

I tried the following command

root@GB3TO01TR123401:~/Image-ExifTool-10.73# ./exiftool -p /var/www/html/test_drive/gpxext.fmt -ee -ext mp4 -w "$f".gpx /var/www/html/test_drive/test_drive.MP4

trying couple of different versions of gpxext.fmt, where one of them is http://206.142.243.145/test_drive/gpxext.fmt to get speed and direction information but with no success. I must be doing some stupid mistake.

Also is there any chance to ask exiftool what kind of GPS information is available if I don't know that?

Phil Harvey

FAQ 2 explains how to determine the tag names in a file.  In your case you should add -ee to the command.

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

fakej

oh, so it should have been using gpsspeed instead of speed and gspstrack instead of direction. Got it thanks :)
By the way is there an option to multilply gpsspeed by some constant value to get kilometers/miles per hour instead of knots?

I have seen the thread https://exiftool.org/forum/index.php?topic=1780.0 (8 years old already so maybe something has changed in the meantime) so I'm assuming it may not be possible in exiftool and I should use some external tool like sed or awk which is OK.

Phil Harvey

You can do arbitrary math on GPS speed like this in the fmt file:

${gpsspeed;$_*=1.852}

The above multiplies by 1.852 to convert knots to km/h.

This feature is described in the Advanced formatting section of the application documentation.  I doubt this feature existed 8 years ago, but back then this could have been accomplished though user-defined tags.

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

fakej

That's perfect, I really appreciate your support. I promise to spend more time on documentation before asking next question.

Phil Harvey

I realized that I forgot to mention that the date needs to be reformatted for GPX.  The "Usage" example in the fmt file shows how this is done using the -d option.  But I have just come up with a better way to do this using a DateFmt function in the fmt file, like this:

#[BODY]  <time>${gpsdatetime#;DateFmt("%Y-%m-%dT%H:%M:%SZ")}</time>

With this method, the -d option isn't necessary.  I've update the example on the geotagging page with this new technique, and the next ExifTool release will have updated fmt files.

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

gmc

Hi Phil,

Hope you can help, I've got some files from a Garmin dash cam and this method isn't working for me, I'd appreciate it if you can have a look into this for me. I can start a new topic if preferred and send an example of the error I'm getting and an MP4 file.

Thanks!

StarGeek

Unfortunately, Phil, the author, is away on vacation right now until late September.  If you hit the "Notify" button in the upper right corner of this thread, you'll get an email when there's a response.  But definitely check back by October. 
"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

Phil Harvey

Right.  The Garmin doesn't store GPSDateTime.  This will require a user-defined tag to generate this from CreateDate and the SampleTime from each fix.  The actual logic depends on whether CreateDate represents the start or end of the video.  There is a sample config file for DJI that does this, assuming the CreateDate is the end of the video.  For Garmin, a similar config file would be:

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        GPSDateTime => {
            Description => 'GPS Date/Time',
            Groups => { 2 => 'Time' },
            SubDoc => 1,
            Require => {
                0 => 'SampleTime',
                1 => 'Main:CreateDate',
                2 => 'Main:Duration',
            },
            # (assuming CreateDate is the end time of the video, we subtract
            #  Duration because a SampleTime of zero is at the start of the video)
            ValueConv => q{
                my $diff = $val[0] - $val[2];
                my $sign = $diff =~ s/^-// ? '-' : '';
                my $time = $val[1] . '.000';
                ShiftTime($time, "${sign}0:0:$diff");
                return $time;
            },
            PrintConv => '$self->ConvertDateTime($val)',
        },
    },
);


Then use "-config my.config" as the first option in your command.

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

gmc


gmc

Hi Phil,

Unfortunately it's not working, not sure what I could be doing wrong.

Here's the output:

C:\Users\XXXXXXXX\Videos\Dashcam\>"C:\Program Files (x86)\GeoSetter\tools\exiftool.exe" -config my.config -p gpx.fmt -ee -ext mp4 -w OUTDIR/f.gpx 20190908
Error creating 20190908/GRMN0007OUTDIR/f.gpx
Error creating 20190908/GRMN0008OUTDIR/f.gpx
Error creating 20190908/GRMN0009OUTDIR/f.gpx
Error creating 20190908/GRMN0010OUTDIR/f.gpx
Error creating 20190908/GRMN0011OUTDIR/f.gpx
Error creating 20190908/GRMN0012OUTDIR/f.gpx
Error creating 20190908/GRMN0013OUTDIR/f.gpx
Error creating 20190908/GRMN0014OUTDIR/f.gpx
Error creating 20190908/GRMN0015OUTDIR/f.gpx
Error creating 20190908/GRMN0016OUTDIR/f.gpx
Error creating 20190908/GRMN0017OUTDIR/f.gpx
    1 directories scanned
    0 image files read
   11 files could not be read
    0 output files created

I've put the code from your last post in a file named my.config and put this in the same folder as ExifTool, is this right?

Thanks again

Phil Harvey

It looks like you don't have write permission for the output directory.

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