Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python+;Hachoir元数据-从.MP4文件读取FPS标记_Python_Video_Metadata_Mp4_Hachoir Parser - Fatal编程技术网

Python+;Hachoir元数据-从.MP4文件读取FPS标记

Python+;Hachoir元数据-从.MP4文件读取FPS标记,python,video,metadata,mp4,hachoir-parser,Python,Video,Metadata,Mp4,Hachoir Parser,我正在用Python编写一个Windows应用程序,它必须从.MP4视频文件中读取元数据 我开始用Python3编写应用程序,但找不到合适的模块从视频文件中读取元数据。当时我将整个项目转移到Python2上,这样我就可以安装,这在网络上广受好评,使用pip-install-hachoir-core,pip-install-hachoir-parser,以及pip-install-hachoir-metadata 我使用了以下代码: from hachoir_core.error import H

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

我开始用Python3编写应用程序,但找不到合适的模块从视频文件中读取元数据。当时我将整个项目转移到Python2上,这样我就可以安装,这在网络上广受好评,使用
pip-install-hachoir-core
pip-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
这返回了以下元数据:

  • 持续时间:37秒940毫秒
  • 图像宽度:1280像素
  • 图像高度:960像素
  • 创建日期:2014-12-13 19:27:36
  • 最后修改:2014-12-13 19:27:36
  • 评论:播放速度:100.0%
  • 评论:用户数量:100.0%
  • MIME类型:视频/快速时间
  • Endianness:大endian
这是伟大的,除了事实,我还真的需要知道每秒帧数(FPS)。。对于.AVI文件,Hachoir元数据确实显示FPS,正如您从该测试输出中看到的:

  • 持续时间:6秒66毫秒
  • 图像宽度:256像素
  • 图像高度:240像素
  • 帧速率:30.0 fps
  • 比特率:884.4千比特/秒
  • 注释:有音频/视频索引(2920字节)
  • MIME类型:视频/x-msvideo
  • endian:小endian
是的,FPS标记设置在.MP4文件(100fps)上

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


提前感谢您的帮助

好的,我设法提取了我需要的所有数据,还有更多!让我想到尝试MediaInfo来提取元数据

为此,我再次切换回Python 3。我还不得不将
MediaInfoDLL3.py
中的第22行更改为
MediaInfoDLL\u Handler=windlel(“C:\Program Files(x86)\MediaInfo\MediaInfo\u 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
  • FPS: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))

本网站将为您提供关键字帮助:

您显示的数据包含您请求的所有信息,但对于视频文件而言,100 fps听起来不太可能-MP4/quicktime没有真正的帧速率,因为它使用关键帧和插值帧。我怀疑报告的
100 fps
不正确标记
播放速度=100%
和Hachoir是正确的。@SteveBarnes感谢您的快速回答!该视频是使用GoPro HERO3黑色版本拍摄的,该版本能够以每秒100帧的速度拍摄。Windows资源管理器>文件属性>详细信息选项卡也显示了100 fps。很高兴你得到了排序@kreguscan
metadata\u,用于
方法获取文件而不是文件路径?感谢分享,这很有帮助!