首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ffmpeg函数avcodec_receive_frame总是返回EAGAIN错误

ffmpeg函数avcodec_receive_frame总是返回EAGAIN错误
EN

Stack Overflow用户
提问于 2019-07-21 20:10:50
回答 1查看 1.6K关注 0票数 1

我在iOS上使用ffmpeg解码he-aac音频文件,它的解码器是libfdk_aac,这是音频文件:https://cdn.perterpon.com/listen/test/bbc.mp4,这是av_dump_format结果:

Metadata:
    major_brand     : iso6
    minor_version   : 0
    compatible_brands: iso6dash
  Duration: N/A, bitrate: N/A
    Stream #0:0(und): Audio: aac (mp4a / 0x6134706D), 48000 Hz, 2 channels (default)
    Metadata:
      handler_name    : USP Sound Handler

av_read_frameavcodec_send_packet返回0,但avcodec_receive_frame始终返回AVERROR(EAGAIN)

我已经尝试了ffmpeg命令行工具:ffmpeg -i bbc.mp4 bbc.mp3,它成功了,并且mp3文件可以很好地在iOS上播放。

下面是我的代码:

av_register_all();
AVFormatContext *avFormatContext = avformat_alloc_context();
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"bbc" ofType:@"mp4"];

int ret;
ret = avformat_open_input(&avFormatContext, [filePath UTF8String], NULL, NULL);
if (0 != ret) {
    NSLog(@"avformat_open_input failed: %d", ret);
}

ret = avformat_find_stream_info(avFormatContext, NULL);
if (0 != ret) {
    NSLog(@"avformat_find_stream_info: %d", ret);
}

// the libfdk_aac decoder
AVCodec *codec = avcodec_find_decoder_by_name("libfdk_aac");
AVCodecContext *codecContext = avcodec_alloc_context3(codec);

ret = avcodec_open2(codecContext, codec, NULL);
if (0 != ret) {
    NSLog(@"avcodec_open2 faild: %d", ret);
}

AVFrame *frame = av_frame_alloc();
AVPacket packet;
av_init_packet(&packet);

// start read data and decode data
while (true) {
    ret = av_read_frame(avFormatContext, &packet);
    if (0 != ret) {
        break;
    }
    ret = avcodec_send_packet(codecContext, &packet);
    if (ret != 0) {
        NSLog(@"send package with error: %d", ret);
        continue;
        break;
    }
    while (true) {
        // the ret below is always return -35, means AVERROR(EAGAIN)
        ret = avcodec_receive_frame(codecContext, frame);
        if (ret == AVERROR(EAGAIN)) {
            NSLog(@"avcodec_receive_frame with EAGAIN error: %d", ret);
            break;
        } else if (ret == AVERROR_EOF) {
            NSLog(@"end of file");
            break;
        }
    }
    if (ret == AVERROR(EAGAIN)) {
        continue;
    }
}

我尝试将bbc.mp4文件替换为bbc.mp3文件,并将解码器更改为:AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_MP3);,所有的东西都工作得很好。非常感谢。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57133098

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档