为什么python中的pyav包无法识别h264_cuvid编解码器而ffmpeg可以?
我有一个如下的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经回答了我自己的问题,因为我找到了解决方案。
为了在 pyav 中使用硬件加速,您需要编译 ffmpeg,然后从源代码安装 pyav(不使用 pip install pyav)。另外,您还必须将系统的 cuda 路径设置到 bashrc 文件中,
此 bash脚本可以帮助您完成所有步骤:
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:
除非您从源代码编译 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.