It's hard. After one months of ...
Purpose: From an AVI transcoding to Mp4,avi (Video encoder: MPEG4, audio codec: MPEG2 audio,), MP4 (Video encoder: H264/AVC, Audio encoder: MPEG2 audio)
As a result, the basic can be, the video has a little problem, audio can be. Here first put the code here, then slowly improve it, the audio must be resampled, the video is to be re-scaled ... There are also audio and video synchronization also wood has done, and so have done to add comments ...
int Mp4tomp4 (char* srcname, char* destname) {logi ("Logi----------------0");
Input_file_name= SrcName;
Av_register_all ();
Avformatcontext *ic = NULL;
Logi ("Logi----------------02");
Logi ("Logi----------------1 input_file%s", input_file_name);
Ic=av_alloc_format_context ();
AVFORMAT_ALLOC_OUTPUT_CONTEXT2 (&ic, NULL, NULL, INPUT_FILE_NAME); if (Avformat_open_input (&ic, input_file_name, NULL, NULL) < 0) {Logi ("can ' t open the file%s\n", Input_file_n
AME);
Exit (1);
} if (Avformat_find_stream_info (ic,null) <0) {Logi ("can ' t find suitable codec parameters\n");
Exit (1);
} logi ("Logi----------------2");
Av_dump_format (ic,0,input_file_name,1);
int i;
int Videoindex=-1;int audioindex=-1;
for (i=0;i<ic->nb_streams;i++) {if (Ic->streams[i]->codec->codec_type==avmedia_type_video) {
Videoindex=i;
Logi ("Video%d\n", Videoindex); } else if (ic->streams[i]-≫codec->codec_type==avmedia_type_audio) {audioindex=i;
Logi ("Audio%d\n", Audioindex);
}} logi ("Logi----------------3");
if (videoindex==-1) {Logi ("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) {Logi ("can ' t find suitable video decoder\n");
Exit (1);
}//ҵʵƶif (Avcodec_open2 (vcodecctx,vcodec,null) <0) {Logi ("can ' t open the video decoder\n");
Exit (1); }//
Logi ("Logi----------------4,%d", vcodecctx->codec_id);
if (audioindex==-1) {Logi ("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) {Logi ("can ' t find suitable audio decoder\n");
Exit (1);
} if (Avcodec_open2 (acodecctx,acodec,null) <0) {Logi ("can ' t open the audio decoder\n");
Exit (1);
} logi ("Logi----------------5 output file");
const char *output_file_name= destname;
Avoutputformat *fmt;
Avformatcontext *oc;
Avcodeccontext *OVCC,*OACC;
Avcodec *OVC,*OAC;
Avstream *video_st,*audio_st;
Avframe *ovframe = Null,*oaframe = NULL;
Ovframe=avcodec_alloc_frame ();
Oaframe=avcodec_alloc_frame ();
Fmt=av_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 ();
Logi ("Logi----------------6");
AVFORMAT_ALLOC_OUTPUT_CONTEXT2 (&OC, NULL, NULL, output_file_name);
if (!OC) {logi ("Memory error\n");
Exit (0);
} Oc->oformat->video_codec = av_codec_id_h264;
Oc->oformat->audio_codec = acodecctx->codec_id;
fmt=oc->oformat;
Video_st = NULL;
Audio_st = NULL;
pstrcpy (Oc->filename,sizeof (oc->filename), output_file_name);
Logi ("Logi----------------7");
Video_st=avformat_new_stream (Oc,null);
if (!video_st) {Logi ("Could not alloc video stream\n");
Exit (0);
}//Ovcc=avcodec_alloc_context3 (VIDEO_ST->CODEC);
Logi ("===============%d", vcodecctx->pix_fmt);
ovcc=video_st->codec;
OVc = Avcodec_find_encoder (av_codec_id_h264);
if (!OVC) {Logi ("1111codec not found\n");
Exit (1); } avcodec_get_context_Defaults3 (OVCC,OVC);//Do what ovcc->codec_id=av_codec_id_h264;
ovcc->codec_type=avmedia_type_video;
Fixed bit rate control ovcc->bit_rate=12000;
ovcc->bit_rate_tolerance=200000; Ovcc->rc_min_rate =12000;//set minimum video bitrate tolerance ovcc->rc_max_rate = 12000;//set maximum video bitrate tolerance ovcc->bit_rate_tolerance =
12000;
ovcc->rc_buffer_size=12000;//set bitrate control buffer size ovcc->rc_initial_buffer_occupancy = ovcc->rc_buffer_size*3/4;
ovcc->rc_buffer_aggressivity= (float) 1.0;
ovcc->rc_initial_cplx= 0.5;
Ovcc->qcompress =0;//Video Quantization scale compression (VBR) Ovcc->qmin = 10;//Minimum Video quantization scale (VBR) is the ratio of sampling.
Ovcc->qmax = 50;//Maximum video quantization scale (VBR)//ovcc->global_quality = 10;
-qscale Q uses fixed video quantization scale (VBR)//variable bitrate control//ovcc->flags |= Codec_flag_qscale;
Ovcc->rc_min_rate = 100000;
Ovcc->rc_max_rate = 200000;
Ovcc->bit_rate = 150000;
ovcc->width=320;
ovcc->height=240;
Ovcc->time_base.den = 25; Ovcc->tiMe_base.num = 1;
ovcc->time_base=vcodecctx->time_base;
ovcc->gop_size=10;//This value indicates that an I-frame is inserted every 10 frames (intra frame)//ovcc->pix_fmt=vcodecctx->pix_fmt;
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;//frame rate//audio_st=av_new_stream (OC, Oc->nb_streams);
Logi ("Logi----------------8%d", ovcc->codec_id); if (! strcmp (oc-> oformat-> name, "mp4") | |!strcmp (OC->oformat->name, "mov") | |!strcmp (OC->oforma
T->name, "3gp")) Ovcc->flags |= Codec_flag_global_header;
Audio_st=avformat_new_stream (Oc,null);
if (!audio_st) {Logi ("could not alloc audio stream\n");
Exit (0);
}//audio_st->id=1;
OACC=AVCODEC_ALLOC_CONTEXT3 (ACODECCTX->CODEC);
Logi ("Logi----------------9.6");
oacc=audio_st->codec; OAc = Avcodec_find_encoder (acodecCTX->CODEC_ID);
if (!OAC) {Logi ("1111codec not found\n");
Exit (1);
} avcodec_get_context_defaults3 (OACC,OAC);
Resamplecontext *RSC = NULL;
oacc->codec_id=acodecctx->codec_id;
oacc->codec_type=avmedia_type_audio;
oacc->bit_rate=acodecctx->bit_rate;//bit Rate oacc->sample_rate=acodecctx->sample_rate;
OACC->SAMPLE_FMT = AV_SAMPLE_FMT_S16;
Oacc->channels = 1;
Oacc->frame_size = acodecctx->frame_size;
if (Oc->oformat->flags & Avfmt_globalheader) oacc->flags |= Codec_flag_global_header;
Logi ("Logi----------------Bayi%d", oacc->sample_rate); if (! strcmp (oc-> oformat-> name, "mp4") | |!strcmp (OC->oformat->name, "mov") | |!strcmp (OC->oforma
T->name, "3gp")) Oacc->flags |= Codec_flag_global_header;
Av_dump_format (oc,0,output_file_name,1);
Ovc=avcodec_find_encoder (ovcc->codec_id); IF (!OVC) {Logi ("can ' t find suitable video encoder\n");
Exit (0);
} int rett = 0;
if ((Rett =avcodec_open2 (ovcc,ovc,null)) <0) {Logi ("can ' t open the output video codec%d", Rett);
Exit (0);
} oac=avcodec_find_encoder (acodecctx->codec_id);
if (!OAC) {Logi ("can ' t find suitable audio encoder\n");
Exit (0);
} if (Avcodec_open2 (oacc,oac,null) <0) {Logi ("can ' t open the output audio codec11");
Exit (0); }/*if (Url_exist (output_file_name)) {printf ("The output file name%s has a exist,please select other\n", out
Put_file_name);
Exit (0); }*/if (! (
Oc->flags & Avfmt_nofile) {if (Avio_open (&OC->PB, Output_file_name, Avio_flag_write) < 0) {
Logi ("Could not open '%s ' \ n", output_file_name);
return 1; }} if (!oc->nb_strEams) {logi ("Output file dose not contain any stream\n");
Exit (0);
} logi ("Logi----------------11");
if (Avformat_write_header (oc,null) <0) {Logi ("Could not write header for output file\n");
Exit (1);
} logi ("Logi----------------hebing");
Avpacket packet;
uint8_t *ptr,*out_buf;
int out_size;
int16_t *samples=null,resample_buffer = NULL;
static unsigned int samples_size=0;
uint8_t *video_outbuf,*audio_outbuf;
int video_outbuf_size,audio_outbuf_size;
video_outbuf_size=100000;
video_outbuf= (unsigned char *) malloc (video_outbuf_size);
Audio_outbuf_size = 10000;
Audio_outbuf = Av_malloc (audio_outbuf_size);
Audio_outbuf = (unsigned char *) malloc (audio_outbuf_size); int flag;int framefinished,framefinished2;int Len;int frame_index=0,ret,ret1;
int got_output=0;
Logi ("Logi----------------13");
File* F;
f = fopen ("VIDEO/11111111.AAC", "WB");
Avfifobuffer *fifo;
fifo= Av_fifo_alloc (1024); if (!FIFO)
return-1;
Oaframe->nb_samples = oacc->frame_size;
Oaframe->format = oacc->sample_fmt;
Oaframe->channel_layout = oacc->channel_layout; while (Av_read_frame (ic,&packet) >=0) {if (Packet.stream_index==videoindex) {L
OGI ("Logi----------------14.4");
Len=avcodec_decode_video2 (Vcodecctx,ovframe,&framefinished,&packet);
Logi ("%d", Len);
if (len<0) {Logi ("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=av_picture_type_none;
Out_size = Avcodec_encode_video (OVCC, Video_outbuf, Video_outbuf_size, ovframe); if (Out_size > 0) {avpacket pkt; Av_init_packet (&PKT);//After executing this statement, the PKT pts and DTS are set to No if (Ovcc->coded_frame && Ovcc->coded_fra
Me->key_frame) pkt.flags |= Av_pkt_flag_key;
Pkt.flags = Packet.flags;
pkt.stream_index= video_st->index;
Pkt.data= Video_outbuf;
Pkt.size= out_size;
Ret=av_write_frame (OC, &PKT);
Ret=av_interleaved_write_frame (Oc,&packet);
Logi ("%d----111111----%d----", packet.size,pkt.size);
} frame_index++;
} else {Logi ("Logi----------------16");
Logi ("... \ n"); } #if 0 if (ret!=0) {Logi ("while write vid
EO frame error\n "); Exit (0); } #endif} else if (Packet.stream_index = = Audioindex) {logi ("Logi----------------17,
%d ", audioindex);
int ret = 0;
while (Packet.size > 0) {logi ("Logi----------------18");
Out_buf = NULL; if (Packet.size > 0) Samples = (short *) av_fast_realloc (samples, &samples_size, Ffmax (packet.size* sizeof (*samples), 2000));
Avcodec_max_audio_frame_size= Logi ("Logi----------------19");
ret = Avcodec_decode_audio4 (Acodecctx, Oaframe, &framefinished2, &packet); ret = Avcodec_decode_audio3 (acodecctx,samples,&out_size,&packet);//If it is an audio packet, decode the audio packet Logi ("R
etretret:%d ", ret);
if (Ret < 0) {Logi ("while Decode audio failure\n");
Exit (0);
} Packet.data + = ret;
Packet.size-= ret;
Av_fifo_generic_write (Fifo,oaframe->data[0],oaframe->linesize[0],null); if (FRAMEFINISHED2) {//Logi ("----------lddfd,%d", fwrite (Oaframe->data[0], oaframe->line
Size[0], 1, PCM);
Fflush (PCM); int data_size = Av_samples_get_buffer_size (NULL, acodecctx->channels,//
Oaframe->nb_samples,//ACODECCTX->SAMPLE_FMT, 1);
Fwrite (Oaframe->data[0], 1, DATA_SIZE/2, PCM);
Fflush (PCM);
Logi ("Logi----------------20");
Out_buf = (uint8_t *) samples;
Avpacket PKT;
Av_init_packet (&PKT);
Pkt.data = NULL;
pkt.size = 0;
Ret1 = Avcodec_encode_audio2 (OACC, &pkt, Oaframe, &got_output);
if (Ret1 < 0) {fprintf (stderr, "Error encoding audio frame\n");
Exit (1); }//Pkt.size = Avcodec_encode_audio (OACC, Audio_outbuf, AUDIO_OUTBUF_size, (short int*) samples);
if (got_output) {Logi ("DFD,"); Fwrite (Pkt.data, 1, pkt.size, f);//fwrite (Pkt.data, 1, pkt.size, f); without av_interleaved_write_frame, write directly to the AAC file to play
, which means there is no problem with decoding.
Pkt.dts = Av_nopts_value; if (oacc->coded_frame&& oacc->coded_frame->pts! = av_nopts_value) {pkt.pts= A
V_rescale_q (oacc->coded_frame->pts, Oacc->time_base, audio_st->time_base);
} logi ("===========================");
Pkt.flags |= Av_pkt_flag_key;
Pkt.stream_index = audio_st->index;
Logi ("=====-=-=-=----=-=-=-=-=-=-=-");
Av_write_frame (OC, &PKT);
Av_free_packet (&PKT);
}}/** sampling, but not resamplecontext *RSC = NULL; RSC = Av_audio_resample_init (Oacc->channels, Acodecctx->channels, Oacc->sample_rate, ACodecCtx->
; sample_rate, Oacc->sample_fmt, ACODECCTX->SAMPLE_FMT, 16, 10, 0, 0.8);
Avfifobuffer *iofifo;
Iofifo = Av_fifo_alloc (192000*2);
int BS;
BS = Audio_resample (RSC, (short *) Resample_buffer, samples, out_size/(acodecctx->channels*2));
ret = Av_fifo_generic_write (Iofifo, (uint8_t *) Resample_buffer, bs*oacc->channels*2, NULL);
Audio_outbuf_size = oacc->frame_size * 2 * oacc->channels; while (Av_fifo_size (IOFIFO) >= audio_outbuf_size) {ret = Av_fifo_generic_read (Iofifo, Out_buf, Audio_ou
Tbuf_size, NULL);
ret = Avcodec_encode_audio (OACC, Audio_outbuf,audio_outbuf_size, (short *) out_buf);
Pkt.size = ret;
Pkt.stream_index= Audioindex;
Pkt.data= Audio_outbuf;
ret = Av_write_frame (OC, &PKT);
}}**/}} logi ("----------while");
Av_free_packet (&packet);
} logi ("Logi----------------18");
Av_write_trailer (OC);
Logi ("Logi----------------End"); for (i = 0; i < Oc-> nb_streams;
i++) {av_freep (&OC->STREAMS[I]->CODEC);
Av_freep (&oc->streams[i]);
} logi ("Logi----------------end 1");
Url_fclose (OC);
Av_free (OC);
Av_free (Ovframe);
Av_free (OUT_BUF);
Logi ("Logi----------------end 2");
Avcodec_close (VCODECCTX);
Avcodec_close (ACODECCTX);
Avformat_close_input (&ic);
Logi ("Logi----------------end 3");
return 1; }