Example of how to write an MP4 File Using FFMPEG

Source: Internet
Author: User

Http://blog.csdn.net/eightdegree/article/details/7425811

After reading the example muxing. C provided by FFMPEG during this time, I slightly changed the source code to generate an MP4 file. The audio uses AAC encoding and the video uses H.264 encoding. The code is very simple and I will not explain it. The Code is as follows.

In the future, we will continue to write how to encode the audio and video data collected in DirectShow and generate MP4 files.

/* 5 seconds stream duration */# define stream_duration 5.0 # define stream_frame_rate 25/* 25 images/S */# define stream_nb_frames (INT) (stream_duration * stream_frame_rate )) # define stream_pix_fmt pix_fmt_yuv420p/* default pix_fmt */static int sws_flags = sws_bicubic; /*************************************** * *********************/* audio output */static float t, tincr, tincr2; static int16 _ T * samples; static uint8_t * audio_outbuf; static int audio_outbuf_size; static int bytes;/** add an audio output stream */static avstream * add_audio_stream (avformatcontext * oC, enum codecid codec_id) {avcodeccontext * C; avstream * st; ST = avformat_new_stream (OC, null); If (! St) {fprintf (stderr, "cocould not alloc stream \ n"); exit (1) ;}st-> id = 1; C = ST-> codec; c-> codec_id = codec_id; C-> codec_type = avmedia_type_audio;/* put sample parameters */C-> sample_fmt = av_sample_fmt_s16; C-> bit_rate = 64000; c-> sample_rate = 44100; C-> channels = 2; // some formats want stream headers to be separate if (oc-> oformat-> flags & avfmt_globalheader) c-> flags | = codec_flag_global_h Eader; return st;} static void open_audio (avformatcontext * oC, avstream * st) {avcodeccontext * C; avcodec * codec; C = ST-> codec; /* Find the Audio Encoder */codec = avcodec_find_encoder (c-> codec_id); If (! Codec) {fprintf (stderr, "codec not found \ n"); exit (1);}/* Open It */If (avcodec_open (C, codec) <0) {fprintf (stderr, "cocould not open codec \ n"); exit (1);}/* init Signal Generator */T = 0; tincr = 2 * m_pi * 110.0/C-> sample_rate; /* increment frequency by 110Hz per second */tincr2 = 2 * m_pi * 110.0/C-> sample_rate; audio_outbuf_size = 10000; audio_outbuf = (uint8_t *) AV _ Malloc (audio_outbuf_size ); /* uugly hack for PCM codecs (will be removed ASAP with new PCM support to compute the input frame size in samples */If (c-> frame_size <= 1) {audio_input_frame_size = audio_outbuf_size/C-> channels; Switch (St-> codec-> codec_id) {case when: audio_input_frame_size> = 1; break; DEFA Ult: Break ;}} else {audio_input_frame_size = C-> frame_size;} samples = (int16_t *) av_malloc (audio_input_frame_size * 2 * C-> channels );} /* prepare a 16 bit dummy audio frame of 'frame _ size' samples and 'nb _ channels 'channels */static void get_audio_frame (int16_t * samples, int frame_size, int nb_channels) {Int J, I, V; int16_t * q; q = samples; For (j = 0; j <frame_size; j ++) {v = (INT) (sin (T) * 10000); for (I = 0; I <nb_channels; I ++) * q ++ = V; t + = tincr; tincr + = tincr2 ;}} static void write_audio_frame (avformatcontext * oC, avstream * st) {avcodeccontext * C; avpacket Pkt; av_init_packet (& Pkt); C = ST-> codec; get_audio_frame (samples, example, c-> channels); Pkt. size = avcodec_encode_audio (C, audio_outbuf, audio_outbuf_size, samples); If (c-> coded_frame & C-> coded_f Rame-> PTS! = Av_nopts_value) Pkt. PTS = av_rescale_q (c-> coded_frame-> pts, C-> time_base, St-> time_base); Pkt. flags | = av_pkt_flag_key; Pkt. stream_index = ST-> index; Pkt. data = audio_outbuf;/* write the compressed frame in the media file */If (av_interleaved_write_frame (OC, & Pkt )! = 0) {fprintf (stderr, "error while writing audio frame \ n"); exit (1) ;}} static void close_audio (avformatcontext * oC, avstream * st) {avcodec_close (St-> codec); av_free (samples); av_free (audio_outbuf );} /*************************************** * *********************/* video output */static avframe * picture, * tmp_picture; static uint8_t * video_outbuf; static int frame_count, video_outbuf_size ;/ * Add a video output stream */static avstream * add_video_stream (avformatcontext * oC, Enum codecid codec_id) {avcodeccontext * C; avstream * st; avcodec * codec; ST = avformat_new_stream (OC, null); If (! St) {fprintf (stderr, "cocould not alloc stream \ n"); exit (1);} c = ST-> codec; /* find the video encoder */codec = avcodec_find_encoder (codec_id); If (! Codec) {fprintf (stderr, "codec not found \ n"); exit (1);} avcodec_get_context_defaults3 (C, codec); C-> codec_id = codec_id; /* put sample parameters */C-> bit_rate =/* 400000 */3000000; /* resolution must be a multiple of two */C-> width =/* 352 */640; C-> Height =/* 288 */480;/* time base: this is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented. for fixed-F PS content, timebase shocould be 1/framerate and timestamp increments shocould be identically 1. */C-> time_base.den = stream_frame_rate; C-> time_base.num = 1; C-> gop_size = 12; /* emit one intra frame every twelve frames at most */C-> pix_fmt = stream_pix_fmt; If (c-> codec_id = codec_id_mpeg2video) {/* just for testing, we also add B frames */C-> max_ B _frames = 2;} If (c-> codec_id = codec_id_mpeg 1 Video) {/* needed to avoid using macroblocks in which some coeffs overflow. this does not happen with normal video, it just happens here as the motion of the chroma plane does not match the luma plane. */C-> mb_demo-= 2;} // some formats want stream headers to be separate if (oc-> oformat-> flags & avfmt_globalheader) C-> flags | = codec_flag_global_header; return st;} static avframe * alloc_pictu Re (Enum pixelformat pix_fmt, int width, int height) {avframe * picture; uint8_t * picture_buf; int size; picture = avcodec_alloc_frame (); If (! Picture) return NULL; size = avpicture_get_size (pix_fmt, width, height); picture_buf = (uint8_t *) av_malloc (size); If (! Picture_buf) {av_free (picture); return NULL;} avpicture_fill (avpicture *) picture, picture_buf, pix_fmt, width, height); Return picture ;} static void open_video (avformatcontext * oC, avstream * st) {avcodec * codec; avcodeccontext * C; C = ST-> codec; /* find the video encoder */codec = avcodec_find_encoder (c-> codec_id); If (! Codec) {fprintf (stderr, "codec not found \ n"); exit (1);}/* Open the codec */If (avcodec_open (C, codec) <0) {fprintf (stderr, "cocould not open codec \ n"); exit (1) ;}video_outbuf = NULL; If (! (Oc-> oformat-> flags & avfmt_rawpicture) {/* allocate output buffer * // * XXX: API change will be done * // * buffers passed into LAV * can be allocated any way you prefer, as long as they're aligned enough for the architecture, and they're freed appropriately (such as using av_free for buffers allocated with av_malloc) */percent = 200000; video_outbuf = (uint8_t *) av_malloc (video_outbuf _ Size);}/* allocate the encoded raw picture */picture = alloc_picture (c-> pix_fmt, C-> width, C-> height); If (! Picture) {fprintf (stderr, "cocould not allocate picture \ n"); exit (1) ;}/ * If the output format is not yuv420p, then a temporary yuv420p picture is needed too. it is then converted to the required output format */tmp_picture = NULL; If (c-> pix_fmt! = Pix_fmt_yuv420p) {tmp_picture = alloc_picture (pix_fmt_yuv420p, C-> width, C-> height); If (! Tmp_picture) {fprintf (stderr, "cocould not allocate temporary picture \ n"); exit (1 );}}} /* prepare a dummy image */static void fill_yuv_image (avframe * pict, int frame_index, int width, int height) {int x, y, I; I = frame_index; /* y */For (y = 0; y 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.