简体   繁体   English

PyAv av.open() 指定使用的编解码器

[英]PyAv av.open() specify used codec

When PyAv is used to open the alsa audio device.当使用 PyAv 打开 alsa 音频设备时。 How I can specify used codec and not the ffmpeg default one because that is wrong.我如何指定使用的编解码器而不是 ffmpeg 默认的编解码器,因为那是错误的。 By default it will use pcm_s16le and I need to use pcm_s32le .默认情况下它将使用pcm_s16le ,我需要使用pcm_s32le I can record from my device with following ffmpeg command:我可以使用以下 ffmpeg 命令从我的设备录制:

ffmpeg -f alsa -acodec pcm_s32le -i dmic_sv alsaout.wav

but not with但不与

ffmpeg -f alsa -i dmic_sv alsaout.wav

Which will give me following error:这会给我以下错误:

[alsa @ 0x12061c0] cannot set sample format 0x10000 2 (Invalid argument)
dmic_sv: Input/output error

How to transfer the working command to PyAv av.open() function?如何将工作命令转移到 PyAv av.open() function? There is stream_options but it doesn't seem to work.stream_options但它似乎不起作用。 I tried我试过了

stream_options = [{'-acodec': 'pcm_s32le'}]
av.open('dmic_sv', format='alsa', mode='r', stream_options=stream_options)

And I get the same as above.我得到的和上面一样。

av.error.OSError: [Errno 5] Input/output error: 'dmic_sv'; last error log: [alsa] cannot set sample format 0x10000 2 (Invalid argument)

How to do this?这个怎么做?

I'll answer my own question because I figured it out.我会回答我自己的问题,因为我想通了。 I read ffmpeg source code and saw that when using alsa audio device and codec is not specified ffmpeg will default to use signed 16-bit pcm samples.我阅读了 ffmpeg 源代码,发现当使用 alsa 音频设备和未指定编解码器时,ffmpeg 将默认使用带符号的 16 位 pcm 样本。 Code here .代码在这里 By further exploring the source code the codec value comes from AVFormatContext::audio_codec_id struct field.通过进一步探索源代码,编解码器值来自AVFormatContext::audio_codec_id结构字段。

Now figuring out that PyAV using Cython to use FFmpeg and by reading PyAV source code of Container class I noticed it holds AVFormatContext in it's self.ptr variable.现在弄清楚 PyAV 使用 Cython 来使用 FFmpeg 并通过阅读容器class 的 PyAV 源代码,我注意到它在self.ptr变量中保存了AVFormatContext Then reading InputContainer source code and especially before calling avformat_open_input function to open the alsa device.然后阅读InputContainer源代码,尤其是在调用avformat_open_input function 打开 alsa 设备之前。 Specifying the used audio codec is not supported by PyAV. PyAV 不支持指定使用的音频编解码器。

I forked the library and ended quickly hacking the solution for me.我分叉了图书馆,并很快为我破解了解决方案 Now the question is would it be possible to add this feature to PyAV to force the codec used for the audio?现在的问题是是否可以将此功能添加到 PyAV 以强制用于音频的编解码器? In this case when the device is using pcm samples and relying ffmpeg to use choose the default one it will always use 16-bit samples and in my case I needed to use 32-bit samples.在这种情况下,当设备使用 pcm 样本并依赖 ffmpeg 使用选择默认值时,它将始终使用 16 位样本,而在我的情况下,我需要使用 32 位样本。

Hopefully this helps someone and save them the trouble I went through:) I also posted this same answer for PyAV issue here .希望这可以帮助某人并为他们省去我遇到的麻烦:) 我也在这里发布了针对 PyAV 问题的相同答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM