The simplest Video Encoder Based on FFMPEG (YUV encoding: H.264)

Source: Internet
Author: User

This article introduces a simple Video Encoder Based on FFMPEG. The YUV420P pixel data is encoded as H.264 compressed data. The code of the encoder is very simple, but every line of code is very important. It is suitable for a good study. After the code is clarified, the encoding process of FFMPEG is basically clarified. At present, although I have already completed the program, I still haven't fully understood it in some places. I need to continue to explore and then add content.

This program uses the latest version of the Class Library (the Compilation Time is 2014.5.6), and the development platform is VC2010. All the configurations have been completed. You only need to run them.

The following code is directly used:

 

/** The Simplest FFmpeg-Based Video Encoder * Simplest FFmpeg Video Encoder ** leixiao Lei Xiaohua * leixiaohua1020@126.com * China Media University/Digital TV technology * Communication University of China/Digital TV Technology * http://blog.csdn.net/leixiaohua1020 ** this program implements the YUV pixel data encoding for video streams (H264, MPEG2, VP8, etc ). * Is the simplest tutorial on FFmpeg video encoding. * By studying this example, you can understand the FFmpeg encoding process. * This software encode YUV420P data to H. 264 bitstream. * It's the simplest video encoding software based on FFmpeg. * Suitable for beginner of FFmpeg */# include "stdafx. h "extern" C "{# include" libavcodec \ avcodec. h "# include" libavformat \ avformat. h "# include" libswscale \ swscale. h "}; int _ tmain (int argc, _ TCHAR * argv []) {AVFormatContext * pFormatCtx; AVOutputFormat * fmt; AVStream * video_st; AVCodecContext * pCo DecCtx; AVCodec * pCodec; uint8_t * picture_buf; AVFrame * picture; int size; FILE * in_file = fopen ("src0109480x272.yuv", "rb "); // video YUV source file int in_w = 480, in_h = 272; // width and height int framenum = 50; const char * out_file = "src01.h264 "; // output file path av_register_all (); // method 1. use several functions in combination: pFormatCtx = avformat_alloc_context (); // guess the format fmt = av_guess_format (NULL, out_file, NULL); pFormatCtx-> oformat = fmt; // method 2. more automated // avformat_alloc_output_co Ntext2 (& pFormatCtx, NULL, NULL, out_file); // fmt = pFormatCtx-> oformat; // note the output path if (avio_open (& pFormatCtx-> pb, out_file, AVIO_FLAG_READ_WRITE) <0) {printf ("failed to open the output file"); return-1;} video_st = av_new_stream (pFormatCtx, 0); if (video_st = NULL) {return-1 ;} pCodecCtx = video_st-> codec; pCodecCtx-> codec_id = fmt-> video_codec; pCodecCtx-> codec_type = AVMEDIA_TYPE_VIDEO; pCodecCtx-pix> _fmt = Beijing; pCodecC Tx-> width = in_w; pCodecCtx-> height = in_h; pCodecCtx-> time_base.num = 1; pCodecCtx-> time_base.den = 25; pCodecCtx-> bit_rate = 400000; pCodecCtx-> gop_size = 250; // H264 // pCodecCtx-> me_range = 16; // pCodecCtx-> max_qdiff = 4; pCodecCtx-> qmin = 10; pCodecCtx-> qmax = 51; // pCodecCtx-> qcompress = 0.6; // output format information av_dump_format (pFormatCtx, 0, out_file, 1 ); pCodec = avcodec_find_encoder (pCodecCtx-> codec_id); if (! PCodec) {printf ("no suitable encoder found! \ N "); return-1;} if (avcodec_open2 (pCodecCtx, pCodec, NULL) <0) {printf (" encoder opening failed! \ N "); return-1;} picture = avcodec_alloc_frame (); size = avpicture_get_size (pCodecCtx-> pix_fmt, pCodecCtx-> width, pCodecCtx-> height ); picture_buf = (uint8_t *) av_malloc (size); watermark (AVPicture *) picture, picture_buf, pCodecCtx-> pix_fmt, pCodecCtx-> width, pCodecCtx-> height ); // write the file header avformat_write_header (pFormatCtx, NULL); AVPacket pkt; int y_size = pCodecCtx-> width * pCodecCtx-> height; av_new_packet (& Pkt, y_size * 3); for (int I = 0; I <framenum; I ++) {// read YUVif (fread (picture_buf, 1, y_size * 3/2, in_file) <0) {printf ("file read error \ n"); return-1;} else if (feof (in_file) {break ;} picture-> data [0] = picture_buf; // brightness Ypicture-> data [1] = picture_buf + y_size; // U picture-> data [2] = picture_buf + y_size * 5/4; // V // PTSpicture-> pts = I; int got_picture = 0; // encode int ret = avcodec_encode_video2 (pCodecCtx, & pkt, picture, & got _ Picture); if (ret <0) {printf ("Encoding Error! \ N "); return-1;} if (got_picture = 1) {printf (" frame % d after successful encoding! \ N ", I); pkt. stream_index = video_st-> index; ret = av_write_frame (pFormatCtx, & pkt); av_free_packet (& pkt) ;}// write the end Of the file av_write_trailer (pFormatCtx ); // clear if (video_st) {avcodec_close (video_st-> codec); av_free (picture); av_free (temperature);} avio_close (pFormatCtx-> pb); avformat_free_context (pFormatCtx ); fclose (in_file); return 0 ;}

Software running (limited by the file size, the number of original YUV frames is very small ):


The YUV sequence Before encoding:


Encoded H.264 code stream:


:

Http://download.csdn.net/detail/leixiaohua1020/7324115

SourceForge Project address:

Https://sourceforge.net/projects/simplestffmpegvideoencoder/

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.