Python+Hachoir元数据-从.MP4 fi读取FPS标记

2024-04-18 22:08:16 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我开始用Python 3编写应用程序,但找不到合适的模块来读取视频文件中的元数据。那时候我用3to2把整个项目移到Python 2,这样我就可以安装Hachoir-Metadata,这在网络上广受好评,使用pip install hachoir-corepip install hachoir-parserpip 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

这将返回以下元数据:

  • 持续时间:37秒940毫秒
  • 图像宽度:1280像素
  • 图像高度:960像素
  • 创作日期:2014-12-13 19:27:36
  • 上次修改时间:2014-12-13 19:27:36
  • 点评:播放速度:100.0%
  • 评论:用户量:100.0%
  • MIME类型:video/quicktime
  • 端部:大端部

这很好,除了我还需要知道每秒帧数(FPS)。。对于.AVI文件,Hachoir元数据确实会显示FPS,您可以从该测试输出中看到:

  • 持续时间:6秒66毫秒
  • 图像宽度:256像素
  • 图像高度:240像素
  • 帧速率:30.0 fps
  • 比特率:884.4kbit/sec
  • 注释:有音频/视频索引(2920字节)
  • MIME类型:video/x-msvideo
  • Endianness:小endian

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

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

提前感谢您的帮助!


Tags: pip数据fromcore图像importparserfor
2条回答

好吧,我设法提取了我需要的所有数据和更多!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
  • 浮点数:100000
  • 宽度: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

相关问题 更多 >