[FFmpeg-devel] [ffmpeg-devel] H264 encoding with libavcodec/libavformat

Quy Pham Sy phamsyquybk
Fri Mar 11 20:30:05 CET 2011

Hi all,

I want to encode a video file with H264 from various type of input file. I
know that i can use ffmpeg.exe to convert files, but i need to be able to
handle the muxed output video/audio stream of encoding processes so I will
use libavcodec and libavformat directly. I look at the example in ffmpeg's
source code (api-example.c) as a start, but these examples use MPEG1. As I
understand, in those examples, video stream is decompressed frame by frame
which is can not be applied properly for H264 due to the fact that h264 uses
referencing information between frames to encode (am i right?).

*Can you point me some examples of using libavcode/libavformat with H264? *

I did change the some lines in api-example in order to use H264 as following

encoderCodec = avcodec_find_encoder(CODEC_ID_H264);

//............ allocate codec context ........


/* put sample parameters */
encoderContext->bit_rate = 400000;

/* resolution must be a multiple of two */
encoderContext->width = 352;
encoderContext->height = 288;

/* frames per second: 25 frames per second */
encoderContext->time_base.num = 1;
encoderContext->time_base.den = 25;
encoderContext->me_range = 16;
encoderContext->max_qdiff = 4;
encoderContext->qmin = 10;
encoderContext->qmax = 51;
encoderContext->qcompress = 0.6;
encoderContext->gop_size = 10; /* emit one intra frame every ten frames */
encoderContext->pix_fmt = PIX_FMT_YUV420P;

//.... continue with original code...

the quality of the output file is not as good as MPEG1. *Is that because the
encoder options?
very thank,


More information about the ffmpeg-devel mailing list