ffmpeg 视频格式转换后没有声音,该怎么解决
ffmpeg 视频格式转换后没有声音
int main(int argc,char **argv)
{
const char *input_file_name="E://testdata//94.ts";
const char *output_file_name="E://testdata//94.avi";
av_register_all();
//输入文件处理部分
AVFormatContext *ic;
ic=av_alloc_format_context();
//打开输入文件
if(av_open_input_file(&ic,input_file_name,NULL,0,NULL)!=0)
{
printf("can't open the file %s\n",input_file_name);
exit(1);
}
//取出流信息
if(av_find_stream_info(ic)<0)
{
printf("can't find suitable codec parameters\n");
exit(1);
}
//列出输入文件的相关流信息
dump_format(ic,0,input_file_name,0);
int i;
int videoindex=-1;
int audioindex=-1;
for(i=0;i<(int)ic->nb_streams;i++)
{
if(ic->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) {
videoindex=i;
}
else if(ic->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO)
{
audioindex=i;
}
}
if(videoindex==-1)
{
printf("can't find video stream\n");
exit(1);
}
AVCodecContext *vCodecCtx;
vCodecCtx=ic->streams[videoindex]->codec;//取得视频流编码上下文指针
AVCodec *vCodec;
//列出输入文件的相关流信息
vCodec=avcodec_find_decoder(vCodecCtx->codec_id);
if(vCodec==NULL) {
printf("can't find suitable video decoder\n");
exit(1);
}
//打开该视频解码器
if(avcodec_open(vCodecCtx,vCodec)<0) {
printf("can't open the video decoder\n");
exit(1);
}
if(audioindex==-1) {
printf("can't find audio stream\n");
exit(1);
}
AVCodecContext *aCodecCtx;
aCodecCtx=ic->streams[audioindex]->codec;
AVCodec *aCodec;
//找到合适的音频解码器
aCodec=avcodec_find_decoder(aCodecCtx->codec_id);
if(aCodec==NULL) {
printf("can't find suitable audio decoder\n");
exit(1);
}
//打开该音频解码器
if(avcodec_open(aCodecCtx,aCodec)<0) {
printf("can't open the audio decoder\n");
exit(1);
}
//下面为输出文件处理部分
AVOutputFormat *fmt;
AVFormatContext *oc;
AVCodecContext *oVcc,*oAcc;
AVCodec *oVc,*oAc;
AVStream *video_st,*audio_st;
AVFrame *oVFrame;
oVFrame = avcodec_alloc_frame();
//判断是否可以判断输出文件的编码格式
fmt = guess_format(NULL,output_file_name,NULL);
if(!fmt)
{
printf("could not deduce output format from outfile extension\n");
exit(0);
}
oc = av_alloc_format_context();
if(!oc)
{
printf("Memory error\n");
exit(0);
}
oc->oformat = fmt;
snprintf(oc->filename,sizeof(oc->filename),"%s",output_file_name);
video_st = av_new_stream(oc,0);
if(!video_st)
{
printf("could not alloc video stream\n");
exit(0);
}
oVcc = avcodec_alloc_context();
oVcc = video_st->codec;
oVcc->codec_id = CODEC_ID_H264;
oVcc->codec_type = CODEC_TYPE_VIDEO;
oVcc->bit_rate = vCodecCtx->bit_rate/2;
oVcc->width = vCodecCtx->width;
oVcc->height = vCodecCtx->height;
oVcc->time_base = vCodecCtx->time_base;
oVcc->gop_size = vCodecCtx->gop_size;
oVcc->pix_fmt = vCodecCtx->pix_fmt;