簡體   English   中英

Python + Hachoir-Metadata-從.MP4文件讀取FPS標簽

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

我正在用Python編寫Windows應用程序,該應用程序必須從.MP4視頻文件讀取元數據

我開始用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類型:視頻/快速時間
  • 字節序:大字節序

這很棒,除了我還真的需要知道每秒的幀數(FPS)的事實。對於.AVI文件,Hachoir-Metadata確實顯示了FPS,如您從此測試輸出中所看到的:

  • 持續時間:6秒66毫秒
  • 圖像寬度:256像素
  • 圖像高度:240像素
  • 幀率:30.0 fps
  • 比特率:884.4 Kbit / sec
  • 注釋:具有音頻/視頻索引(2920字節)
  • MIME類型:video / x-msvideo
  • 字節序:小字節序

是的,在FPS標簽設置在.MP4文件(100FPS)。

有沒有辦法從.MP4文件中提取FPS? 最好還包括width(px),height(px),持續時間和創建時間。

在此先感謝您的幫助!

好的,我設法提取了我需要的所有數據以及更多信息! 關於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
  • 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))

該站點將為您提供關鍵字幫助: http : //docs.opencv.org/3.1.0/dd/de7/group__videoio.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM