Python + Hachoir-Metadata - 从 .MP4 文件中读取 FPS 标签

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

我正在 Python 中编写一个 Windows 应用程序,它必须从 元数据 中读取 [= =23=].MP4 视频文件.

我在 Python 3 开始编写应用程序,但无法找到合适的模块来从视频文件中读取元数据。那时候我用了3to2 to move the entire project to Python 2, so I could install Hachoir-Metadata,全网都在夸,用了pip install hachoir-corepip install hachoir-parser,还有pip install hachoir-metadata

我使用了以下代码:

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

这返回了以下元数据:

这很好,除了我还确实需要知道每秒帧数 (FPS) 的事实之外。对于 .AVI 文件,Hachoir-Metadata 确实显示了 FPS,您可以从此测试输出中看到:

是的,在 .MP4 文件 (100fps) 上设置了 FPS 标签

有没有办法从 .MP4 文件中提取 FPS? 最好包括宽度 (px)、高度 (px)、持续时间和创建时间。

在此先感谢您的帮助!

好的,我设法提取了我需要的所有数据以及更多! This answer on Stack Overflow 让我想到尝试使用 MediaInfo 提取元数据。

为此,我再次切换回 Python 3。我还必须将 MediaInfoDLL3.py 中的第 22 行更改为 MediaInfoDLL_Handler = WinDLL("C:\Program Files (x86)\MediaInfo\MediaInfo_i386.dll")

这是我使用的代码:

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

这返回:

  • 扩展名:MP4
  • 长度:00:00:37.940
  • 每秒帧数:100.000
  • 宽度:1280
  • 身高:960
  • 比率:1.333
  • 帧数:3794
  • 创建日期:2015-01-07 15:25:11.678
  • 修改日期:2014-12-13 19:28:14.000

希望这对某人有所帮助!

这与您处理问题的方式完全不同。我只想获得 mp4 视频的 fps,我通过使用 openCV 获得了它。这不是答案,但我认为它对某人有用。

import cv2
cap = cv2.VideoCapture('name_of_video_file')
fps    = cap.get(cv2.CAP_PROP_FPS)
print 'fps=',fps

您也可以用同样的方式获取其他元数据。例如,

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

本网站将帮助您处理关键字:http://docs.opencv.org/3.1.0/dd/de7/group__videoio.html