当前位置: 首页 > news >正文

FFmpeg - 如何在Linux上安装支持CUDA的FFmpeg

FFmpeg - 如何在Linux(Ubuntu)上安装支持CUDA的FFmpeg

笔者认为现在的很多“xx教程”只讲干什么不讲为什么,这样即使报错了看官也不知道如何解决。

在安装过程的探索部分会记录我的整个安装过程以及报错和报错的解决办法。

在省流之一步到位的方法部分会省去安装过程中磕磕绊绊的哪些坑,提供一种避坑的方法。

安装过程的探索

确保机器上安装好了NVIDIA驱动和CUDA Toolkit

可以通过nvidia-smi命令检查NVIDIA驱动程序是否已正确安装,通过nvcc --version命令来验证CUDA Toolkit是否安装完成。

安装带有NVIDIA硬件加速支持的FFmpeg

git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg  
cd ffmpeg
./configure --enable-cuda-nvcc --enable-cuda-sdk --enable-libnpp --enable-nvenc --extra-cflags="-I/usr/local/cuda/include" --extra-ldflags="-L/usr/local/cuda/lib64"
make
sudo make install

非自由软件的启用

上一步执行时候会报错:

cuda_nvcc is nonfree and --enable-nonfree is not specified.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.libera.chat.
Include the log file "ffbuild/config.log" produced by configure as this will help
solve the problem.

大概意思是说cuda_nvcc是非自由软件,需要在FFmpeg的配置选项中加入--enable-nonfree

这样生成的二进制文件将会包含非自由代码,可能会限制FFmpeg版本的分发。

./configure --enable-cuda-nvcc --enable-cuda-sdk --enable-libnpp --enable-nvenc --enable-nonfree --extra-cflags="-I/usr/local/cuda/include" --extra-ldflags="-L/usr/local/cuda/lib64"
make
sudo make install

安装ffnvcodec

上一步执行时候会报错:

ERROR: nvenc requested, but not all dependencies are satisfied: ffnvcodecIf 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.libera.chat.
Include the log file "ffbuild/config.log" produced by configure as this will help
solve the problem.

ffnvcodec是FFmpeg对NVIDIA编解码SDK的封装,必须在系统中安装此库才能启用NVENC编码器。

sudo apt-get update
sudo apt-get install libnvidia-encode-<version> ffmpeg
sudo apt-get install nv-codec-headers

如何确定libnvidia-encodeversion呢?需要将<version>替换为NVIDIA驱动版本号。

nvidia-smi | grep "Driver Version"

可以看到| NVIDIA-SMI 470.239.06 Driver Version: 470.239.06 CUDA Version: 11.4 |,即驱动版本Driver Version470.239.06

尝试sudo apt-get install libnvidia-encode-470.239.06 ffmpeg报错找不到libnvidia-encode-470.239.06

尝试不加版本号sudo apt-get install libnvidia-encode得到:

正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
虚拟软件包 libnvidia-encode 由下面的软件包提供:nvidia-340 340.108-0ubuntu5.20.04.2libnvidia-encode-390 390.157-0ubuntu0.20.04.1libnvidia-encode-525-server 525.105.17-0ubuntu0.18.04.1libnvidia-encode-515-server 515.105.01-0ubuntu0.18.04.1libnvidia-encode-470-server 470.182.03-0ubuntu0.18.04.1libnvidia-encode-450-server 450.236.01-0ubuntu0.18.04.1libnvidia-encode-418-server 418.226.00-0ubuntu0.18.04.2libnvidia-encode-550 550.54.15-0ubuntu1libnvidia-encode-535 535.161.08-0ubuntu1libnvidia-encode-470 470.239.06-0ubuntu1libnvidia-encode-545 545.23.08-0ubuntu1libnvidia-encode-525 525.147.05-0ubuntu1libnvidia-encode-450 450.248.02-0ubuntu1libnvidia-encode-515 515.105.01-0ubuntu1libnvidia-encode-510 510.108.03-0ubuntu1libnvidia-encode-520 520.61.05-0ubuntu1libnvidia-encode-495 495.29.05-0ubuntu1libnvidia-encode-465 465.19.01-0ubuntu1libnvidia-encode-460 460.106.00-0ubuntu1libnvidia-encode-455 455.45.01-0ubuntu1
请您明确地选择安装其中一个。E: 软件包 libnvidia-encode 没有可安装候选

其中libnvidia-encode-470对应的470.239.06-0ubuntu1不正是我显卡驱动的版本吗。

因此sudo apt-get install libnvidia-encode-470 ffmpeg解决。

在执行命令sudo apt-get install nv-codec-headers时报错无法定位软件包 nv-codec-headers,因此需要手动下载其源码并编译安装:

git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers
sudo make install

最后一步之编译FFmpeg

准备工作做完了,直接

./configure --enable-cuda-nvcc --enable-cuda-sdk --enable-libnpp --enable-nvenc --enable-nonfree --extra-cflags="-I/usr/local/cuda/include" --extra-ldflags="-L/usr/local/cuda/lib64"
make
sudo make install

等待一会儿,就安装好了。

ffmpeg -hwaccels

可以看到:

Hardware acceleration methods:
vdpau
cuda

说明ffmpeg现在已经支持CUDA加速了。

./configure的时候抛出了警告WARNING: Option --enable-cuda-sdk is deprecated. Use --enable-cuda-nvcc instead.,已经有--enable-cuda-nvcc了,因此我们也可以删掉--enable-cuda-sdk。)

启用libx264

sudo apt-get install libx264-dev

然后在./configure的时候添加两个参数--enable-libx264--enable-gpl--enable-gpl是添加--enable-libx264的时候它让加的)

省流之一步到位的方法

若安装过程遇到任何报错可以尝试在在安装过程的探索中寻找可能的解决方法。

# 安装libnvidia-encode和ffmpeg开发包
sudo apt-get update
sudo apt-get install libnvidia-encode-<version> ffmpeg  # 将<version>替换为你显卡驱动的版本
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers
sudo make install
cd ..
# 编译安装FFmpeg
./configure --enable-cuda-nvcc --enable-libnpp --enable-nvenc --enable-nonfree --extra-cflags="-I/usr/local/cuda/include" --extra-ldflags="-L/usr/local/cuda/lib64"  # 也可以加上--enable-libx264 --enable-gpl以便支持libx264
make
sudo make install

如何加速?加速命令是什么

ffmpeg -hwaccel cuda -i input.mp4 output.mp4

命令中-hwaccel cuda选项告诉FFmpeg使用CUDA进行硬件加速。

ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 -c:v h264_nvenc output.mp4

命令中-c:v h264_nvenc选项指定使用NVIDIA的NVENC进行视频编码,而-hwaccel_output_format cuda选项指定了使用CUDA格式的硬件加速输出。

End

有关FFmpeg的一些常用命令可以查看FFmpeg(强大的音视频处理工具) - 一些基本实用方法。

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/137449955

http://www.lryc.cn/news/336108.html

相关文章:

  • 新火种AI|商汤发布下棋机器人元萝卜,率先深入家庭场景。
  • CSS实现三栏自适应布局(两边固定,中间自适应)
  • MoCo 算法阅读记录
  • 华为OD机试 - 数组连续和 - 滑动窗口(Java 2024 C卷 100分)
  • 微店micro获得微店micro商品详情,API接口封装系列
  • C语言中的数据结构--链表的应用1(2)
  • .Net6 使用Autofac进行依赖注入
  • 第十二届蓝桥杯省赛真题(C/C++大学B组)
  • DC40V降压恒压芯片H4120 40V转5V 3A 40V降压12V 车充降压恒压控制器
  • 2、Qt UI控件 -- qucsdk项目使用
  • MATLAB算法实战应用案例精讲-【人工智能】AIGC概念三部曲(三)
  • 外汇110:外汇交易不同货币类别及交易注意事项!
  • gerrit 拉取失败
  • 大数据行业英语单词巩固20240410
  • 天软特色因子看板 (2024.4 第3期)
  • 使用QT 开发不规则窗体
  • 如何构建企业经营所需的商业智能(BI)能力
  • 【vue】watch监听取不到this指向的数?
  • Ubuntu-22.04安装VMware虚拟机并安装Windows10
  • ELK企业日志分析系统介绍
  • 在C#中读取写入字节流与读取写入二进制数据, 有何差异?
  • 数据库相关知识总结
  • 【汇编语言实战】输出数组中特定元素
  • WordPress LayerSlider插件SQL注入漏洞复现(CVE-2024-2879)
  • MOS管的判别符号记忆与导通条件
  • 数据指标与经营智慧:构建有洞见的经营分析报告
  • Spring 中类似 aBbb 单字母单词序列化与反序列问题
  • TiDB 慢查询日志分析
  • 网页文件批量下载工具有哪些 网页文件批量下载工具推荐 IDM免费激活 网络下载加速器
  • 嵌入式算法开发系列之图像处理算法