[英]Python + Hachoir-Metadata - Reading FPS tag from .MP4 file
我正在用Python編寫Windows應用程序,該應用程序必須從.MP4視頻文件讀取元數據 。
我開始用Python 3編寫應用程序,但無法找到合適的模塊從視頻文件中讀取元數據。 那是我使用3to2將整個項目移至Python 2的時候,因此我可以安裝Hachoir-Metadata ,在整個網絡上都受到贊譽,使用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
這返回了以下元數據:
這很棒,除了我還真的需要知道每秒的幀數(FPS)的事實。對於.AVI文件,Hachoir-Metadata確實顯示了FPS,如您從此測試輸出中所看到的:
是的,在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視頻的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.