求助 ffmpeg 转成WAV 进入死循环

btzhu btzhu 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
转成wav,文件无限变大。。。。。。。哪里出问题 了??还有中间那段循环很难懂,哪里有文献参考??



AVFormatContext *infmt_ctx;
int i = 0;
//////////////////////// 初始化 ////////////////////////
avcodec_register_all();
avdevice_register_all();
av_register_all();
//////////////////////// 输入 ////////////////////////
infmt_ctx = av_alloc_format_context();
//打开输入文件
if(av_open_input_file(&infmt_ctx, input_file, NULL, 0, NULL)!=0)
{
debug_string("can't open input file\n");
return -1;
}
//取出流信息
if(av_find_stream_info(infmt_ctx) <0)
{
debug_string("can't find suitable codec parameters\n");
return -2;
}
//dump_format(infmt_ctx, 0, input_file, 0); //列出输入文件的相关流信息
// 查找音频流信息
int audioindex=-1;
for(unsigned int j = 0; j < infmt_ctx->nb_streams; j++)
{
if(infmt_ctx->streams[j]->codec->codec_type == CODEC_TYPE_AUDIO)
{
audioindex=j;
break;
}
}
if(audioindex == -1) //没有找到音频流
{
debug_string("can't find audio stream\n");
return -3;
}
AVCodecContext *incode_ctx;
AVCodec *incodec;
incode_ctx = infmt_ctx->streams[audioindex]->codec;
//找到合适的音频解码器
incodec = avcodec_find_decoder(incode_ctx->codec_id);
if(incodec == NULL)
{
debug_string("can't find suitable audio decoder\n");
return -4;
}
//打开该音频解码器
if(avcodec_open(incode_ctx, incodec) < 0)
{
debug_string("can't open the audio decoder\n");
return -5;
}

//////////////////////// 输出 ////////////////////////
/* 解析输出文件的格式 */
AVOutputFormat *outfmt = guess_format(NULL, output_file, NULL);
if (!outfmt) {
printf("Could not deduce output format from file extension: using MPEG.\n");
outfmt = guess_format("mpeg", NULL, NULL);
}
if (!outfmt) {
debug_string("Could not find suitable output format\n");
return -6;
}
//outfmt->audio_codec = CODEC_ID_MP3; //////????

if(!strcmp(outfmt->extensions,"ogg"))
outfmt->audio_codec = CODEC_ID_VORBIS ;
/* allocate the output media context */
AVFormatContext *outfmt_ctx = av_alloc_format_context();
if (!outfmt_ctx) {
debug_string("Memory error\n");
return -7;
}
/* 保存输出文件的格式 */
outfmt_ctx->oformat = outfmt;
snprintf(outfmt_ctx->filename, sizeof(outfmt_ctx->filename), "%s", output_file);
/* add the audio and video streams using the default format codecs and initialize the codecs */
/* 输出文件的视频流编码器ID */
AVCodecContext *outcode_ctx;
AVCodec *outcodec;
AVStream *audio_st = NULL/*, *video_st*/;
//double audio_pts, video_pts;
//if (outfmt->video_codec != CODEC_ID_NONE) {
// video_st = add_video_stream(outfmt_ctx, outfmt->video_codec);
//}
/* 输出文件的音频流编码器ID */
if (outfmt->audio_codec != CODEC_ID_NONE) {
audio_st = av_new_stream(outfmt_ctx, 1);

outcode_ctx = audio_st->codec;
outcode_ctx->codec_id = outfmt->audio_codec;
outcode_ctx->codec_type = CODEC_TYPE_AUDIO;
outcode_ctx->bit_rate =incode_ctx->bit_rate ;
outcode_ctx->sample_rate = samples_rate > 0 ? samples_rate : incode_ctx->sample_rate; //////设置采样率
//outcode_ctx->time_base.den = 1;
//outcode_ctx->time_base.num = outcode_ctx->sample_rate;
outcode_ctx->channels = channel > 0 ? channel : incode_ctx->channels;
outcode_ctx->block_align = incode_ctx->block_align;
if(outcode_ctx->block_align == 1 && outcode_ctx->codec_id == CODEC_ID_MP3)
outcode_ctx->block_align = 0;
}
/* 设置输出参数 */
if (av_set_parameters(outfmt_ctx, NULL) < 0) {
debug_string("Invalid output format parameters\n");
return -8;
}

/* 列出输出文件的格式信息 */
dump_format(outfmt_ctx, 0, output_file, 1);
strcpy(outfmt_ctx->title, infmt_ctx->title);
strcpy(outfmt_ctx->author, infmt_ctx->author);
strcpy(outfmt_ctx->copyright, infmt_ctx->copyright);
strcpy(outfmt_ctx->comment, infmt_ctx->comment);
strcpy(outfmt_ctx->album, infmt_ctx->album);
outfmt_ctx->year = infmt_ctx->year;
outfmt_ctx->track = infmt_ctx->track;
strcpy(outfmt_ctx->genre, infmt_ctx->genre);
//dump_format(oc,0,output_file_name,1);//列出输出文件的相关流信息
//找到合适的音频编码器
outcodec = avcodec_find_encoder(outfmt_ctx->oformat->audio_codec);//(CODEC_ID_AAC);
if(!outcodec)
{
debug_string("can't find suitable audio encoder\n");
return -9;
}
if(avcodec_open(outcode_ctx, outcodec) <0) //////////////////////////////////////////////////
{
debug_string("can't open the output audio codec");
return -10;
}
/* now that all the parameters are set, we can open the audio and
video codecs and allocate the necessary encode buffers */
//if (video_st)
// open_video(outfmt_ctx, video_st);
/* 打开音频编码器 */
/*
int audioSize = 128 * 1024 * 4; //AVCODEC_MAX_AUDIO_FRAME_SIZE;//audio_outbuf_size/incode_ctx->channels;
if (outcode_ctx->frame_size <= 1)
{
switch(outcode_ctx->codec_id)
{
case CODEC_ID_PCM_S32LE:
case CODEC_ID_PCM_S32BE:
case CODEC_ID_PCM_U32LE:
case CODEC_ID_PCM_U32BE:
audioSize <<= 1;
break;
case CODEC_ID_PCM_S24LE:
case CODEC_ID_PCM_S24BE:
case CODEC_ID_PCM_U24LE:
case CODEC_ID_PCM_U24BE:
case CODEC_ID_PCM_S24DAUD:
audioSize = audioSize / 2 * 3;
break;
case CODEC_ID_PCM_S16LE:
case CODEC_ID_PCM_S16BE:
case CODEC_ID_PCM_U16LE:
case CODEC_ID_PCM_U16BE:
break;
default:
audioSize >>= 1;
break;
}
outSampleSize = audioSize * 2 * outcode_ctx->channels;
}
else
{
outSampleSize = outcode_ctx->frame_size * 2 * outcode_ctx->channels;
}
*/

/* 如果输出文件不存在, 则创建输出文件 */
if (!(outfmt->flags & AVFMT_NOFILE)) {
if (url_fopen(&outfmt_ctx->pb, output_file, URL_WRONLY) < 0) {
debug_string("Could not open '%s'\n", output_file);
return -11;
}
}

int ret = 0;

...全文
评论 1
收藏 已收藏
分享
返回顶部
btzhu
C语言 70,039 成员 243,237 内容