我正在使用ffmpeg在iOS上解码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_frame
和avcodec_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);
,所有工作正常。非常非常感谢你。 最佳答案
当avcodec_receive_frame返回EAGAIN时,必须在再次调用avcodec_receive_frame之前,调用具有更多数据的avcodec_send_packet(或流末尾为空数据包)。
关于ios - ffmpeg函数avcodec_receive_frame始终返回EAGAIN错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57133098/