macOS Sierra (10.12.4)下使用Python操作视频,FFMPEG是目前来说最好的一个选择,但是没有为Python专门提供适配接口,网上搜索了比较长时间,才找到PyAV来操作FFMPEG。
PyAV的文档地址在:https://mikeboers.github.io/PyAV/
代码地址在:https://github.com/mikeboers/PyAV
首先需要通过HomeBrew安装FFMPEG:
|
1 |
$ brew install ffmpeg |
接下来安装PyAV,安装方式两种:
一种是直接通过PIP来安装:
|
1 |
$ pip install av |
另外一种是通过下载代码来手工安装
|
1 2 3 |
$ git clone https://github.com/mikeboers/PyAV.git $ cd PyAV $ python setup.py install |
安装好后的例子如下:
|
1 2 3 4 5 6 7 8 9 10 11 |
import av from av.frame import Frame from av.packet import Packet from av.stream import Stream from av.utils import AVError from av.video import VideoFrame container = av.open('san.mp4') for frame in container.decode(video=0): frame.to_image().save('./pyav/frame-%04d.jpg' % frame.index) |