Then the previous article
http://blog.csdn.net/openswc/article/details/51597755
Second, the ffmpeg will be YUV encoded as. H264
1. Download and install FFmpeg
./configure--enable-libx264--ENABLE-GPL--enable-shared
Make
Make install
2. Use the installed ffmpeg with the command to encode YUV as. H264
Ffmpeg-s 480x272-i ds_480x272.yuv-r 25-vcodec libx264 ds2.h264
YUV File:
Https://github.com/leixiaohua1020/simplest_ffmpeg_video_encoder/blob/master/ds_480x272.yuv
3. Call the FFmpeg interface with the C language code to encode YUV as. H264
Compile command: Before compiling, execute the next ldconfig command to update the LIBX264 library to the system library path;
GCC simplest_ffmpeg_video_encoder_pure.cpp-g-o sffmpegencoder-i/usr/local/include-l/usr/local/lib-lavformat- Lavcodec-lavutil
The code is as follows
Simplest_ffmpeg_video_encoder_pure.cpp
/* * simplest FFmpeg Video Encoder Pure * * Lei Xiaohua * leixiaohua1020@126.com * communication University of China /Digital TV Technology * http://blog.csdn.net/leixiaohua1020 * * * Software encode yuv420p data to video Bitstre
AM * (Such as H. h.265, VP8, MPEG2 etc). * It only uses libavcodec to encode video (without Libavformat) * It's the simplest video encoding software based on FFMP
eg. * Suitable for beginner of FFmpeg * * * MODIFY:OPENSWC * Build: * gcc simplest_ffmpeg_video_encoder_pure.cpp-g-o Sffmpegencoder-i/usr/local/include-l/usr/local/lib-lavformat-lavcodec-lavutil * before use sffmpegencoder:you nee D to excute the following command:su root;
Ldconfig; * Or there is a error: *./sffmpegencoder:error while loading shared libraries:libavcodec.so.57:cannot open shared OB ject file:no such file or directory */#include <stdio.h> #define __stdc_constant_macros #ifdef _WIN32//windo ws extern "C" {#include "libavutil/opT.h "#include" libavcodec/avcodec.h "#include" libavutil/imgutils.h "}; #else//linux #ifdef __cplusplus extern "C" {#endif #include <libavutil/opt.h> #include <libavcodec/avcodec.
h> #include <libavutil/imgutils.h> #ifdef __cplusplus}; #endif #endif//test Different codec #define TEST_H264 1 #define TEST_HEVC 0 int main (int argc, char* argv[]) {Avco
Dec *pcodec;
Avcodeccontext *pcodecctx= NULL;
int I, RET, got_output;
FILE *fp_in;
FILE *fp_out;
Avframe *pframe;
Avpacket PKT;
int y_size;
int framecnt=0; Char filename_in[]= "..
/ds_480x272.yuv ";
#if TEST_HEVC avcodecid CODEC_ID=AV_CODEC_ID_HEVC;
Char filename_out[]= "DS.HEVC";
#else avcodecid codec_id=av_codec_id_h264;
Char filename_out[]= "ds.h264";
#endif int in_w=480,in_h=272;
int framenum=100;
Avcodec_register_all ();
Pcodec = Avcodec_find_encoder (codec_id);
if (!pcodec) {printf ("Codec not found\n");
return-1; } Pcodecctx = Avcodec_alloc_CONTEXT3 (PCODEC);
if (!pcodecctx) {printf ("Could not allocate video codec context\n");
return-1;
} pcodecctx->bit_rate = 400000;
Pcodecctx->width = In_w;
Pcodecctx->height = In_h;
pcodecctx->time_base.num=1;
pcodecctx->time_base.den=25;
Pcodecctx->gop_size = 10;
Pcodecctx->max_b_frames = 1;
PCODECCTX->PIX_FMT = av_pix_fmt_yuv420p;
if (codec_id = = av_codec_id_h264) av_opt_set (pcodecctx->priv_data, "preset", "slow", 0);
if (Avcodec_open2 (Pcodecctx, Pcodec, NULL) < 0) {printf ("Could not open codec\n");
return-1;
} pframe = Av_frame_alloc ();
if (!pframe) {printf ("Could not allocate video frame\n");
return-1;
} Pframe->format = pcodecctx->pix_fmt;
Pframe->width = pcodecctx->width;
Pframe->height = pcodecctx->height; ret = Av_image_alloc (Pframe->data, Pframe->linesize, Pcodecctx->width, PCOdecctx->height, PCODECCTX->PIX_FMT, 16);
if (Ret < 0) {printf ("Could not allocate raw picture buffer\n");
return-1;
}//input raw Data fp_in = fopen (filename_in, "RB");
if (!fp_in) {printf ("Could not open%s\n", filename_in);
return-1;
}//output bitstream fp_out = fopen (Filename_out, "WB");
if (!fp_out) {printf ("Could not open%s\n", filename_out);
return-1;
} y_size = Pcodecctx->width * pcodecctx->height;
Encode for (i = 0; i < Framenum; i++) {av_init_packet (&PKT); Pkt.data = NULL;
Packet data is allocated by the encoder pkt.size = 0; Read Raw YUV Data if (fread (pframe->data[0],1,y_size,fp_in) <= 0| | Y fread (pframe->data[1],1,y_size/4,fp_in) <= 0| |
U fread (pframe->data[2],1,y_size/4,fp_in) <= 0) {//V return-1;
}else if (feof (fp_in)) {break;
} pframe->pts = i; /* Encode the image */ret = Avcodec_encode_video2 (Pcodecctx, &pkt, Pframe, &got_output);
if (Ret < 0) {printf ("Error encoding frame\n");
return-1;
} if (Got_output) {printf ("Succeed to encode frame:%5d\tsize:%5d\n", framecnt,pkt.size);
framecnt++;
Fwrite (Pkt.data, 1, pkt.size, fp_out);
Av_free_packet (&PKT); }}//flush Encoder for (got_output = 1; got_output; i++) {ret = Avcodec_encode_video2 (Pcodecctx, &am
P;PKT, NULL, &got_output);
if (Ret < 0) {printf ("Error encoding frame\n");
return-1;
} if (Got_output) {printf ("Flush encoder:succeed to encode 1 frame!\tsize:%5d\n", pkt.size);
Fwrite (Pkt.data, 1, pkt.size, fp_out);
Av_free_packet (&PKT);
}} fclose (Fp_out);
Avcodec_close (PCODECCTX);
Av_free (PCODECCTX);
Av_freep (&pframe->data[0]); Av_frame_free (&pframe);
return 0;
}
Third, if you play the. h264 file
Use VLC media player to play YUV converted. h264 video files;
VLC Media Player website
http://www.videolan.org