python + hachoir-metadata - reading fps tag from .mp4 file

  • Last Update :
  • Techknowledgy :

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 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))

Suggestion : 2

Creation date: 2014-12-13 19:27:36

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 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

Suggestion : 3

In this quick tutorial, you will learn how you can extract video or audio metadata in Python using FFmpeg.,There are a lot of Python wrappers of FFmpeg. However, ffmpeg-python seems to work well for both simple and complex usage.,Learn how to add a static photo to an audio file to form a video file using MoviePy library in Python.,Learn how to use pikepdf library to extract useful information from PDF files in Python.

To make everything work properly, you need to install FFmpeg. Use this link to get it installed in your environment. Once you have it installed, you need to install the Python wrapper:

$ pip install ffmpeg - python

Below is the code responsible for extracting the metadata:

import ffmpeg
import sys
from pprint
import pprint #
for printing Python dictionaries in a human - readable way

# read the audio / video file from the command line arguments
media_file = sys.argv[1]
# uses ffprobe command to extract all possible metadata from the media file
pprint(ffmpeg.probe(media_file)["streams"])

The ffmpeg.probe() method uses the ffprobe command under the hood. We also use pprint instead of print, so it'll print the Python dictionary in a human-readable way. I'm going to run it on a video file:

$ python extract_media_metadata.py zoo.mp4

That's a lot of information including the duration in seconds, sample rate, codec information, and a lot more. Below is a run on an MP3 file:

$ python extract_media_metadata.py zoo.mp3

Suggestion : 4

Last Updated : 12 Nov, 2020

This module does not come built-in with Python. To install this module type the below command in the terminal.

pip install tinytag

Output:

Title: GeeksForGeeks_Audio
Artist: Neeraj Rana / GFG
Genre: Geek Music
Year Released: 2020
Bitrate: 182.72 kBits / s
Composer: GeeksForGeeks Team
Filesize: 63076 bytes
AlbumArtist: Voice Recorder
Duration: 2.7306458333333334 seconds
TrackTotal: None

Suggestion : 5

(en) Mutagen: audio metadata tag reader and writer (Python),The command hachoir-metadata --mime works like file --mime, and hachoir-metadata --type like file. But today file command supports more file formats then hachoir-metadata.,The hachoir-metadata program is a tool to extract metadata from multimedia files: videos, pictures, sounds, archives, etc. It supports most common file formats:,(en) getid3: Library written in PHP to extact meta-datas from several multimedia file formats (and not only MP3)

$ hachoir - metadata pacte_des_gnous.avi
Common:
   -Duration: 4 min 25 sec -
   Comment: Has audio / video index(248.9 KB) -
   MIME type: video / x - msvideo -
   Endian: Little endian
Video stream:
   -Image width: 600 -
   Image height: 480 -
   Bits / pixel: 24 -
   Compression: DivX v4(fourcc: "divx") -
   Frame rate: 30.0
Audio stream:
   -Channel: stereo -
   Sample rate: 22.1 KHz -
   Compression: MPEG Layer 3
$ hachoir - metadata--mime logo - Kubuntu.png sheep_on_drugs.mp3 wormux_32x32_16c.ico
logo - Kubuntu.png: image / png
sheep_on_drugs.mp3: audio / mpeg
wormux_32x32_16c.ico: image / x - ico
$ hachoir - metadata--type logo - Kubuntu.png sheep_on_drugs.mp3 wormux_32x32_16c.ico
logo - Kubuntu.png: PNG picture: 331 x90x8(alpha layer)
sheep_on_drugs.mp3: MPEG v1 layer III, 128.0 Kbit / sec, 44.1 KHz, Joint stereo
wormux_32x32_16c.ico: Microsoft Windows icon: 16 x16x32
$ hachoir - metadata logo - Kubuntu.png
Image:
   -Image width: 331 -
   Image height: 90 -
   Bits / pixel: 8 -
   Image format: Color index -
   Creation date: 2006 - 05 - 26 09: 41: 46 -
   Compression: deflate -
   MIME type: image / png -
   Endian: Big endian
$ hachoir - metadata--level = 7 logo - Kubuntu.png
Image:
   -Image width: 331 -
   Image height: 90 -
   Bits / pixel: 8 -
   Image format: Color index -
   Creation date: 2006 - 05 - 26 09: 41: 46 -
   Compression: deflate
$ hachoir - metadata--level = 3 logo - Kubuntu.png
Image:
   -Image width: 331 -
   Image height: 90 -
   Bits / pixel: 8 -
   Image format: Color index