引言
安装
参考官方给的安装教程,以Linux系统为例,Windows系统请直接查看官网教程。
预先安装依赖
(1)安装Nvidia video driver和CUDA SDK
①安装与GPU匹配的CUDA和英伟达显卡驱动,需要注意版本对应。
②下载NVIDIA Video Codec SDK并解压备用,官网下载需要注册,可以直接从CSDN下载。
(2)从源码安装FFMpeg
推荐从源码安装FFMpeg:
cd ~/installs
git clone https://github.com/FFmpeg/FFmpeg.git
cd FFmpeg
mkdir -p $(pwd)/build_x64_release_shared
./configure \
--prefix=$(pwd)/build_x64_release_shared \
--disable-static \
--disable-stripping \
--disable-doc \
--enable-shared
注:如果没有预先安装yasm可能会报错如下:
nasm/yasm not found or too old. Use --disable-x86asm for a crippled
build.If you think configure made a mistake, make sure you are using the
latest version from Git. If the latest version fails, report the
problem to the ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on
irc.freenode.net. Include the log file “ffbuild/config.log” produced
by configure as this will help
此时需要先安装yasm:
sudo apt install -y yasm
再编译安装ffmpeg:
make -j -s && make install
安装VPF
# Export paths to Video Codec SDK and FFMpeg
export PATH_TO_SDK=~/installs/Video_Codec_SDK_9.1.23
export PATH_TO_FFMPEG=~/installs/FFmpeg/build_x64_release_shared
# Clone repo and start building process
cd ~/installs
git clone https://github.com/NVIDIA/VideoProcessingFramework.git
# Export path to CUDA compiler (you may need this sometimes if you install drivers from Nvidia site):
export CUDACXX=/usr/local/cuda/bin/nvcc
# Now the build itself
cd VideoProcessingFramework
export INSTALL_PREFIX=$(pwd)/install
mkdir -p install
mkdir -p build
cd build
# If you want to generate Pytorch extension, set up corresponding CMake value GENERATE_PYTORCH_EXTENSION
cmake .. \
-DFFMPEG_DIR:PATH="$PATH_TO_FFMPEG" \
-DVIDEO_CODEC_SDK_DIR:PATH="$PATH_TO_SDK" \
-DGENERATE_PYTHON_BINDINGS:BOOL="1" \
-DGENERATE_PYTORCH_EXTENSION:BOOL="1" \
-DPYTHON_LIBRARY=/home/user/anaconda3/lib/libpython3.7m.so \
-DCMAKE_INSTALL_PREFIX:PATH="$INSTALL_PREFIX"
需要注意如果存在多个Python环境,要额外添加Python相关的配置,不能完全按照官网教程的cmake设置来,否则可能存在自动Python环境查找不是自己想要的情况。最终无法正常使用。
此处额外添加DPYTHON_EXECUTABLE
和DPYTHON_INCLUDE_DIR
两项设置。
由于不需要PyTorch扩展(PYTORCH_EXTENSION),所以将DGENERATE_PYTORCH_EXTENSION:BOOL
设为0。
cmake .. -DFFMPEG_DIR:PATH="$PATH_TO_FFMPEG" \
-DVIDEO_CODEC_SDK_DIR:PATH="$PATH_TO_SDK" \
-DGENERATE_PYTHON_BINDINGS:BOOL="1" \
-DGENERATE_PYTORCH_EXTENSION:BOOL="0" \
-DPYTHON_LIBRARY=/root/user/env_py38/lib/libpython3.8.so \
-DCMAKE_INSTALL_PREFIX:PATH="$INSTALL_PREFIX" \
-DPYTHON_EXECUTABLE=/root/user/env_py38/bin/python \
-DPYTHON_INCLUDE_DIR=/root/user/env_py38/include/python3.8/
编译安装:
make && make install
编译安装过程打印信息如下:
[ 87%] Linking CXX shared library libTC.so
[ 87%] Built target TC
Scanning dependencies of target PyNvCodec
[ 93%] Building CXX object PyNvCodec/CMakeFiles/PyNvCodec.dir/src/PyNvCodec.cpp.o
[100%] Linking CXX shared library PyNvCodec.cpython-38-x86_64-linux-gnu.so
[100%] Built target PyNvCodec
[ 18%] Built target TC_CORE
[ 87%] Built target TC
[100%] Built target PyNvCodec
Install the project...
-- Install configuration: ""
-- Installing: /root/user/installs/VideoProcessingFramework/install/bin/libTC_CORE.so
-- Installing: /root/user/installs/VideoProcessingFramework/install/bin/libTC.so
-- Installing: /root/user/installs/VideoProcessingFramework/install/bin/PyNvCodec.cpython-38-x86_64-linux-gnu.so
-- Installing: /root/user/installs/VideoProcessingFramework/install/bin/SampleDecode.py
-- Installing: /root/user/installs/VideoProcessingFramework/install/bin/SampleEncode.py
-- Installing: /root/user/installs/VideoProcessingFramework/install/bin/SampleDecodeSw.py
-- Installing: /root/user/installs/VideoProcessingFramework/install/bin/SampleDecodeMultiThread.py
-- Installing: /root/user/installs/VideoProcessingFramework/install/bin/SampleEncodeMultiThread.py
检查CUDA和Video Codec SDK libraries是否被正常加载:
ldd PyNvCodec.cpython-38-x86_64-linux-gnu.so
验证是否安装成功:
cd ~/installs/VideoProcessingFramework/install/bin
python SampleDecode.py
如果打印以下信息则表示安装成功:
This sample decodes input video to raw NV12 file on given GPU. Usage:
SampleDecode.py $gpu_id $input_file $output_file. Provide gpu ID, path
to input and output files
如果打印以下信息则表示安装失败,请注意环境配置一定要正确:
Traceback (most recent call last): File “SampleDecode.py”, line
16, in
import PyNvCodec as nvc ModuleNotFoundError: No module named ‘PyNvCodec’
使用
如果需要在其他工程中使用VPF,则拷贝编译好的PyNvCodec.cpython-38-x86_64-linux-gnu.so
文件到工程主目录下,或者在工程代码中使用sys.path.append('/root/user/installs/VideoProcessingFramework/install/bin')
来添加,还可以将生成的.so文件拷贝到使用的Python包路径(例如cp PyNvCodec.cpython-38-x86_64-linux-gnu.so /root/conda/envs/env_name/lib/python3.8/site-packages/
)。
导入VPF:
import PyNvCodec as nvc
其他
可能遇到的报错及解决方案:
(1)cuda链接库文件找不到
/usr/bin/ld: cannot find -lcuda
collect2: error: ld returned 1 exit status
make[2]: *** [PyNvCodec/TC/CMakeFiles/TC.dir/build.make:226: PyNvCodec/TC/libTC.so] Error 1
make[1]: *** [CMakeFiles/Makefile2:329: PyNvCodec/TC/CMakeFiles/TC.dir/all] Error 2
make: *** [Makefile:130: all] Error 2
需要重新创建软链接:
ls -la /usr/lib/x86_64-linux-gnu/libcuda.so
ls: cannot access '/usr/lib/x86_64-linux-gnu/libcuda.so': No such file or directory
sudo ln -s /usr/lib/x86_64-linux-gnu/libcuda.so.1 /usr/lib/x86_64-linux-gnu/libcuda.so
参考资料
[1] NVIDIA-VideoProcessingFramework-wiki-Building-from-source
[2] Github: NVIDIA-VideoProcessingFramework
[3] NVIDIA Video Codec SDK
[4] Ubuntu上安装ffmpeg-cuda加速
[5] Video_Codec_SDK_9.1.23.zip
[6] Nvidia 视频处理框架----1. VideoProcessingFramework 介绍和安装
[7] VideoProcessingFramework框架编译启动全过程
[8] VideoProcessingFramework编译安装使用终极教程(包含gpu-ffmpeg,nv-codec-headers,Video_Codec_SDK等安装教程 )
[9] VPF:适用于 Python 的开源视频处理框架,加速视频任务、提高 GPU 利用率
[10] VPF: Hardware-Accelerated Video Processing Framework in Python
[11] ValueError: No AVFormatContext provided. #8
[12] Optimizing Video Memory Usage with the NVDECODE API and NVIDIA Video Codec SDK
[13] Introducing NVIDIA Video Codec SDK 10 Presets