一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

Read and Write Video Frames in Python Using FFMPEG

 昵稱(chēng)597197 2016-01-21

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
FFMPEG_BIN = "ffmpeg" # on Linux ans Mac OS
FFMPEG_BIN = "ffmpeg.exe" # on Windows

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
import subprocess as sp
command = [ FFMPEG_BIN,
            '-i', 'myHolidays.mp4',
            '-f', 'image2pipe',
            '-pix_fmt', 'rgb24',
            '-vcodec', 'rawvideo', '-']
pipe = sp.Popen(command, stdout = sp.PIPE, bufsize=10**8)

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
import numpy
# read 420*360*3 bytes (= 1 frame)
raw_image = pipe

感谢您访问我们的网站,您可能还对以下资源感兴趣:

一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区 亚洲国产精品av在线观看 | av中文字幕一区二区三区在线| 国产欧美亚洲精品自拍| 高清不卡一卡二卡区在线| 国产精品内射婷婷一级二级| 久草精品视频精品视频精品| 亚洲伦理中文字幕在线观看| 日韩av亚洲一区二区三区| 日本加勒比系列在线播放| 在线视频三区日本精品| 亚洲男人的天堂久久a| 日韩中文高清在线专区| 视频一区中文字幕日韩| 日韩免费国产91在线| 亚洲精品福利视频你懂的| 国产精品丝袜美腿一区二区| 五月综合激情婷婷丁香| 成年男女午夜久久久精品| 日韩一区二区三区免费av| 欧美激情床戏一区二区三| 国产亚洲欧美日韩精品一区| 日韩精品成区中文字幕| 欧美整片精品日韩综合| 99久久免费看国产精品| 亚洲女同一区二区另类| 色婷婷成人精品综合一区| 91日韩在线视频观看| 日韩成人动作片在线观看| 国产一区欧美午夜福利| 亚洲一区二区三区日韩91| 国产色一区二区三区精品视频| 久久精品国产一区久久久| 欧美精品在线观看国产| 大香蕉再在线大香蕉再在线| 欧美精品在线观看国产| 成人日韩视频中文字幕| 亚洲黄色在线观看免费高清| 欧美一级黄片免费视频| 亚洲日本久久国产精品久久| 久久re6热在线视频| 美女被后入福利在线观看|