Extracting metadata from video file

Started by sarcha, November 08, 2022, 12:27:24 PM

Previous topic - Next topic

sarcha

Hii Geeks,
I have tried extracting metadata from MP4 file using the following code in python on Windows. I know its particularly easy from command line but unfortunately the requirements are such that I have to gather output from this code.

So the issue is while calling exiftool_command in the following code, when I give the copied path of fmt file location eg "C:\Users\sara\Projects\Drone\dist\gpx.fmt" it accepts and the output folder contians the metadata. But if I give "fmtlocation" it does not accept instead the output file contains only strings as '-fmtlocation' .

can you please help me with this? If there is an easier solution to extract metadata from video file I would be thankful for that.

Exiftool version 12.3.3.0

import os
import os.path
import subprocess

path = os.getcwd()
filename = 'MP4_csv.py'
file_path = os.path.join(path, filename)
#print(file_path)

fmtfile = input('Please write the name of fmt file with extension  ')
fmtlocation = os.path.join(path, fmtfile)

exiftoolname = input('Please write the name of exiftool file with extension  ')
exiftoollocation = os.path.join(path, exiftoolname)

                 
videoname = input('Please write the name of video file with extension  ')
videolocation = os.path.join(path, videoname)
     
full_gpx_path = videolocation.replace(".MP4", ".GPX")
with open(full_gpx_path, "w") as gpx_file:
    exiftool_command = ["exiftool", "-ee", "-p", "fmtlocation", videolocation]
    subprocess.run(exiftool_command, stdout=(gpx_file))
    print(f"Successfully created: {full_gpx_path}\n")
           

StarGeek

I don't know python, but I'm guessing that fmtlocation should not be in quotes, similar to how videolocation is not in quotes.
* 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).

sarcha

Hey yes that was my mistake. It worked. Thank you.

I am actually looking to extract metadata into a csv file. The code helps me extract it in gpx file. Is there a way to directly extract the metadata in csv file or convert the gpx file in csv file?

Phil Harvey

You can use the -p option to create a CSV-format file, something like this:

exiftool -n -ee -p "$gpsdatetime,$gpslatitude,$gpslongitude" FILE

If you want, you can continue to use a format file.  For the above command, the format file would be:

$gpsdatetime,$gpslatitude,$gpslongitude
- 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 ($).

sarcha

Hey Phil thank you for your input.

I just used this to run the same command from python

import os
os.system('cmd /c "exiftool -ee -G3 -s C1208.mp4 > metadata.csv"')

and it extracted me the metadata file from video file in csv format.

Phil Harvey

That isn't CSV format.  But if that's what you wanted, then great.

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

bparks0263

Have you looked at PyExifTool? https://pypi.org/project/PyExifTool/

PyExifTool is a Python library to communicate with an instance of Phil Harvey's ExifTool command-line application.

The library provides the class exiftool.ExifTool that runs the command-line tool in batch mode and features methods to send commands to that program, including methods to extract meta-information from one or more image files. Since exiftool is run in batch mode, only a single instance needs to be launched and can be reused for many queries. This is much more efficient than launching a separate process for every single query.