[Tutor] Reading a video

Steven D'Aprano steve at pearwood.info
Fri Aug 16 05:53:07 CEST 2013


On 15/08/13 23:54, Zoya Tavakkoli wrote:
> Hi
> How can read a video frame by frame in python?

Of course! Videos are just binary files, and Python can read binary files. The hard part is writing code that understands the internal structure of a video. There are many video formats (ogv, mp4, mov, avi, flv, mpg, asf, wmv, ram, to name just a few), and the internal structure of them will be different and not always documented anywhere, which may mean you have to reverse-engineer the format.

Doing this in pure Python code will likely be slow, unless you use an optimizing compiler like PyPy.

Your best best is to look for a library that already understands video formats. If there is no pre-existing Python library, there may be a C library with a Python interface. Of you can create your own Python interface, maybe using ctypes to talk to the library.

But honestly, it's probably a huge amount of work for something that will likely be rather slow, unless you use PyPy, in which case it will just be a huge amount of work. It will be less work if you only care about one video format.

What are you hoping to do with the frames once you get them? You may be better off using an external application like mplayer or ffmpeg to extract the frames to (say) png files, and then manipulate the images from Python, perhaps using PIL or similar.


-- 
Steven


More information about the Tutor mailing list