0

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?

Winson
  • 35
  • 2
  • 11
  • Did you try VLC bindings for python? – zenpoy Jul 23 '12 at 15:16
  • Thanks @zenpoy, i'm currently looking into VLC binding. If i have any problem i will get back to you again =3. Appreciate your effort in answering my question. – Winson Jul 23 '12 at 16:13
  • hello @zenpoy, i have problem getting VLC binding to work in my python which is in version 2.7. Do you have any related website that have an instruction to install it? – Winson Jul 23 '12 at 17:14
  • see also [Python native library to read metadata from videos?](http://stackoverflow.com/q/10075176/309483) – Janus Troelsen Feb 23 '13 at 18:23

2 Answers2

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)
Kinch
  • 26
  • 1
  • Thanks @kinch for answering to my post however i have already explored opencv and it seems to be too big a library for my mini project. It seems that the library for opencv is an astounding 2.4gb which is simply too big for my project. In addition, i'm unable to use ffmpeg because the project requirement clearly stated not to use it ><. Either way, i do agree both of the libraries are being well adopted and are quite useful. – Winson Jul 23 '12 at 16:17
  • Oh ya, @kinch, the reason why we don't simply use framecount/FPS is because in video there are two type, CBR(constant bit rate) & VBR(variable bit rate). In this case if we use famecount/FPS, it will be only applicable for CBR however not VBR. No harm in sharing knowledge =3 – Winson Jul 23 '12 at 16:22
  • Thanks @kinch, i was given the green light to use FFmpeg and it works miracle. =3 appreciate your help. – Winson Jul 28 '12 at 14:15
1

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.

Kinch
  • 11
  • 1