FFMPEG 解码MP3的Header Missing报错

紅線 紅線 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
使用FFMPEG解码MP3文件时,函数avcodec_decode_audio4会返回无效值(-1094995529)

打开文件

_INT ret = 0;
//分配输入
in_fmtctx = avformat_alloc_context();
//输入缓存
in_ioctx = avio_alloc_context((_UCHAR*)av_malloc(32768), 32768, 0, src_buf, &_V2D_Audio::read_buffer, NULL, NULL);
in_fmtctx->pb = in_ioctx;
in_fmtctx->flags = AVFMT_FLAG_CUSTOM_IO;
if ((ret = avformat_open_input(&in_fmtctx, "", NULL, NULL)) < 0)
return ret;
if ((ret = avformat_find_stream_info(in_fmtctx, NULL)) < 0)
return ret;

//decode file prepare
AVCodecID decode_id;
audiostream_index = get_codecid(in_fmtctx, decode_id);
if(decode_id == AV_CODEC_ID_NONE)
return ret;

if ((ret = avcodec_open2(in_fmtctx->streams[audiostream_index]->codec, avcodec_find_decoder(decode_id), NULL)) < 0)
return ret;

return ret;


解码文件

while(TRUE)
{
const int output_frame_size = out_fmtctx->streams[0]->codec->frame_size > 0 ? out_fmtctx->streams[0]->codec->frame_size : in_fmtctx->streams[audiostream_index]->codec->frame_size;

_BOOL finished = FALSE;

AVFrame* frame = NULL;
while (av_audio_fifo_size(fifo) < output_frame_size)
{
AVPacket packet;

av_init_packet(&packet);

packet.data = NULL;

packet.size = 0;

if ((ret = av_read_frame(in_fmtctx, &packet)) < 0)
{
finished = TRUE;

av_free_packet(&packet);

break;
}

_INT got_frame = 0;

frame = av_frame_alloc();

//avcodec_decode_audio4返回无效值,得到错误信息为Header Missing
if ((ret = avcodec_decode_audio4(in_fmtctx->streams[audiostream_index]->codec, frame, &got_frame, &packet)) < 0)
{
av_free_packet(&packet);

av_frame_free(&frame);

return 0;
}
//后面还有对解码出来的frame的后续操作,但是因为这个函数报错返回,所以省略后面的操作
}
}
...全文
评论 5
收藏 已收藏
分享
返回顶部
紅線
多媒体/流媒体开发 2,554 成员 20,277 内容