AVCodecContext::channel_layout 0 表示 WAV 文件

AVCodecContext::channel_layout 0 for WAV files

本文关键字:表示 WAV 文件 layout channel AVCodecContext      更新时间:2023-10-16

我已经使用 FFmpeg 成功加载压缩音频文件,并使用我编写的一些代码查询它们的channel_layouts:

AVFormatContext* fmtCxt = nullptr;
avformat_open_input( &fmtCxt, "###/440_sine.wav", nullptr, nullptr );
avformat_find_stream_info( fmtCxt, nullptr );
av_find_best_stream( fmtCxt, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0 );
AVCodecContext* codecCxt = fmtCxt->streams[ret]->codec;
AVCodec* codec = avcodec_find_decoder( codecCxt->codec_id );
avcodec_open2( codecCxt, codec, nullptr );
std::cout << "Channel Layout: " << codecCxt->channel_layout << std::endl;
av_dump_format( fmtCxt, 0, "###/440_sine.wav", 0 );

为了简洁起见,我删除了所有错误检查。 但是,对于Microsoft WAV 文件(单声道或立体声),AVCodecContext::channel_layout成员始终为 0 - 尽管ffprobeav_dump_format(..)都返回有效信息:

Input #0, wav, from '###/440_sine.wav':
Duration: 00:00:00.01, bitrate: 740 kb/s
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, 1 channels, s16, 705 kb/s

此外,codecCxt->channels返回正确的值。 使用 flac 文件(具有从同一应用程序生成的完全相同的音频数据)给出0x4 ( AV_CH_FRONT_CENTER ) 的channel_layout

您的WAV文件使用FFmpeg的pcm_s16le编解码器,该编解码器没有有关通道布局的信息。您只能拥有频道数。在这里可以找到很多解释

您对 flac 文件具有正确的channel_layout,因为 FFmpeg 的 flac 编解码器填充此字段。你可以在libavcodec/flac.c文件(flac_channel_layouts数组)上找到对应表。

如果您需要手动填写channel_layout,您可以致电:

codecCxt->channel_layout = av_get_default_channel_layout( codecCxt->channels );