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:
- Duration: 37 sec 940 ms
- Image width: 1280 pixels
- Image height: 960 pixels
- Creation date: 2014-12-13 19:27:36
- Last modification: 2014-12-13 19:27:36
- Comment: Play speed: 100.0%
- Comment: User volume: 100.0%
- MIME type: video/quicktime
- Endianness: Big endian
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:
- Duration: 6 sec 66 ms
- Image width: 256 pixels
- Image height: 240 pixels
- Frame rate: 30.0 fps
- Bit rate: 884.4 Kbit/sec
- Comment: Has audio/video index (2920 bytes)
- MIME type: video/x-msvideo
- Endianness: Little endian
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!