本程序实现转码一个普通视频文件为视频mpeg4,音频mp3的功能
- #include <avcodec.h>
- #include <avformat.h>
- #include <stdio.h>
- #include <avutil.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- main(int argc,char **argv)
- {
- const char *input_file_name="/root/movies/ddh1.mpg";
- 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<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);
- }
-
- const char *output_file_name="/root/123.avi";
- AVOutputFormat *fmt;
- AVFormatContext *oc;
- AVCodecContext *oVcc,*oAcc;
- AVCodec *oVc,*oAc;
- AVStream *video_st,*audio_st;
- AVFrame *oVFrame,*oAFrame;
- double video_pts;
- 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;
- pstrcpy(oc->filename,sizeof(oc->filename),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_MPEG4;
- oVcc->codec_type=CODEC_TYPE_VIDEO;
- oVcc->bit_rate=2500000;
- oVcc->width=704;
- oVcc->height=480;
- oVcc->time_base=vCodecCtx->time_base;
- oVcc->gop_size=vCodecCtx->gop_size;
-
- oVcc->pix_fmt=vCodecCtx->pix_fmt;
- oVcc->max_b_frames=vCodecCtx->max_b_frames;
- video_st->r_frame_rate=ic->streams[videoindex]->r_frame_rate;
- audio_st=av_new_stream(oc,oc->nb_streams);
- if(!audio_st)
- {
- printf("could not alloc audio stream\n");
- exit(0);
- }
- avcodec_get_context_defaults2(audio_st->codec,CODEC_TYPE_AUDIO);
- oAcc=avcodec_alloc_context();
- oAcc=audio_st->codec;
- oAcc->codec_id=CODEC_ID_MP3;
- oAcc->codec_type=CODEC_TYPE_AUDIO;
- oAcc->bit_rate=aCodecCtx->bit_rate;
- oAcc->sample_rate=aCodecCtx->sample_rate;
- oAcc->channels=2;
- if (av_set_parameters(oc, NULL) < 0)
- {
- printf( "Invalid output format parameters\n");
- exit(0);
- }
- strcpy(oc->title,ic->title);
- strcpy(oc->author,ic->author);
- strcpy(oc->copyright,ic->copyright);
- strcpy(oc->comment,ic->comment);
- strcpy(oc->album,ic->album);
- oc->year=ic->year;
- oc->track=ic->track;
- strcpy(oc->genre,ic->genre);
- dump_format(oc,0,output_file_name,1);
- oVc=avcodec_find_encoder(CODEC_ID_MPEG4);
- if(!oVc)
- {
- printf("can't find suitable video encoder\n");
- exit(0);
- }
- if(avcodec_open(oVcc,oVc)<0)
- {
- printf("can't open the output video codec\n");
- exit(0);
- }
- oAc=avcodec_find_encoder(CODEC_ID_MP3);
- if(!oAc)
- {
- printf("can't find suitable audio encoder\n");
- exit(0);
- }
- if(avcodec_open(oAcc,oAc)<0)
- {
- printf("can't open the output audio codec");
- exit(0);
- }
-
-
-
-
-
- if (!(oc->flags & AVFMT_NOFILE))
- {
- if(url_fopen(&oc->pb,output_file_name,URL_WRONLY)<0)
- {
- printf("can't open the output file %s\n",output_file_name);
- exit(0);
- }
- }
- if(!oc->nb_streams)
- {
- fprintf(stderr,"output file dose not contain any stream\n");
- exit(0);
- }
- if(av_write_header(oc)<0)
- {
- fprintf(stderr, "Could not write header for output file\n");
- exit(1);
- }
- AVPacket packet;
- uint8_t *ptr,*out_buf;
- int out_size;
- static short *samples=NULL;
- static unsigned int samples_size=0;
- uint8_t *video_outbuf,*audio_outbuf;
- int video_outbuf_size,audio_outbuf_size;
- video_outbuf_size=400000;
- video_outbuf= (unsigned char *) malloc(video_outbuf_size);
- audio_outbuf_size = 10000;
- audio_outbuf = av_malloc(audio_outbuf_size);
- int flag;int frameFinished;int len;int frame_index=0,ret;
- while(av_read_frame(ic,&packet)>=0)
- {
- if(packet.stream_index==videoindex)
- {
- len=avcodec_decode_video(vCodecCtx,oVFrame,
- &frameFinished,packet.data,packet.size);
- if(len<0)
- {
- printf("Error while decoding\n");
- exit(0);
- }
- if(frameFinished)
- {
- fflush(stdout);
- oVFrame->pts=av_rescale(frame_index,
- AV_TIME_BASE*(int64_t)oVcc->time_base.num,
- oVcc->time_base.den);
- oVFrame->pict_type=0;
- out_size = avcodec_encode_video(oVcc, video_outbuf,
- video_outbuf_size, oVFrame);
- if (out_size > 0)
- {
- AVPacket pkt;
- av_init_packet(&pkt);
- if
(秩名)
|