Tuesday, August 09, 2011

FFmpeg Tutorial

Introduction

In this post, we will see how you can use FFmpeg libraries in your application and decode movie files. I am taking dranger's first tutorial as the source and I will build it using Visual C++ 6.0. You can find the source code here.

Background

If you do not have the ffmpeg libraries with you, you will need download the source code and build it. You will find a step by step tutorial for building the libraries here

Code

Opening the video file

First, we initialize ffmpeg by calling 'av_register_all()' at the starting of the program. This registers all supported formats and codecs.
 
Next we open the video file using av_open_input_file(). The first parameter is the pointer to the 'AVFormatContext' which we will use in our program to refer to the video file. The second parameter is the name of the file to be opened. The last three parameters are for file format, buffer size, and format options. By setting it to NULL and 0, libavformat detects and fills values on it own.
 
We dump information about the input file using 'dump_format()'

// Register all formats and codecs
av_register_all();
 
// Open video file
if(av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL)!=0)
return -1; // Couldn't open file

// Retrieve stream information
if(av_find_stream_info(pFormatCtx) > 0)
return -1; // Couldn't find stream information

// Dump information about file onto standard error
dump_format(pFormatCtx, 0, argv[1], 0);

Opening the decoder

We loop through the list of streams present in the input file to locate the 'video' stream present in it. The file can typically contain one or more audio/video streams in it. Once a video stream is located, we extract the codec type of the video stream. This codec type will be used to initialize the decode as shown below.
 

videoStream=-1;
for(i=0; inb_streams; i++)
  videoStream=i;
  break;
}
if(videoStream==-1)
return -1;
pCodecCtx=pFormatCtx->streams[videoStream]->codec;

Read more: Codeproject
QR: FFmpeg-Tutorial

Posted via email from Jasper-Net