PyAV简介
说明文档:https://docs.mikeboers.com/pyav/develop/installation.html
PyAV is a Pythonic binding for FFmpeg. We aim to provide all of the power and control of the underlying library, but manage the gritty details as much as possible.
PyAV is for direct and precise access to your media via containers, streams, packets, codecs, and frames. It exposes a few transformations of that data, and helps you get your data to/from other packages (e.g. Numpy and Pillow).
This power does come with some responsibility as working with media is horrendously complicated and PyAV can’t abstract it away or make all the best decisions for you. If the ffmpeg command does the job without you bending over backwards, PyAV is likely going to be more of a hindrance than a help.
But where you can’t work without it, PyAV is a critical tool.
Currently we provide:
libavformat: containers, audio/video/subtitle streams, packets;
libavdevice (by specifying a format to containers);
libavcodec: Codec, CodecContext, audio/video frames, data planes, subtitles;
libavfilter: Filter, Graph;
libswscale: VideoReformatter;
libswresample: AudioResampler;
and a few more utilities.
一、准备工作:
1.1 安装Python
安装最新版本:https://www.python.org/ftp/python/3.7.4/python-3.7.4.exe
1.2 安装git
安装最新版本:https://git-scm.com/download/win
1.3 下载PyAV源码
git clone https://github.com/mikeboers/PyAV --depth 11.4 下载ffmpeg库
开发库:https://ffmpeg.zeranoe.com/builds/win32/dev/ffmpeg-3.2.4-win32-dev.zip
运行库:https://ffmpeg.zeranoe.com/builds/win32/shared/ffmpeg-3.2.4-win32-shared.zip
注意:这里没有下载最新版本的4.1.3。原因是最新的库貌似和PyAV有点不兼容。
二、安装:
2.1 安装PyAV依赖
pip install -r tests/requirements.txt上面的方式我安装了好几次都失败了,貌似有编译错误。打开tests/requirements.txt文件里面是如下内容:
autopep8 Cython editorconfig flake8 isort numpy Pillow使用pip逐个来安装:
pip install autopep8 pip install Cython pip install editorconfig pip install flake8 pip install isort pip install numpy pip install Pillow这次安装成功了。如果碰到失败的情况,请多次尝试执行这句失败的pip行。
2.2 编译PyAV
python setup.py build --ffmpeg-dir=your_ffmpeg_dev_dir注意:your_ffmpeg_dev_dir是下载的ffmpeg-3.2.4-win32-dev.zip解压后的目录,这个目录中的内容如下:
E:\zdwork\pyAvTest\ffmpeg\ffmpeg-3.2.4-win32-dev>tree Folder PATH listing E:. ├───doc │ └───examples ├───include │ ├───libavcodec │ ├───libavdevice │ ├───libavfilter │ ├───libavformat │ ├───libavutil │ ├───libpostproc │ ├───libswresample │ └───libswscale ├───lib └───licenses2.3 安装编译好的PyAV模块
使用指令:python setup.py install
E:\zdwork\pyAvTest\PyAV>python setup.py install running install running bdist_egg running egg_info writing av.egg-info\PKG-INFO writing dependency_links to av.egg-info\dependency_links.txt writing entry points to av.egg-info\entry_points.txt writing top-level names to av.egg-info\top_level.txt reading manifest file 'av.egg-info\SOURCES.txt' reading manifest template 'MANIFEST.in' writing manifest file 'av.egg-info\SOURCES.txt' installing library code to build\bdist.win32\egg running install_lib running build_py running build_ext running config writing build\temp.win32-3.7\Release\include\pyav\config.h running cythonize creating build\bdist.win32\egg creating build\bdist.win32\egg\av creating build\bdist.win32\egg\av\audio copying build\lib.win32-3.7\av\audio\codeccontext.cp37-win32.pyd -> build\bdist.win32\egg\av\audio copying build\lib.win32-3.7\av\audio\fifo.cp37-win32.pyd -> build\bdist.win32\egg\av\audio copying build\lib.win32-3.7\av\audio\format.cp37-win32.pyd -> build\bdist.win32\egg\av\audio copying build\lib.win32-3.7\av\audio\frame.cp37-win32.pyd -> build\bdist.win32\egg\av\audio copying build\lib.win32-3.7\av\audio\layout.cp37-win32.pyd -> build\bdist.win32\egg\av\audio copying build\lib.win32-3.7\av\audio\plane.cp37-win32.pyd -> build\bdist.win32\egg\av\audio copying build\lib.win32-3.7\av\audio\resampler.cp37-win32.pyd -> build\bdist.win32\egg\av\audio copying build\lib.win32-3.7\av\audio\stream.cp37-win32.pyd -> build\bdist.win32\egg\av\audio copying build\lib.win32-3.7\av\audio\__init__.py -> build\bdist.win32\egg\av\audio copying build\lib.win32-3.7\av\buffer.cp37-win32.pyd -> build\bdist.win32\egg\av copying build\lib.win32-3.7\av\bytesource.cp37-win32.pyd -> build\bdist.win32\egg\av creating build\bdist.win32\egg\av\codec copying build\lib.win32-3.7\av\codec\codec.cp37-win32.pyd -> build\bdist.win32\egg\av\codec copying build\lib.win32-3.7\av\codec\context.cp37-win32.pyd -> build\bdist.win32\egg\av\codec copying build\lib.win32-3.7\av\codec\__init__.py -> build\bdist.win32\egg\av\codec creating build\bdist.win32\egg\av\container copying build\lib.win32-3.7\av\container\core.cp37-win32.pyd -> build\bdist.win32\egg\av\container copying build\lib.win32-3.7\av\container\input.cp37-win32.pyd -> build\bdist.win32\egg\av\container copying build\lib.win32-3.7\av\container\output.cp37-win32.pyd -> build\bdist.win32\egg\av\container copying build\lib.win32-3.7\av\container\pyio.cp37-win32.pyd -> build\bdist.win32\egg\av\container copying build\lib.win32-3.7\av\container\streams.cp37-win32.pyd -> build\bdist.win32\egg\av\container copying build\lib.win32-3.7\av\container\__init__.py -> build\bdist.win32\egg\av\container creating build\bdist.win32\egg\av\data copying build\lib.win32-3.7\av\data\stream.cp37-win32.pyd -> build\bdist.win32\egg\av\data copying build\lib.win32-3.7\av\data\__init__.py -> build\bdist.win32\egg\av\data copying build\lib.win32-3.7\av\datasets.py -> build\bdist.win32\egg\av copying build\lib.win32-3.7\av\deprecation.py -> build\bdist.win32\egg\av copying build\lib.win32-3.7\av\descriptor.cp37-win32.pyd -> build\bdist.win32\egg\av copying build\lib.win32-3.7\av\dictionary.cp37-win32.pyd -> build\bdist.win32\egg\av copying build\lib.win32-3.7\av\enums.cp37-win32.pyd -> build\bdist.win32\egg\av creating build\bdist.win32\egg\av\filter copying build\lib.win32-3.7\av\filter\context.cp37-win32.pyd -> build\bdist.win32\egg\av\filter copying build\lib.win32-3.7\av\filter\filter.cp37-win32.pyd -> build\bdist.win32\egg\av\filter copying build\lib.win32-3.7\av\filter\graph.cp37-win32.pyd -> build\bdist.win32\egg\av\filter copying build\lib.win32-3.7\av\filter\link.cp37-win32.pyd -> build\bdist.win32\egg\av\filter copying build\lib.win32-3.7\av\filter\pad.cp37-win32.pyd -> build\bdist.win32\egg\av\filter copying build\lib.win32-3.7\av\filter\__init__.py -> build\bdist.win32\egg\av\filter copying build\lib.win32-3.7\av\format.cp37-win32.pyd -> build\bdist.win32\egg\av copying build\lib.win32-3.7\av\frame.cp37-win32.pyd -> build\bdist.win32\egg\av copying build\lib.win32-3.7\av\logging.cp37-win32.pyd -> build\bdist.win32\egg\av copying build\lib.win32-3.7\av\option.cp37-win32.pyd -> build\bdist.win32\egg\av copying build\lib.win32-3.7\av\packet.cp37-win32.pyd -> build\bdist.win32\egg\av copying build\lib.win32-3.7\av\plane.cp37-win32.pyd -> build\bdist.win32\egg\av copying build\lib.win32-3.7\av\stream.cp37-win32.pyd -> build\bdist.win32\egg\av creating build\bdist.win32\egg\av\subtitles copying build\lib.win32-3.7\av\subtitles\codeccontext.cp37-win32.pyd -> build\bdist.win32\egg\av\subtitles copying build\lib.win32-3.7\av\subtitles\stream.cp37-win32.pyd -> build\bdist.win32\egg\av\subtitles copying build\lib.win32-3.7\av\subtitles\subtitle.cp37-win32.pyd -> build\bdist.win32\egg\av\subtitles copying build\lib.win32-3.7\av\subtitles\__init__.py -> build\bdist.win32\egg\av\subtitles copying build\lib.win32-3.7\av\utils.cp37-win32.pyd -> build\bdist.win32\egg\av creating build\bdist.win32\egg\av\video copying build\lib.win32-3.7\av\video\codeccontext.cp37-win32.pyd -> build\bdist.win32\egg\av\video copying build\lib.win32-3.7\av\video\format.cp37-win32.pyd -> build\bdist.win32\egg\av\video copying build\lib.win32-3.7\av\video\frame.cp37-win32.pyd -> build\bdist.win32\egg\av\video copying build\lib.win32-3.7\av\video\plane.cp37-win32.pyd -> build\bdist.win32\egg\av\video copying build\lib.win32-3.7\av\video\reformatter.cp37-win32.pyd -> build\bdist.win32\egg\av\video copying build\lib.win32-3.7\av\video\stream.cp37-win32.pyd -> build\bdist.win32\egg\av\video copying build\lib.win32-3.7\av\video\__init__.py -> build\bdist.win32\egg\av\video copying build\lib.win32-3.7\av\_core.cp37-win32.pyd -> build\bdist.win32\egg\av copying build\lib.win32-3.7\av\__init__.py -> build\bdist.win32\egg\av copying build\lib.win32-3.7\av\__main__.py -> build\bdist.win32\egg\av byte-compiling build\bdist.win32\egg\av\audio\__init__.py to __init__.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\codec\__init__.py to __init__.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\container\__init__.py to __init__.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\data\__init__.py to __init__.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\datasets.py to datasets.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\deprecation.py to deprecation.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\filter\__init__.py to __init__.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\subtitles\__init__.py to __init__.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\video\__init__.py to __init__.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\__init__.py to __init__.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\__main__.py to __main__.cpython-37.pyc creating stub loader for av\buffer.cp37-win32.pyd creating stub loader for av\bytesource.cp37-win32.pyd creating stub loader for av\descriptor.cp37-win32.pyd creating stub loader for av\dictionary.cp37-win32.pyd creating stub loader for av\enums.cp37-win32.pyd creating stub loader for av\format.cp37-win32.pyd creating stub loader for av\frame.cp37-win32.pyd creating stub loader for av\logging.cp37-win32.pyd creating stub loader for av\option.cp37-win32.pyd creating stub loader for av\packet.cp37-win32.pyd creating stub loader for av\plane.cp37-win32.pyd creating stub loader for av\stream.cp37-win32.pyd creating stub loader for av\utils.cp37-win32.pyd creating stub loader for av\_core.cp37-win32.pyd creating stub loader for av\audio\codeccontext.cp37-win32.pyd creating stub loader for av\audio\fifo.cp37-win32.pyd creating stub loader for av\audio\format.cp37-win32.pyd creating stub loader for av\audio\frame.cp37-win32.pyd creating stub loader for av\audio\layout.cp37-win32.pyd creating stub loader for av\audio\plane.cp37-win32.pyd creating stub loader for av\audio\resampler.cp37-win32.pyd creating stub loader for av\audio\stream.cp37-win32.pyd creating stub loader for av\codec\codec.cp37-win32.pyd creating stub loader for av\codec\context.cp37-win32.pyd creating stub loader for av\container\core.cp37-win32.pyd creating stub loader for av\container\input.cp37-win32.pyd creating stub loader for av\container\output.cp37-win32.pyd creating stub loader for av\container\pyio.cp37-win32.pyd creating stub loader for av\container\streams.cp37-win32.pyd creating stub loader for av\data\stream.cp37-win32.pyd creating stub loader for av\filter\context.cp37-win32.pyd creating stub loader for av\filter\filter.cp37-win32.pyd creating stub loader for av\filter\graph.cp37-win32.pyd creating stub loader for av\filter\link.cp37-win32.pyd creating stub loader for av\filter\pad.cp37-win32.pyd creating stub loader for av\subtitles\codeccontext.cp37-win32.pyd creating stub loader for av\subtitles\stream.cp37-win32.pyd creating stub loader for av\subtitles\subtitle.cp37-win32.pyd creating stub loader for av\video\codeccontext.cp37-win32.pyd creating stub loader for av\video\format.cp37-win32.pyd creating stub loader for av\video\frame.cp37-win32.pyd creating stub loader for av\video\plane.cp37-win32.pyd creating stub loader for av\video\reformatter.cp37-win32.pyd creating stub loader for av\video\stream.cp37-win32.pyd byte-compiling build\bdist.win32\egg\av\buffer.py to buffer.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\bytesource.py to bytesource.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\descriptor.py to descriptor.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\dictionary.py to dictionary.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\enums.py to enums.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\format.py to format.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\frame.py to frame.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\logging.py to logging.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\option.py to option.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\packet.py to packet.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\plane.py to plane.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\stream.py to stream.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\utils.py to utils.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\_core.py to _core.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\audio\codeccontext.py to codeccontext.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\audio\fifo.py to fifo.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\audio\format.py to format.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\audio\frame.py to frame.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\audio\layout.py to layout.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\audio\plane.py to plane.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\audio\resampler.py to resampler.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\audio\stream.py to stream.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\codec\codec.py to codec.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\codec\context.py to context.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\container\core.py to core.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\container\input.py to input.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\container\output.py to output.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\container\pyio.py to pyio.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\container\streams.py to streams.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\data\stream.py to stream.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\filter\context.py to context.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\filter\filter.py to filter.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\filter\graph.py to graph.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\filter\link.py to link.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\filter\pad.py to pad.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\subtitles\codeccontext.py to codeccontext.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\subtitles\stream.py to stream.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\subtitles\subtitle.py to subtitle.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\video\codeccontext.py to codeccontext.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\video\format.py to format.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\video\frame.py to frame.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\video\plane.py to plane.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\video\reformatter.py to reformatter.cpython-37.pyc byte-compiling build\bdist.win32\egg\av\video\stream.py to stream.cpython-37.pyc creating build\bdist.win32\egg\EGG-INFO copying av.egg-info\PKG-INFO -> build\bdist.win32\egg\EGG-INFO copying av.egg-info\SOURCES.txt -> build\bdist.win32\egg\EGG-INFO copying av.egg-info\dependency_links.txt -> build\bdist.win32\egg\EGG-INFO copying av.egg-info\entry_points.txt -> build\bdist.win32\egg\EGG-INFO copying av.egg-info\not-zip-safe -> build\bdist.win32\egg\EGG-INFO copying av.egg-info\top_level.txt -> build\bdist.win32\egg\EGG-INFO writing build\bdist.win32\egg\EGG-INFO\native_libs.txt creating 'dist\av-6.2.1.dev0-py3.7-win32.egg' and adding 'build\bdist.win32\egg' to it removing 'build\bdist.win32\egg' (and everything under it) Processing av-6.2.1.dev0-py3.7-win32.egg removing 'c:\program files (x86)\python37-32\lib\site-packages\av-6.2.1.dev0-py3.7-win32.egg' (and everything under it) creating c:\program files (x86)\python37-32\lib\site-packages\av-6.2.1.dev0-py3.7-win32.egg Extracting av-6.2.1.dev0-py3.7-win32.egg to c:\program files (x86)\python37-32\lib\site-packages av 6.2.1.dev0 is already the active version in easy-install.pth Installing pyav-script.py script to C:\Program Files (x86)\Python37-32\Scripts Installing pyav.exe script to C:\Program Files (x86)\Python37-32\Scripts Installing pyav.exe.manifest script to C:\Program Files (x86)\Python37-32\Scripts Installed c:\program files (x86)\python37-32\lib\site-packages\av-6.2.1.dev0-py3.7-win32.egg Processing dependencies for av==6.2.1.dev0 Finished processing dependencies for av==6.2.1.dev02.4 配置PyAV
将前面下载的ffmpeg-3.2.4-win32-shared.zip文件解压。把解压出来的文件夹下的bin目录添加到PATH环境变量,否则在import av的时候回出现以下错误:
no module named av.__core__原因是PyAV找不到ffmpeg的运行库(Dll动态链接库)
2.5 测试PyAV
E:\zdwork\pyAvTest>python Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import av >>>如果可以看见import av行后面的>>>引导付,则说明我们的PyAV安装成功了。
注意:不要在我们下载的PyAV的源码目录下启动Python解释器进行av模块的导入,会出现如下错误:
E:\zdwork\pyAvTest\PyAV>python Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import av Traceback (most recent call last): File "<stdin>", line 1, in <module> File "E:\zdwork\pyAvTest\PyAV\av\__init__.py", line 9, in <module> from av._core import time_base, pyav_version as __version__ ModuleNotFoundError: No module named 'av._core'