Multimedia programming for FFmpeg and SDL (i) Making screen videos

Source: Internet
Author: User

Movie files have a lot of basic components. First, the file itself is called a container container, and the type of the container determines where the information is stored in the file. Avi and QuickTime are examples of containers. Then you have a set of streams, for example, you often have an audio stream and a video stream. (A stream is just an imaginary word used to denote a series of data elements that are concatenated through time.) The data element in the stream is called the frame frame. Each stream is encoded by a different encoder. The codec describes how the actual data is encoded coded and decoded decoded, so its name is codec. DivX and MP3 are examples of codecs. What is then read out of the stream is called the packet (Packets). The package is a piece of data that can be decoded into the original frame that we finally manipulate in the application. For our purposes, each package contains a complete frame or, for audio, a full frame of many formats.

Basically, it's easy to process video and audio streams:

10 Open the video stream from the Video.avi file Video_stream

20 reading packets from the video stream into the frame

30 If this frame is not complete, skip to 20

40 do some action on this frame

50 Jump back to 20

It is fairly easy to use ffmpeg in this program to handle multiple media, although many programs may be very complex to manipulate frames. So in this tutorial, we'll open a file to read the video stream inside, and our action on the frame will be to write this frame into a ppm file. Open File

First, let's look at how we open a file. By FFmpeg, you must initialize the library first. (Note that <ffmpeg/avcodec.h> and <ffmpeg/avformat.h> must be replaced in some systems)

#include <avcodec.h>

#include <avformat.h>

...

int main (int argc, Charg *argv[]) {

Av_register_all ();

All file formats and codec libraries are registered here, so they will be automatically used in the appropriate format files that are opened. Note that you only need to call Av_register_all () once, so we are in the main letter

Number in main () to invoke it. If you like, you can register only specific formats and codecs, but usually you don't need to do this.

Now we can actually open the file:

Avformatcontext *pformatctx;

Open Video File

if (Av_open_input_file (&pformatctx, argv[1], NULL, 0, NULL)!=0)

return-1; Couldn ' t open file

We get the filename with the first argument. This function reads the head of the file and saves the information to the AVFORMATCONTEXT structure we give it. The last three parameters are used to specify a special file format.

Buffer size and format parameters, but if you set them to null or 0,libavformat will automatically detect the parameters.

This function only detects the header of the file, so we then need to check the flow information in the file:

Retrieve Stream Information

if (Av_find_stream_info (PFORMATCTX) <0)

return-1; Couldn ' t find stream information

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.