Ios ffmpeg函数avcodec_接收_帧始终返回EAGAIN错误
我正在使用ffmpeg对iOS上的aac音频文件进行解码,解码器为Ios ffmpeg函数avcodec_接收_帧始终返回EAGAIN错误,ios,ffmpeg,Ios,Ffmpeg,我正在使用ffmpeg对iOS上的aac音频文件进行解码,解码器为libfdk_aac,以下是音频文件: 下面是av\u dump\u格式的结果: Metadata: major_brand : iso6 minor_version : 0 compatible_brands: iso6dash Duration: N/A, bitrate: N/A Stream #0:0(und): Audio: aac (mp4a / 0x6134706D),
libfdk_aac
,以下是音频文件:
下面是av\u dump\u格式的结果:
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\u read\u frame
和avcodec\u send\u packet
返回0,但avcodec\u receive\u 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\u find\u解码器(AV\u codec\u ID\u mp3)代码>,所有的工作都很好。非常感谢。当avcodec\u receive\u frame返回EAGAIN时,在再次调用avcodec\u receive\u frame之前,必须先调用avcodec\u send\u packet(或流末尾的空数据包),然后再调用avcodec\u receive\u 我尝试过,当接收averor(EAGAIN)
时,执行av\u read\u帧(avFormatContext,&packet)代码>和avcodec\u发送\u数据包(编解码器上下文和数据包)
,在此之后avcodec\u接收\u帧(codecContext,frame)代码>总是得到AVERROR(EAGAIN)
我想这个逻辑已经在代码中了。可能是我错了,哈,内在的while循环是一个无限的循环。当EAGAIN返回时,它调用continue,继续循环,得到EAGAIN。在循环被中断(或删除)之前,您将始终获得EAGAIN抱歉,我的错误,当我发布此代码时,我做了一些编辑,已经更新了我的代码,内部循环中的continue
更改为break
,结果相同。