Ubuntu上安装NVIDIA VideoProcessingFramework (VPF)

引言

在这里插入图片描述

安装

参考官方给的安装教程,以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_EXECUTABLEDPYTHON_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

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TracelessLe

❀点个赞加个关注再走吧❀

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值