【问题标题】:AVCodecContext::channel_layout 0 for WAV filesAVCodecContext::channel_layout 0 用于 WAV 文件
【发布时间】:2013-11-15 12:43:38
【问题描述】:

我已经使用 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-&gt;channels 也返回正确的值。使用 flac 文件(完全从同一个应用程序生成相同的音频数据),得到一个 0x4 (AV_CH_FRONT_CENTER) 的 channel_layout

【问题讨论】:

  • 我应该声明我使用的是 FFmpeg 2.1。

标签: c++ ffmpeg wav codec


【解决方案1】:

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

你有正确的channel_layout 和flac 文件,因为FFmpeg 的flac 编解码器填充了这个字段。您可以在libavcodec/flac.c文件中找到对应表,flac_channel_layouts数组。

如需手动填写channel_layout,可致电:

codecCxt-&gt;channel_layout = av_get_default_channel_layout( codecCxt-&gt;channels );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多