英文:
Building FFmpeg with NVIDIA GPU Hardware Acceleration in docker image, cannot load libnvcuvid.so.1 and libnvidia-encode.so.1
问题
我正在尝试按照这些说明使用NVIDIA GPU硬件加速构建FFmpeg:https://docs.nvidia.com/video-technologies/video-codec-sdk/ffmpeg-with-nvidia-gpu/index.html#compiling-for-linux。我正在使用的Docker镜像是nvidia/cuda:12.0.1-devel-ubuntu20.04
运行测试命令ffmpeg -y -vsync 0 -hwaccel cuda -hwaccel_output_format cuda -i bbb.mp4 -c:a copy -c:v h264_nvenc -b:v 5M output.mp4
,我得到以下输出:
ffmpeg版本 N-109965-ge50a02b0f6 版权所有 (c) 2000-2023年FFmpeg开发人员
使用gcc 9构建(Ubuntu 9.4.0-1ubuntu1~20.04.1)
配置:--enable-nonfree --enable-cuda-nvcc --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64 --disable-static --enable-shared
libavutil 58. 3.100 / 58. 3.100
libavcodec 60. 6.100 / 60. 6.100
libavformat 60. 4.100 / 60. 4.100
libavdevice 60. 2.100 / 60. 2.100
libavfilter 9. 4.100 / 9. 4.100
libswscale 7. 2.100 / 7. 2.100
libswresample 4. 11.100 / 4. 11.100
-vsync已被弃用。使用-fps_mode
将数字传递给-vsync已被弃用,使用手册中描述的字符串参数。
输入#0,mov,mp4,m4a,3gp,3g2,mj2,来自'bbb.mp4':
元数据:
major_brand : isom
minor_version : 1
compatible_brands: isomavc1
creation_time : 2013-12-16T17:44:39.000000Z
title : Big Buck Bunny, Sunflower version
artist : Blender Foundation 2008, Janus Bager Kristensen 2013
comment : Creative Commons Attribution 3.0 - http://bbb3d.renderfarming.net
genre : Animation
composer : Sacha Goedegebure
时长:00:10:34.60,开始:0.000000,比特率:3481 kb/s
流#0:0[0x1](und):视频:h264(High)(avc1 / 0x31637661),yuv420p(渐进式),1920x1080 [SAR 1:1 DAR 16:9],2998 kb/s,30 fps,30 tbr,30k tbn(默认)
元数据:
creation_time : 2013-12-16T17:44:39.000000Z
handler_name : GPAC ISO Video Handler
vendor_id : [0][0][0][0]
流#0:1[0x2](und):音频:mp3(mp4a / 0x6134706D),48000 Hz,立体声,fltp,160 kb/s(默认)
元数据:
creation_time : 2013-12-16T17:44:42.000000Z
handler_name : GPAC ISO Audio Handler
vendor_id : [0][0][0][0]
流#0:2[0x3](und):音频:ac3(ac-3 / 0x332D6361),48000 Hz,5.1(side),fltp,320 kb/s(默认)
元数据:
creation_time : 2013-12-16T17:44:42.000000Z
handler_name : GPAC ISO Audio Handler
vendor_id : [0][0][0][0]
附加数据:
音频服务类型:主要
流映射:
流#0:0 -> #0:0(h264(本机) -> h264(h264_nvenc))
流#0:2 -> #0:1(复制)
按[q]停止,[?]以获取帮助
[h264 @ 0x55bd878c2d80] 无法加载libnvcuvid.so.1
[h264 @ 0x55bd878c2d80] 加载nvcuvid失败。
[h264 @ 0x55bd878c2d80] cuda格式的hwaccel初始化返回错误。
[h264_nvenc @ 0x55bd86f5e680] 无法加载libnvidia-encode.so.1
[h264_nvenc @ 0x55bd86f5e680] nvenc所需的最低Nvidia驱动程序为520.56.06或更高
[vost#0:0/h264_nvenc @ 0x55bd86f5e1c0] 初始化输出流时发生错误:打开输出流#0:0的编码器时出现错误,可能是比特率、速率、宽度或高度等参数不正确
转换失败!
nvidia-smi
的输出:
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.85.05 驱动程序版本: 525.85.05 CUDA版本: 12.0 |
|-------------------------------+----------------------+----------------------+
| GPU 名称 持续性-M | 总线-ID Disp.A | 不稳定的ECC错误 |
| 风扇 温度 性能 功耗:使用/Cap | 内存使用 | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 NVIDIA GeForce ... 关闭 | 00000000:01:00.0 开启 | N/A |
| 27% 43摄氏度 P8 12瓦 / 250瓦 | 500兆字节 / 11264兆字节 | 1% 默认 |
|
<details>
<summary>英文:</summary>
I'm trying to build FFmpeg with NVIDIA GPU Hardware Acceleration following these instructions: https://docs.nvidia.com/video-technologies/video-codec-sdk/ffmpeg-with-nvidia-gpu/index.html#compiling-for-linux. The Docker image I'm using is `nvidia/cuda:12.0.1-devel-ubuntu20.04`
Running the test command `ffmpeg -y -vsync 0 -hwaccel cuda -hwaccel_output_format cuda -i bbb.mp4 -c:a copy -c:v h264_nvenc -b:v 5M output.mp4`, I get the following output:
ffmpeg version N-109965-ge50a02b0f6 Copyright (c) 2000-2023 the FFmpeg developers
built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
configuration: --enable-nonfree --enable-cuda-nvcc --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64 --disable-static --enable-shared
libavutil 58. 3.100 / 58. 3.100
libavcodec 60. 6.100 / 60. 6.100
libavformat 60. 4.100 / 60. 4.100
libavdevice 60. 2.100 / 60. 2.100
libavfilter 9. 4.100 / 9. 4.100
libswscale 7. 2.100 / 7. 2.100
libswresample 4. 11.100 / 4. 11.100
-vsync is deprecated. Use -fps_mode
Passing a number to -vsync is deprecated, use a string argument as described in the manual.
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'bbb.mp4':
Metadata:
major_brand : isom
minor_version : 1
compatible_brands: isomavc1
creation_time : 2013-12-16T17:44:39.000000Z
title : Big Buck Bunny, Sunflower version
artist : Blender Foundation 2008, Janus Bager Kristensen 2013
comment : Creative Commons Attribution 3.0 - http://bbb3d.renderfarming.net
genre : Animation
composer : Sacha Goedegebure
Duration: 00:10:34.60, start: 0.000000, bitrate: 3481 kb/s
Stream #0:00x1: Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], 2998 kb/s, 30 fps, 30 tbr, 30k tbn (default)
Metadata:
creation_time : 2013-12-16T17:44:39.000000Z
handler_name : GPAC ISO Video Handler
vendor_id : [0][0][0][0]
Stream #0:10x2: Audio: mp3 (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 160 kb/s (default)
Metadata:
creation_time : 2013-12-16T17:44:42.000000Z
handler_name : GPAC ISO Audio Handler
vendor_id : [0][0][0][0]
Stream #0:20x3: Audio: ac3 (ac-3 / 0x332D6361), 48000 Hz, 5.1(side), fltp, 320 kb/s (default)
Metadata:
creation_time : 2013-12-16T17:44:42.000000Z
handler_name : GPAC ISO Audio Handler
vendor_id : [0][0][0][0]
Side data:
audio service type: main
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> h264 (h264_nvenc))
Stream #0:2 -> #0:1 (copy)
Press [q] to stop, [?] for help
[h264 @ 0x55bd878c2d80] Cannot load libnvcuvid.so.1
[h264 @ 0x55bd878c2d80] Failed loading nvcuvid.
[h264 @ 0x55bd878c2d80] Failed setup for format cuda: hwaccel initialisation returned error.
[h264_nvenc @ 0x55bd86f5e680] Cannot load libnvidia-encode.so.1
[h264_nvenc @ 0x55bd86f5e680] The minimum required Nvidia driver for nvenc is 520.56.06 or newer
[vost#0:0/h264_nvenc @ 0x55bd86f5e1c0] Error initializing output stream: Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
Conversion failed!
Output from `nvidia-smi`:
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.85.05 Driver Version: 525.85.05 CUDA Version: 12.0 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 NVIDIA GeForce ... Off | 00000000:01:00.0 On | N/A |
| 27% 43C P8 12W / 250W | 500MiB / 11264MiB | 1% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
+-----------------------------------------------------------------------------+
The shared libraries are not part of the docker image. What are my options to add them?
</details>
# 答案1
**得分**: 3
检查您的nvidia-container-runtime参数。对于NVENC,您需要启用```video```功能。示例docker运行命令如下:
docker run --gpu all,capabilities=video -it nvidia/cuda:12.0.1-devel-ubuntu20.04 /bin/bash
在docker-compose.yml中,在`deploy/resources/reservations/devices/capabilities`下必须包括值```video```:
version: '2.2'
services:
name_of_your_service:
deploy:
resources:
reservations:
devices:
- driver: nvidia
device_ids: ['0']
capabilities: [gpu,video]
空(或仅包含值gpu)将不会链接NVENC,从而导致上述错误。我认为特权模式也将启用所有GPU功能。默认的apt预构建的ffmpeg ubuntu包在NVENC上可以工作,但在docker中需要进行与功能相关的相同步骤。否则,您将会遇到以下错误:
Cannot load libnvidia-encode.so.1
The minimum required Nvidia driver for nvenc is 470.57.02 or newer
Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
请参阅https://github.com/NVIDIA/nvidia-container-runtime以获取更多信息。
<details>
<summary>英文:</summary>
Check your nvidia-container-runtime parameters. For NVENC you need to enable the ```video``` capability. Example for docker run:
docker run --gpu all,capabilities=video -it nvidia/cuda:12.0.1-devel-ubuntu20.04 /bin/bash
In docker-compose.yml under your deploy/resources/reservations/devices/capabilities must include the value ```video```:
version: '2.2'
services:
name_of_your_service:
deploy:
resources:
reservations:
devices:
- driver: nvidia
device_ids: ['0']
capabilities: [gpu,video]
Empty (or only the value gpu) will NOT link NVENC resulting in the error above. I think privileged mode will also enable all gpu capabilities. The default apt pre-build ffmpeg ubunutu package does work with NVENC but the same steps regarding capabilities are needed in docker. Otherwise you get this error:
Cannot load libnvidia-encode.so.1
The minimum required Nvidia driver for nvenc is 470.57.02 or newer
Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
See https://github.com/NVIDIA/nvidia-container-runtime for more info.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论