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")
I don't know python, but I'm guessing that fmtlocation should not be in quotes, similar to how videolocation is not in quotes.
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?
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
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.
That isn't CSV format. But if that's what you wanted, then great.
- Phil
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.