ffmpeg tutorial1




## An ffmpeg and SDL Tutorial

Page 1 2 3 4 5 6 7 End Prev Home Next &nbsp_place_holder;

Text version

## Tutorial 01: Making Screencaps

Code: tutorial01.c

### Overview

Movie files have a few basic components. First, the file itself is called a
**container**, and the type of container determines where the information in
the file goes. Examples of containers are AVI and Quicktime. Next, you have a
bunch of **streams**; for example, you usually have an audio stream and a
video stream. (A "stream" is just a fancy word for "a succession of data
elements made available over time".) The data elements in a stream are called
**frames**. Each stream is encoded by a different kind of **codec**. The codec
defines how the actual data is COded and DECoded - hence the name CODEC.
Examples of codecs are DivX and MP3. **Packets** are then read from the
stream. Packets are pieces of data that can contain bits of data that are
decoded into raw frames that we can finally manipulate for our application.
For our purposes, each packet contains complete frames, or multiple frames in
the case of audio.

At its very basic level, dealing with video and audio streams is very easy:

    
    
    10 OPEN video_stream FROM video.avi
    20 READ packet FROM video_stream INTO frame
    30 IF frame NOT COMPLETE GOTO 20
    40 DO SOMETHING WITH frame
    50 GOTO 20
    

Handling multimedia with ffmpeg is pretty much as simple as this program,
although some programs might have a very complex "DO SOMETHING" step. So in
this tutorial, we're going to open a file, read from the video stream inside
it, and our DO SOMETHING is going to be writing the frame to a PPM file.

### Opening the File

First, let's see how we open a file in the first place. With ffmpeg, you have
to first initialize the library.

    
    
    #include <libavcodec/avcodec.h>
    #include <libavformat/avformat.h>
    #include <ffmpeg/swscale.h>
    ...
    int main(int argc, charg *argv[]) {
    av_register_all();
    

This registers all available file formats and codecs with the library so they
will be used automatically when a file with the corresponding format/codec is
opened. Note that you only need to call av_register_all() once, so we do it
here in main(). If you like, it's possible to register only certain individual
file formats and codecs, but there's usually no reason why you would have to
do that.

Now we can actually open the file:

    
    
    AVFormatContext *pFormatCtx = NULL;
    
    // Open video file
    if(avformat_open_input(&pFormatCtx;, argv[1], NULL, 0, NULL)!=0)
      return -1; // Couldn't open file
    

We get our filename from the first argument. This function reads the file
header and stores information about the file format in the AVFormatContext
structure we have given it. The last three arguments are used to specify the
file format, buffer size, and format options, but by setting this to NULL or
0, libavformat will auto-detect these.

This function only looks at the header, so next we need to check out the
stream information in the file.:

    
    
    // Retrieve stream information
    if(avformat_find_stream_info(pFormatCtx, NULL)<0)
      return -1; // Couldn't find stream information
    

This function populates `pFormatCtx->streams` with the proper information. We
introduce a handy debugging function to show us what's inside:

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

Now `pFormatCtx->streams` is just an array of pointers, of size
`pFormatCtx->nb_streams`, so let's walk through it until we find a video
stream.

    
    
    int i;
    AVCodecContext *pCodecCtxOrig = NULL;
    AVCodecContext *pCodecCtx = NULL;
    
    // Find the first video stream
    videoStream=-1;
    for(i=0; i<pFormatCtx->nb_streams; i++)
      if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
        videoStream=i;
        break;
      }
    if(videoStream==-1)
      return -1; // Didn't find a video stream
    
    // Get a pointer to the codec context for the video stream
    pCodecCtx=pFormatCtx->streams[videoStream]->codec;
    

The stream's information about the codec is in what we call the "codec
context." This contains all the information about the codec that the stream is
using, and now we have a pointer to it. But we still have to find the actual
codec and open it:

    
    
    AVCodec *pCodec = NULL;
    
    // Find the decoder for the video stream
    pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
    if(pCodec==NULL) {
      fprintf(stderr, "Unsupported codec!\n");
      return -1; // Codec not found
    }
    // Copy context
    pCodecCtx = avcodec_alloc_context3(pCodec);
    if(avcodec_copy_context(pCodecCtx, pCodecCtxOrig) != 0) {
      fprintf(stderr, "Couldn't copy codec context");
      return -1; // Error copying codec context
    }
    // Open codec
    if(avcodec_open2(pCodecCtx, pCodec)<0)
      return -1; // Could not open codec
    

Note that we must not use the AVCodecContext from the video stream directly!
So we have to use avcodec_copy_context() to copy the context to a new location
(after allocating memory for it, of course).

### Storing the Data

Now we need a place to actually store the frame:

    
    
    AVFrame *pFrame = NULL;
    
    // Allocate video frame
    pFrame=av_frame_alloc();
    

Since we're planning to output PPM files, which are stored in 24-bit RGB,
we're going to have to convert our frame from its native format to RGB. ffmpeg
will do these conversions for us. For most projects (including ours) we're
going to want to convert our initial frame to a specific format. Let's
allocate a frame for the converted frame now.

    
    
    // Allocate an AVFrame structure
    pFrameRGB=av_frame_alloc();
    if(pFrameRGB==NULL)
      return -1;
    

Even though we've allocated the frame, we still need a place to put the raw
data when we convert it. We use `avpicture_get_size` to get the size we need,
and allocate the space manually:

    
    
    uint8_t *buffer = NULL;
    int numBytes;
    // Determine required buffer size and allocate buffer
    numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,
                                pCodecCtx->height);
    buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
    

`av_malloc` is ffmpeg's malloc that is just a simple wrapper around malloc
that makes sure the memory addresses are aligned and such. It will _not_
protect you from memory leaks, double freeing, or other malloc problems.

Now we use avpicture_fill to associate the frame with our newly allocated
buffer. About the AVPicture cast: the AVPicture struct is a subset of the
AVFrame struct - the beginning of the AVFrame struct is identical to the
AVPicture struct.

    
    
    // Assign appropriate parts of buffer to image planes in pFrameRGB
    // Note that pFrameRGB is an AVFrame, but AVFrame is a superset
    // of AVPicture
    avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,
                    pCodecCtx->width, pCodecCtx->height);
    

Finally! Now we're ready to read from the stream!

### Reading the Data

What we're going to do is read through the entire video stream by reading in
the packet, decoding it into our frame, and once our frame is complete, we
will convert and save it.

    
    
    struct SwsContext *sws_ctx = NULL;
    int frameFinished;
    AVPacket packet;
    // initialize SWS context for software scaling
    sws_ctx = sws_getContext(pCodecCtx->width,
        pCodecCtx->height,
        pCodecCtx->pix_fmt,
        pCodecCtx->width,
        pCodecCtx->height,
        PIX_FMT_RGB24,
        SWS_BILINEAR,
        NULL,
        NULL,
        NULL
        );
    
    i=0;
    while(av_read_frame(pFormatCtx, &packet;)>=0) {
      // Is this a packet from the video stream?
      if(packet.stream_index==videoStream) {
    	// Decode video frame
        avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished;, &packet;);
        
        // Did we get a video frame?
        if(frameFinished) {
        // Convert the image from its native format to RGB
            sws_scale(sws_ctx, (uint8_t const * const *)pFrame->data,
    		  pFrame->linesize, 0, pCodecCtx->height,
    		  pFrameRGB->data, pFrameRGB->linesize);
    	
            // Save the frame to disk
            if(++i<=5)
              SaveFrame(pFrameRGB, pCodecCtx->width, 
                        pCodecCtx->height, i);
        }
      }
        
      // Free the packet that was allocated by av_read_frame
      av_free_packet(&packet;);
    }
    

**A note on packets**

Technically a packet can contain partial frames or other bits of data, but
ffmpeg's parser ensures that the packets we get contain either complete or
multiple frames.

The process, again, is simple: `av_read_frame()` reads in a packet and stores
it in the `AVPacket` struct. Note that we've only allocated the packet
structure - ffmpeg allocates the internal data for us, which is pointed to by
`packet.data`. This is freed by the `av_free_packet()` later.
`avcodec_decode_video()` converts the packet to a frame for us. However, we
might not have all the information we need for a frame after decoding a
packet, so `avcodec_decode_video()` sets frameFinished for us when we have the
next frame. Finally, we use `sws_scale()` to convert from the native format
(`pCodecCtx->pix_fmt`) to RGB. Remember that you can cast an AVFrame pointer
to an AVPicture pointer. Finally, we pass the frame and height and width
information to our SaveFrame function.

Now all we need to do is make the SaveFrame function to write the RGB
information to a file in PPM format. We're going to be kind of sketchy on the
PPM format itself; trust us, it works.

    
    
    void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame) {
      FILE *pFile;
      char szFilename[32];
      int  y;
      
      // Open file
      sprintf(szFilename, "frame%d.ppm", iFrame);
      pFile=fopen(szFilename, "wb");
      if(pFile==NULL)
        return;
      
      // Write header
      fprintf(pFile, "P6\n%d %d\n255\n", width, height);
      
      // Write pixel data
      for(y=0; y<height; y++)
        fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile);
      
      // Close file
      fclose(pFile);
    }
    

We do a bit of standard file opening, etc., and then write the RGB data. We
write the file one line at a time. A PPM file is simply a file that has RGB
information laid out in a long string. If you know HTML colors, it would be
like laying out the color of each pixel end to end like `#ff0000#ff0000`....
would be a red screen. (It's stored in binary and without the separator, but
you get the idea.) The header indicated how wide and tall the image is, and
the max size of the RGB values.

Now, going back to our main() function. Once we're done reading from the video
stream, we just have to clean everything up:

    
    
    // Free the RGB image
    av_free(buffer);
    av_free(pFrameRGB);
    
    // Free the YUV frame
    av_free(pFrame);
    
    // Close the codecs
    avcodec_close(pCodecCtx);
    avcodec_close(pCodecCtxOrig);
    
    // Close the video file
    avformat_close_input(&pFormatCtx;);
    
    return 0;
    

You'll notice we use av_free for the memory we allocated with
avcode_alloc_frame and av_malloc.

That's it for the code! Now, if you're on Linux or a similar platform, you'll
run:

    
    
    gcc -o tutorial01 tutorial01.c -lavutil -lavformat -lavcodec -lz -lavutil -lm
    

If you have an older version of ffmpeg, you may need to drop -lavutil:

    
    
    gcc -o tutorial01 tutorial01.c -lavformat -lavcodec -lz -lm
    

Most image programs should be able to open PPM files. Test it on some movie
files.

_**>>** Tutorial 2: Outputting to the Screen_

* * *

Function Reference

Data Reference

email:

dranger at gmail dot com

Back to dranger.com

This work is licensed under the Creative Commons Attribution-Share Alike 2.5
License. To view a copy of this license, visit
http://creativecommons.org/licenses/by-sa/2.5/ or send a letter to Creative
Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.

  
Code examples are based off of FFplay, Copyright (c) 2003 Fabrice Bellard, and
a tutorial by Martin Bohme.




//
// tutorial01.c
// Code based on a tutorial by Martin Bohme (boehme@inb.uni-luebeckREMOVETHIS.de)
// Tested on Gentoo, CVS version 5/01/07 compiled with GCC 4.1.1
// With updates from https://github.com/chelyaev/ffmpeg-tutorial
// Updates tested on:
// LAVC 54.59.100, LAVF 54.29.104, LSWS 2.1.101 
// on GCC 4.7.2 in Debian February 2015

// A small sample program that shows how to use libavformat and libavcodec to
// read video from a file.
//
// Use
//
// gcc -o tutorial01 tutorial01.c -lavformat -lavcodec -lswscale -lz
//
// to build (assuming libavformat and libavcodec are correctly installed
// your system).
//
// Run using
//
// tutorial01 myvideofile.mpg
//
// to write the first five frames from "myvideofile.mpg" to disk in PPM
// format.
#include <stdio.h>
#include <stdlib.h>

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/mem.h>
// compatibility with newer API
//#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
#define av_frame_alloc avcodec_alloc_frame
#define av_frame_free avcodec_free_frame
//#endif

void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame) {
  FILE *pFile;
  char szFilename[32];
  int  y;
  
  // Open file
  sprintf(szFilename, "frame%d.ppm", iFrame);
  pFile=fopen(szFilename, "wb");
  if(pFile==NULL)
    return;
  
  // Write header
  fprintf(pFile, "P6\n%d %d\n255\n", width, height);
  
  // Write pixel data
  for(y=0; y<height; y++)
    fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile);
  
  // Close file
  fclose(pFile);
}

int main(int argc, char *argv[]) {
  // Initalizing these to NULL prevents segfaults!
  AVFormatContext   *pFormatCtx = NULL;
  int               i, videoStream;
  AVCodecContext    *pCodecCtxOrig = NULL;
  AVCodecContext    *pCodecCtx = NULL;
  AVCodec           *pCodec = NULL;
  AVFrame           *pFrame = NULL;
  AVFrame           *pFrameRGB = NULL;
  AVPacket          packet;
  int               frameFinished;
  int               numBytes;
  uint8_t           *buffer = NULL;
  struct SwsContext *sws_ctx = NULL;

  if(argc < 2) {
    printf("Please provide a movie file\n");
    return -1;
  }
  // Register all formats and codecs
  //这一步应该会关键,会注册ffmpeg支持的所有的编解码器
  av_register_all();
  
  // Open video file
  //调用ffmpeg特定的打开函数,会得到一个格式上下文,在格式上下文中将获得文件的头信息,
  //文件大小,编码格式等重要信息。
  if(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL)!=0)
    return -1; // Couldn't open file
  
  // Retrieve stream information
  //获取文件中的流信息(音频流,视频流)并保存的格式上下文中。
  if(avformat_find_stream_info(pFormatCtx, NULL)<0)
    return -1; // Couldn't find stream information
  
  // Dump information about file onto standard error
  //这里应该是显示一下格式上下文的信息吧。
  av_dump_format(pFormatCtx, 0, argv[1], 0);
  
  // Find the first video stream
  //格式上下文中保存有该文件中有几个流数据,以及每一个流的类型等。
  //这里就是找出其中的第一个视频流。
  videoStream=-1;
  for(i=0; i<pFormatCtx->nb_streams; i++)
    if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
      videoStream=i;
      break;
    }
  if(videoStream==-1)
    return -1; // Didn't find a video stream
  
  // Get a pointer to the codec context for the video stream
  //获取该视频流的编码器上下文,编码器上下文中包含了跟编码相关的一些信息。
  
  pCodecCtxOrig=pFormatCtx->streams[videoStream]->codec;
  // Find the decoder for the video stream
  //根据流的编码id找到相应的解码器。
  pCodec=avcodec_find_decoder(pCodecCtxOrig->codec_id);
  if(pCodec==NULL) {
    fprintf(stderr, "Unsupported codec!\n");
    return -1; // Codec not found
  }
  // Copy context
  //分配一下编码器上下文。
  pCodecCtx = avcodec_alloc_context3(pCodec);
  //复制一个由ffmpeg内部维护的编码器上下文,就是复制一下而已。
  if(avcodec_copy_context(pCodecCtx, pCodecCtxOrig) != 0) {
    fprintf(stderr, "Couldn't copy codec context");
    return -1; // Error copying codec context
  }

  // Open codec
  //打开编解码器,打开后,相关的句柄等保存在编解码器上下文中。
  if(avcodec_open2(pCodecCtx, pCodec, NULL)<0)
    return -1; // Could not open codec
  
  // Allocate video frame
  //动态分配一下帧空间用于存储数据。
  pFrame=av_frame_alloc();
  
  // Allocate an AVFrame structure
  //再分配一个用于存储转换为RGB的数据。
  pFrameRGB=av_frame_alloc();
  if(pFrameRGB==NULL)
    return -1;

  // Determine required buffer size and allocate buffer
  //解码器解码时需要缓冲区存储临时数据,这里我们负责帮它申请。
  numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,
                  pCodecCtx->height);
  buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
  
  // Assign appropriate parts of buffer to image planes in pFrameRGB
  // Note that pFrameRGB is an AVFrame, but AVFrame is a superset
  // of AVPicture
  //这个是绑定pFrameRGB跟一个缓冲区,
  //前面使用pFrameRGB=av_frame_alloc()申请时,只是申请一个表面的结构体,
  //内部的缓冲区指针指向的空间还是NULL。
  //但是我们要使用pFrameRGB保存数据,所以我们使用avpicture_fill来进行绑定。
  //泥玛,这是网上找的答案。StackOverflow上一个老外回答的。
  avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,
         pCodecCtx->width, pCodecCtx->height);
  
  // initialize SWS context for software scaling
  //获取一个软件绽放上下文,用于一会儿绽放图像。
  sws_ctx = sws_getContext(pCodecCtx->width,
               pCodecCtx->height,
               pCodecCtx->pix_fmt,
               pCodecCtx->width,
               pCodecCtx->height,
               PIX_FMT_RGB24,
               SWS_BILINEAR,
               NULL,
               NULL,
               NULL
               );

  // Read frames and save first five frames to disk
  i=0;
  //泥玛,这里没有搞明白,为什么读取帧,要从格式上下文中读呢?
  //不过经过试验,的确是把avi中的前5帧保存成了ppm,还能查看呢。
  //每次调用这句,ffmpeg就将一帧或几帧完整的数据放进packet中了。
  while(av_read_frame(pFormatCtx, &packet)>=0) 
  {
    // Is this a packet from the video stream?
    if(packet.stream_index==videoStream) 
    {
      // Decode video frame
      //这里就是将AVPacket中的数据解码后放在AVFrame中。智能呀?还会告诉我们是否解码完毕呀?
      //应该就是一个返回值,表示解码成功或失败呀!
      avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
      
      // Did we get a video frame?
      if(frameFinished) {
    // Convert the image from its native format to RGB
    //将pFrame中的数据软件绽放到指定的大小并存储在pFrameRGB中。
    sws_scale(sws_ctx, (uint8_t const * const *)pFrame->data,
          pFrame->linesize, 0, pCodecCtx->height,
          pFrameRGB->data, pFrameRGB->linesize);
    
    // Save the frame to disk
    if(++i<=5)
      SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, i);
      }
    }
    
    // Free the packet that was allocated by av_read_frame
    av_free_packet(&packet);
  }
  
  // Free the RGB image
  av_free(buffer);
  av_frame_free(&pFrameRGB);
  
  // Free the YUV frame
  av_frame_free(&pFrame);
  
  // Close the codecs
  avcodec_close(pCodecCtx);
  avcodec_close(pCodecCtxOrig);

  // Close the video file
  avformat_close_input(&pFormatCtx);
  
  return 0;
}


shell.albert@yantai:~/temporary> gcc tutorial01.c  -I /home/shell.albert/libffmpeg/include  -L /home/shell.albert/libffmpeg/lib -lavformat -lavcodec -lz -lm -lswscale -lavutil -o tutorial01.bin
tutorial01.c: In function ‘main’:
tutorial01.c:137:3: warning: ‘avcodec_alloc_frame’ is deprecated (declared at /home/shell.albert/libffmpeg/include/libavcodec/avcodec.h:3422) [-Wdeprecated-declarations]
   pFrame=av_frame_alloc();
   ^
tutorial01.c:141:3: warning: ‘avcodec_alloc_frame’ is deprecated (declared at /home/shell.albert/libffmpeg/include/libavcodec/avcodec.h:3422) [-Wdeprecated-declarations]
   pFrameRGB=av_frame_alloc();
   ^
tutorial01.c:211:3: warning: ‘avcodec_free_frame’ is deprecated (declared at /home/shell.albert/libffmpeg/include/libavcodec/avcodec.h:3447) [-Wdeprecated-declarations]
   av_frame_free(&pFrameRGB);
   ^
tutorial01.c:214:3: warning: ‘avcodec_free_frame’ is deprecated (declared at /home/shell.albert/libffmpeg/include/libavcodec/avcodec.h:3447) [-Wdeprecated-declarations]
   av_frame_free(&pFrame);
   ^
shell.albert@yantai:~/temporary> 



shell.albert@yantai:~/temporary> ./tutorial01.bin zsy.avi 
Input #0, avi, from 'zsy.avi':
  Metadata:
    encoder         : Lavf55.33.100
  Duration: 00:03:20.03, start: 0.000000, bitrate: 479 kb/s
    Stream #0:0: Video: mpeg4 (Simple Profile) (FMP4 / 0x34504D46), yuv420p, 352x288 [SAR 1:1 DAR 11:9], 25 tbr, 25 tbn, 25 tbc
    Stream #0:1: Audio: ac3 ([0] [0][0] / 0x2000), 44100 Hz, stereo, fltp, 64 kb/s






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值