kre*_*gus 5 python video mp4 metadata hachoir-parser
我正在用Python编写一个Windows应用程序,它必须从.MP4视频文件中读取元数据.
我开始在Python 3中编写应用程序,但无法找到合适的模块来读取视频文件中的元数据.这时候,我使用3to2整个项目移至Python 2中,这样我就可以安装Hachoir的元数据,这是所有称赞过网,使用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
Run Code Online (Sandbox Code Playgroud)
这返回了以下元数据:
这很棒,除了我真的需要知道每秒帧数(FPS).对于.AVI文件,Hachoir-Metadata确实显示了FPS,正如您可以从此测试输出中看到的:
是的,在FPS标签被设置在.MP4文件(100FPS).
有没有办法从.MP4文件中提取FPS?优选地,还包括宽度(px),高度(px),持续时间和创建时间.
在此先感谢您的帮助!
好的,我成功提取了我需要的所有数据以及更多!Stack Overflow 上的这个答案给了我尝试 MediaInfo 提取元数据的想法。
为此我再次切换回 Python 3。我还必须将第 22 行更改MediaInfoDLL3.py为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
Run Code Online (Sandbox Code Playgroud)
这返回了:
希望这对某人有帮助!
| 归档时间: |
|
| 查看次数: |
6209 次 |
| 最近记录: |