I'm writing a Windows application in Python which has to read metadata from an .MP4 video file.
I started writing the application in Python 3, but was unable to find a suitable module to read metadata from video files. That's when I used 3to2 to move the entire project to Python 2, so I could install Hachoir-Metadata, which was praised all over the net, using pip install hachoir-core
, pip install hachoir-parser
, and pip install hachoir-metadata
I used the following code:
from hachoir_core.error import HachoirError
from hachoir_core.cmd_line import unicodeFilename
from hachoir_parser import createParser
from hachoir_core.tools import makePrintable
from hachoir_metadata import extractMetadata
from hachoir_core.i18n import getTerminalCharset
# Get metadata for video file
def metadata_for(filename):
filename, realname = unicodeFilename(filename), filename
parser = createParser(filename, realname)
if not parser:
print "Unable to parse file"
exit(1)
try:
metadata = extractMetadata(parser)
except HachoirError, err:
print "Metadata extraction error: %s" % unicode(err)
metadata = None
if not metadata:
print "Unable to extract metadata"
exit(1)
text = metadata.exportPlaintext()
charset = getTerminalCharset()
for line in text:
print makePrintable(line, charset)
return metadata
pathname = c:/video.mp4
meta = metadata_for(pathname)
print meta
This returned the following metadata:
This is great, except for the fact that I also really need to know the frames per second (FPS).. For .AVI files Hachoir-Metadata does show the FPS, as you can see from this test output:
And yes, the FPS tag is set on the .MP4 file (100fps).
Is there a way to extract the FPS from a .MP4 file? Preferably including width(px), height(px), duration, and creation time as well.
Thanks in advance for any help!
Ok, I managed to extract all the data I need and more! This answer on Stack Overflow gave me the idea to try MediaInfo to extract metadata.
For this I switched back to Python 3 again. I also had to change line 22 in MediaInfoDLL3.py
to MediaInfoDLL_Handler = WinDLL("C:\Program Files (x86)\MediaInfo\MediaInfo_i386.dll")
This is the code I used:
import os
os.chdir(os.environ["PROGRAMFILES"] + "\\mediainfo") # The folder where you installed MediaInfo
from MediaInfoDLL3 import MediaInfo, Stream
MI = MediaInfo()
def get_mediainfo_from(directory):
for file in os.listdir(directory):
MI.Open(directory + file)
file_extension = MI.Get(Stream.General, 0, "FileExtension")
duration_string = MI.Get(Stream.Video, 0, "Duration/String3") # Length. "Duration" for ms
fps_string = MI.Get(Stream.Video, 0, "FrameRate")
width_string = MI.Get(Stream.Video, 0, "Width")
height_string = MI.Get(Stream.Video, 0, "Height")
aspect_ratio_string = MI.Get(Stream.Video, 0, "DisplayAspectRatio")
frames_string = MI.Get(Stream.Video, 0, "FrameCount")
local_created_date_string = MI.Get(Stream.General, 0, "File_Created_Date_Local") # Date of copying
local_modified_date_string = MI.Get(Stream.General, 0, "File_Modified_Date_Local") # Date of filming
if file_extension == "MP4":
print("Extension: "+file_extension)
print("Length: "+duration_string)
print("FPS: "+fps_string)
print("Width: "+width_string)
print("Height: "+height_string)
print("Ratio: "+aspect_ratio_string)
print("Frames: "+frames_string)
print("Created Date: "+local_created_date_string)
print("Modified Date: "+local_modified_date_string)
else:
print("{} ain't no MP4 file!".format(file))
MI.Close()
get_mediainfo_from("C:\\Users\\Nick\\Desktop\\test\\") # The folder with video files
# print(MI.Option("Info_Parameters")) # Show list of available metadata tags
This returned:
Hopefully this helps someone!
This is totally different from the way you have approached with the problem. I wanted to get only the fps of a mp4 video and I got it by using openCV. This is not an answer but I thought it will be useful for someone.
import cv2
cap = cv2.VideoCapture('name_of_video_file')
fps = cap.get(cv2.CAP_PROP_FPS)
print 'fps=',fps
You can get other metadata also in the same way. For eg.,
length_of_video = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
This site will help you with the keywords: http://docs.opencv.org/3.1.0/dd/de7/group__videoio.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With