Python + Hachoir-Metadata - read FPS tag from .MP4 file

I am writing a Windows application in Python that should read metadata from a .MP4 video file .

I started writing an application in Python 3, but I could not find a suitable module for reading metadata from video files. This is when I used 3to2 to move the entire project to Python 2, so I could install Hachoir-Metadata , which was evaluated all over the network 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 seconds 940 ms
  • Image Width: 1280 pixels
  • Image height: 960 pixels
  • Date Created: 2014-12-13 19:27:36
  • Last modification: 2014-12-13 19:27:36
  • Comment: Playback speed: 100.0%
  • Comment: User volume: 100.0%
  • MIME Type: Video / Fast Time
  • Endianness: Big endian

This is great, except for the fact that I also really need to know frames per second (FPS). For .AVI files, Hachoir-Metadata shows 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
  • Bitrate: 884.4 Kbps
  • Comment: has an audio / video index (2920 bytes)
  • MIME Type: Video / x -msvideo
  • Endianness: little endian

And yes, the FPS tag is set in the .MP4 file (100 frames per second).

Is there a way to extract FPS from a .MP4 file? It is preferable to include width (px), height (px), duration and creation time.

Thanks in advance for your help!

+5
source share
2 answers

Well, I managed to collect all the data I needed and much more! This stack overflow answer gave me the idea to try MediaInfo to extract metadata.

To do this, I returned to Python 3. 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 returns:

  • Extension: MP4
  • Duration: 00: 00: 37.940
  • FPS: 100,000
  • Width: 1280
  • Height: 960
  • Ratio: 1.333
  • Frames: 3794
  • Created Date: 2015-01-07 15: 25: 11.678
  • Modified date: 2014-12-13 19: 28: 14.000

Hope this helps someone!

+4
source

This is completely different from how you came across this problem. I wanted to get only fps mp4 video and I got it using openCV. This is not an answer, but I thought it would 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 also get other metadata. For instance,

 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

+1
source

Source: https://habr.com/ru/post/1210699/


All Articles