This article shows how easy it is to read or write video frames with a few lines of Python, by calling the external software FFMPEG through pipes. If you want a battle-tested and more sophisticated version, check out my module MoviePy. See also this other article for the same with audio files.
Before we start, you must have FFMPEG installed on your computer and you must know the name (or path) of the FFMPEG binary. It should be one of the following:
1 2 |
Reading
To read the frames of the video “myHolidays.mp4” we first ask FFMPEG to open this file and to direct its output to Python:
1 2 3 4 5 6 7 |
In the code above -i myHolidays.mp4
indicates the input file, while rawvideo/rgb24
asks for a raw RGB output. The format image2pipe
and the -
at the end tell FFMPEG that it is being used with a pipe by another program. In sp.Popen
, the bufsize
parameter must be bigger than the size of one frame (see below). It can be omitted most of the time in Python 2 but not in Python 3 where its default value is pretty small.
Now we just have to read the output of FFMPEG. If the video has a size of 420x320 pixels, then the first 420x360x3 bytes outputed by FFMPEG will give the RGB values of the pixels of the first frame, line by line, top to bottom. The next 420x360x3 bytes afer that will represent the second frame, etc. In the next lines we extract one frame and reshape it as a 420x360x3 Numpy array:
1 2 3 4 5 6 7 8 |