【问题标题】:av_write_header - Error with sample formatav_write_header - 样本格式错误
【发布时间】:2021-08-07 15:43:59
【问题描述】:

我正在使用 libav/ffmpeg 编写程序来下载互联网广播流并使用 alsa 在声卡上播放。

我已经设法下载流并提取数据包和帧。

我遇到了必须调用的 av_write_header() 函数(根据 https://www.ffmpeg.org/doxygen/3.2/group__lavf__encoding.html#details)的问题。它崩溃并给我以下错误:

[alsa @ 0x55d7ba32e580] 不支持样本格式 0x15001

数字 0x15001 是十进制的 86017,这是该流使用的 MP3 格式 (AV_CODEC_ID_MP3) 的枚举 AVCodecID 中的索引。示例格式的索引为 3。我不明白为什么 libav 解析标头错误。

这是我负责配置输出的部分代码:

    avdevice_register_all();

    AVOutputFormat *output = av_guess_format("alsa",NULL,NULL);

    AVFormatContext *outputFormatContext = avformat_alloc_context();
    outputFormatContext->oformat = output;
    outputFormatContext->flags = AVFMT_NOFILE;

    AVStream *stream = avformat_new_stream(outputFormatContext,NULL);

    AVCodecParameters *oCodecParameters = avcodec_parameters_alloc();

    ret = avcodec_parameters_copy(oCodecParameters,iCodecParameters);
    if(ret < 0){
        printf("avformat_parameters_copy\n");
        exit(0);
    }

    stream->codecpar = oCodecParameters;

    if(avformat_write_header(outputFormatContext,NULL)<0){
        dumpParameters(stream->codecpar);
        printf("avformat_write_header\n");
        exit(0);
    }

完整代码在这里:https://github.com/szymonbarszcz99/C-internet-radio

【问题讨论】:

    标签: c ffmpeg alsa libav


    【解决方案1】:

    似乎在 libav 中我们不能进行简单的复制。相反,我必须手动给它请求的参数。将avcodec_parameters_copy() 更改为此

        AVCodecParameters *oCodecParameters = avcodec_parameters_alloc();
        oCodecParameters->format = 8;
        oCodecParameters->codec_type = 1;
        oCodecParameters->sample_rate = 44100;
        oCodecParameters->channels = 2;
    
        stream->codecpar = oCodecParameters;
    

    解决了这个问题

    【讨论】:

      猜你喜欢
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      • 2010-09-14
      • 2018-01-01
      • 2011-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多