codeasone
codeasone

Reputation: 1983

How to extract the bitrate and other statistics of a video file with Python

I am trying to extract the prevailing bitrate of a video file (e.g. .mkv file containing a movie) at a regular sampling interval of between 1-10 seconds under conditions of normal playback. Kind of like you may see in vlc, during playback of the file in the statistics window.

Can anyone suggest the best way to bootstrap the coding of such an analyser? Is there a library that provides an API to such information that people know of? Perhaps a Python wrapper for ffmpeg or equivalent tool that processes video files and can thereby extract such statistics.

What I am really aiming for is a CSV format file containing the seconds offset and the average or actual bitrate in KiB/s at that offset into the asset.

Update:

I built pyffmpeg and wrote the following spike:

import pyffmpeg

reader = pyffmpeg.FFMpegReader(False)
reader.open("/home/mark/Videos/BBB.m2ts", pyffmpeg.TS_VIDEO)
tracks=reader.get_tracks()

# Called for each frame
def obs(f):
  pass

tracks[0].set_observer(obs)
reader.run()

But observing frame information (f) in the callback does not appear to give me any hooks to calculate per second bitrates. In fact bitrate calculations within pyffmpeg are measured across the entire file (filesize / duration) and so the treatment within the library is very superficial. Clearly its focus is on extract i-frames and other frame/GOP specific work.

Upvotes: 0

Views: 5591

Answers (2)

joeforker
joeforker

Reputation: 41747

You should be able to do this with gstreamer. http://pygstdocs.berlios.de/pygst-tutorial/seeking.html has an example of a simple media player. It calls

pos_int = self.player.query_position(gst.FORMAT_TIME, None)[0]

periodically. All you have to do is call query_position() a second time with gst.FORMAT_BYTES, do some simple math, and voila! Bitrate vs. time.

Upvotes: 0

jknair
jknair

Reputation: 4764

Something like these:

http://code.google.com/p/pyffmpeg/

http://pymedia.org/

Upvotes: 1

Related Questions