How does one use the x264 C API to encode RBG images into H264 frames? I already created a sequence of RBG images, how can I now transform that sequence into a sequence of H264 frames? In particular, how do I encode this sequence of RGB images into a sequence of H264 frame consisting of a single initial H264 keyframe followed by dependent H264 frames?
相关问题
- Views base64 encoded blob in HTML with PHP
- Multiple sockets for clients to connect to
- How to get the background from multiple images by
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
FFmpeg 2.8.6 runnable example
Using FFpmeg as a wrapper for x264 is a good idea, as it exposes an uniform API for multiple encoders. So if you ever need to change formats, you can change just one parameter instead of learning a new API.
The example synthesizes and encodes some colorful frames generated by
generate_rgb
.Control of frame type (I, P, B) to have as few key-frames as possible (ideally just the first) is discussed here: https://stackoverflow.com/a/36412909/895245 As mentioned there, I do not recommend it for most applications.
The key-lines that do frame type control here are:
and:
We can then verify the frame type with:
as mentioned at: https://superuser.com/questions/885452/extracting-the-index-of-key-frames-from-a-video-using-ffmpeg
Preview of generated output.
Compile and run with:
Tested on Ubuntu 16.04. GitHub upstream.
First of all: check the x264.h file, it contains more or less the reference for each function and structure. The x264.c file you can find in the download contains a sample implementation. Most people say to base yourself on that one, but I find it rather complex for beginners, it is good as an example to fall back on however.
First you set up some parameters, of the type x264_param_t, a good site describing parameters is http://mewiki.project357.com/wiki/X264_Settings . Also take a look at the
x264_param_default_preset
function which allows you to target some functionality without needing to understand all of the (sometimes quite complex) parameters. Also usex264_param_apply_profile
afterwards (you'll probably want the "baseline" profile)This is some example setup from my code:
After this you can initialize the encoder as follows
X264 expects YUV420P data (I guess some others also, but that's the common one). You can use libswscale (from ffmpeg) to convert images to the right format. Initializing this is like this (i assume RGB data with 24bpp).
encoding is as simple as this then, for each frame do:
I hope this will get you going ;), I spent a long time on it myself to get started. X264 is an insanely strong but sometimes complex piece of software.
edit: When you use other parameters there will be delayed frames, this is not the case with my parameters (mostly due to the nolatency option). If this is the case, frame_size will sometimes be zero and you'll have to call
x264_encoder_encode
as long as the functionx264_encoder_delayed_frames
does not return 0. But for this functionality you should take a deeper peek into x264.c and x264.h .I've uploaded an example which generates raw yuv frames and then encodes them using x264. Full code can be found here: https://gist.github.com/roxlu/6453908