为什么python中的pyav包无法识别h264_cuvid编解码器而ffmpeg可以?

发布于 2025-01-16 20:50:04 字数 868 浏览 6 评论 0原文

我有一个如下的Python脚本来从视频中读取byte_stream。我想使用 h264_cuvid 代码进行 GPU 硬件加速而不是 CPU 编码或解码。当我运行此命令时,我的 FFmpeg 确实有 h264_cuvid 编解码器:

ffmpeg -c:v h264_cuvid -i input.mp4 output_codec.mp4

它运行成功,但在 pyav 包中,当我尝试创建如下 h264_cuvid 编解码器时:

import av

video = av.open(VIDEO_FILE_PATH)
target_stream = video.streams.video[0]
ctx = av.Codec('h264_cuvid', 'r').create()

它给了我一个错误,表示未知编解码器:

ctx = av.Codec('h264_cuvid', 'r').create()
File "av/codec/codec.pyx", line 184, in av.codec.codec.Codec.__cinit__
File "av/codec/codec.pyx", line 193, in av.codec.codec.Codec._init
av.codec.codec.UnknownCodecError: h264_cuvid
av version = 9.0.2

ffmpeg version 7:3.4.8-0ubuntu0.2

注意:我通过 pip install av

任何帮助将不胜感激

I have a python script as below to read byte_stream from video. I want to use h264_cuvid code to do GPU hardware accelerating instead of CPU encoding or decoding. my FFmpeg does have h264_cuvid codecs when I run this command:

ffmpeg -c:v h264_cuvid -i input.mp4 output_codec.mp4

it runs successfully but in pyav package, when I tried to create a h264_cuvid codec as below:

import av

video = av.open(VIDEO_FILE_PATH)
target_stream = video.streams.video[0]
ctx = av.Codec('h264_cuvid', 'r').create()

it gave me an error which said an unknown codec:

ctx = av.Codec('h264_cuvid', 'r').create()
File "av/codec/codec.pyx", line 184, in av.codec.codec.Codec.__cinit__
File "av/codec/codec.pyx", line 193, in av.codec.codec.Codec._init
av.codec.codec.UnknownCodecError: h264_cuvid
av version = 9.0.2

ffmpeg version 7:3.4.8-0ubuntu0.2

Note: I installed av by pip install av

any help would be appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧伤慢歌 2025-01-23 20:50:04

我已经回答了我自己的问题,因为我找到了解决方案。
为了在 pyav 中使用硬件加速,您需要编译 ffmpeg,然后从源代码安装 pyav(不使用 pip install pyav)。另外,您还必须将系统的 cuda 路径设置到 bashrc 文件中,

此 bash脚本可以帮助您完成所有步骤:


echo "************************  try building ffmpeg  *******************"
echo "Install nv-codec-headers"

git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git

cd nv-codec-headers && make -j4 && make install && cd ..

wget https://ffmpeg.org/releases/ffmpeg-4.2.1.tar.bz2
tar -xf ffmpeg-4.2.1.tar.bz2
cd ffmpeg-4.2.1
./configure --enable-cuda --enable-cuvid --enable-nvenc --enable-nonfree --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64 --enable-shared

make -j4
make install
cd ..

echo "************************  setting cuda path  *******************"
echo "export PATH=/usr/local/cuda/bin:/ffmpeg/libavdevice:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin" >> ~/.bashrc
echo "export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:/usr/local/cuda/targets/x86_64-linux/lib:/usr/local/cuda/targets/x86_64-linux/include:/usr/local/cuda/lib64:/usr/local/cuda/include:/usr/local/cuda/extras/CUPTI/lib64:/ffmpeg/libavdevice" >> ~/.bashrc

echo "************************  try building pyav  *******************"
git clone -b hwaccel https://github.com/rvillalba-novetta/PyAV.git

cd PyAV
source scripts/activate.sh
pip3 install -r tests/requirements.txt
make -j4

python3 setup.py install
cd ..

ldconfig /usr/local/cuda/lib64
echo "*********************   ffmpeg and pyav are built successfuly ***************"

I have answered my own question because I found the solution.
In order to use hardware accelerating in pyav, you need to compile ffmpeg and then install pyav from the source (not using pip install pyav). Also you have to set cuda path of your system into your bashrc file

this bashscript helps one to do all steps:


echo "************************  try building ffmpeg  *******************"
echo "Install nv-codec-headers"

git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git

cd nv-codec-headers && make -j4 && make install && cd ..

wget https://ffmpeg.org/releases/ffmpeg-4.2.1.tar.bz2
tar -xf ffmpeg-4.2.1.tar.bz2
cd ffmpeg-4.2.1
./configure --enable-cuda --enable-cuvid --enable-nvenc --enable-nonfree --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64 --enable-shared

make -j4
make install
cd ..

echo "************************  setting cuda path  *******************"
echo "export PATH=/usr/local/cuda/bin:/ffmpeg/libavdevice:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin" >> ~/.bashrc
echo "export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:/usr/local/cuda/targets/x86_64-linux/lib:/usr/local/cuda/targets/x86_64-linux/include:/usr/local/cuda/lib64:/usr/local/cuda/include:/usr/local/cuda/extras/CUPTI/lib64:/ffmpeg/libavdevice" >> ~/.bashrc

echo "************************  try building pyav  *******************"
git clone -b hwaccel https://github.com/rvillalba-novetta/PyAV.git

cd PyAV
source scripts/activate.sh
pip3 install -r tests/requirements.txt
make -j4

python3 setup.py install
cd ..

ldconfig /usr/local/cuda/lib64
echo "*********************   ffmpeg and pyav are built successfuly ***************"
等待我真够勒 2025-01-23 20:50:04

除非您从源代码编译 pyAV,否则它将使用供应商版本的 ffmpeg 进行预编译,该版本不支持硬件加速。这样做有两个原因:(1) 更好的设备兼容性,(2) 维护效率。

人们通常会列举使用 hwaccel 的三个主要原因:(1) 更快的编码,(2) 更快的解码,或 (3) 更好的能源效率。关于(1),在实践中,硬件编码器生成的视频质量往往比软件编码器低(比特率相等)。关于 (2),硬件解码器确实比软件解码器更快,但必须将帧复制回主机内存 (RAM),以使其可供 python 层使用。这将减少或抵消任何速度增益。关于(3),如果你追求能源效率,那么切换到编译语言将是比引入 hwaccel 更好的优化。

反对 hwaccel 的主要原因是:它难以实现且脆弱,因此您不仅需要一个非常有才华的人来编写初始代码,而且还需要一个同样有能力的人来志愿维护它并在 hwaccel API 发布后修复问题更改(它不是最稳定的)或者是否有错误或需要添加新功能。

所以总而言之,pyav 不支持它的主要原因是因为它被认为没有足够的影响力,而且 - 更重要的是 - 因为没有人愿意维护它,即不支持效率的维护。

Unless you compile pyAV from source it will come precompiled with a vendored version of ffmpeg that is built without support for hardware acceleration. This is done for two reasons (1) better device compatibility, and (2) efficiency of maintenance.

People typically cite three major reasons for using hwaccel: (1) faster encoding, (2) faster decoding, or (3) better energy efficiency. Regarding (1), hardware encoders tend to generate lower quality video than software encoders in practice (bitrate equated). Regarding (2), hardware decoders are indeed faster than software decoders but will have to copy the frame back into host memory (RAM) to make it available to the python layer. This will diminish or nullify any speed gains. Regarding (3), if you are after energy efficiency then switching to a compiled language would be a much better optimization than introducing hwaccel.

The main reason against hwaccel is: It's hard to implement and brittle, so not only do you need a quite talented individual to write the initial code, but you also need an equally capable person to volunteer to maintain it and fix things once the hwaccel API changes (it isn't the most stable) or if there are bugs or new features to add.

So in sum, the main reason why it isn't supported in pyav is because it is seen as not impactful enough, and - more importantly - because there isn't anyone willing to maintain it, i.e., it isn't supported for efficiency of maintainance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文