mpeg-2 library to extract video duration

﹥>﹥吖頭↗ 提交于 2019-12-17 17:23:22

问题


I have recently ventured out into dealing with audio and video related coding and i have limited knowledge about neither one of them.

It happens that i have a project that is dealing with mpeg-2 video now. Is there any python library that available out there to extract the duration of the mpeg-2 video itself?


回答1:


I don't know any pure python implementations. But maybe the opencv bindings works for you:

import cv
cvcapture = cv.CaptureFromFile("movie.mpg")
cv.GetCaptureProperty(cvcapture,cv.CV_CAP_PROP_FRAME_COUNT)

Otherwise, maye you can use pyffmpeg. Beware: It is my experience that the frame count is often not very accurate. Opencv 2.6 works very well, but ealier versions gives sometimes just garbage.

Edit: Ah, sorry my mistake: This gives you the frame count only. For the duration: Multiply this with the frame rate:

cv.GetCaptureProperty(cvcapture,cv.CV_CAP_PROP_FPS)



回答2:


another option besides opencv or ffmpeg is maybe gstreamer:

import gst
from gst.pbutils import Discoverer

d = Discoverer(5000000000)
vid_info = d.discover_uri("file://<path>") # needs to be a full path
duration = vid_info.get_duration()

# convert to seconds
duration / gst.SECOND

However, this solution crashs randomly (every 100 file or so) with the current gstreamer version (on Ubuntu 12.04). And maybe you need some gstreamer plugin for working with mpg-2; I currently don't know which ones.

I am also working on a more or less large media project, which should be able to handle a large range of codecs and containers and every library we are using is more or less a mess (wrong values or instable). Maybe there is so some specialized mpg library out there.

Oh and VLC is using ffmpeg; so if you are not allowed to use ffmpeg you maybe are also not allowed to use VLC too.



来源:https://stackoverflow.com/questions/11615384/mpeg-2-library-to-extract-video-duration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!