使用 FFmpeg 开发播放器基础--使用 ffmpeg 解码视频文件

原:http://blog.chinaunix.net/uid-11344913-id-4282729.html

使用 ffmpeg 解码多媒体文件之前,首先需要了解一些基本的概念:

容器:多媒体文件中包含视频与音频部分,将音频与视频封装在容器内,封装的形式为容器标准中所规定的格式,除了音频编码数据,视频编码数据,文字编码数据以外,还有一些用来格式化这些数据的字段,组成了容器;

编码器:多媒体文件中包含了视频编码部分与音频编码部分,都为容器所包含;


了解了以上基本概念后,下面看一下解码视频文件的基本步骤:

  1. 打开视频文件
  2. 从视频文件中打开视频流
  3. 从视频流中读取视频信息到视频帧里面
  4. 如果读取视频真没有完成,就继续从视频流中读取视频信息到视频帧里面
  5. 对视频帧进行操作,例如解码,绘制到屏幕,保存文件等
  6. 继续从视频流中读取视频信息到视频帧里面

在执行第2步之前,首先要注意,如果希望使用 ffmpeg 第一步要注册 ffmpeg 的函数,注册完成以后才能够顺利的使用 ffmpeg;


点击(此处)折叠或打开

  1. av_register_all()


通过这个函数,将ffmpeg中的avformat/avcodec/avfilter等模块全部注册完成了;

接下来就可以打开视频文件;


点击(此处)折叠或打开

  1. avformat_open_input()


通过该函数打开视频文件,分析对应的文件的信息,将信息填充进AVFormatContext中,填充完毕以后,可以获得视频流信息;



点击(此处)折叠或打开

  1. av_find_stream_info()

获得到了视频信息之后,可以知道视频中含多少路视频流,一路视频流用一路视频流的解流方式进行解流,例如音频流,例如视频流,例如字幕流等这里只讲述视频流;当或得到视频流信息后,既可根据对应的视频流的类型进行解码,例如h264,则会根据查找对应的解码器进行获得解码器的信息;



点击(此处)折叠或打开

  1. avcodec_find_decoder()

或得到解码器信息后,则可以将解码器打开;



点击(此处)折叠或打开

  1. avcodec_open()

打开解码器以后就可以根据解码信息将对应的frame数据进行读取,本文档来自于http://bbs.chinaffmpeg.com孙悟空读取的时候,每一个frame都需要右地方存储,所以,需要申请frame内存空间;



点击(此处)折叠或打开

  1. avcodec_alloc_frame()

申请完成之后则开始进行了读取视频frame,然后解frame操作;



点击(此处)折叠或打开

  1. av_read_frame();
  2. avcodec_decode_video()


点击(此处)折叠或打开

  1. av_read_frame();
  2. avcodec_decode_video()


av_read_frame会将数据从format中读取出来存储入packet中,然后使用avcodec_decode_video来对packet的data进行解码,然后将对应的解码后的数据存入frame中,frame中的数据可以存储入文件中,也可以直接绘制到屏幕上;

将数据存储入文件中的代码实例如下:


点击(此处)折叠或打开

  1. #include <libavcodec/avcodec.h>
  2. #include <libavformat/avformat.h>
  3. #include <libswscale/swscale.h>

  4. #include <stdio.h>

  5. void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame) {
  6.   FILE *pFile;
  7.   char szFilename[32];
  8.   int y;
  9.   // Open file
  10.   sprintf(szFilename, "frame%d.ppm", iFrame);
  11.   pFile = fopen(szFilename, "wb");
  12.   if (pFile == NULL)
  13.     return;
  14.   // Write header
  15.   fprintf(pFile, "P6\n%d %d\n255\n", width, height);
  16.   // Write pixel data
  17.   for (= 0; y < height; y++)
  18.     fwrite(pFrame->data[0] + y * pFrame->linesize[0], 1, width * 3, pFile);
  19.   // Close file
  20.   fclose(pFile);
  21. }

  22. int main(int argc, char *argv[])
  23. {
  24.   AVFormatContext *pFormatCtx = NULL;
  25.   int i = 0;
  26.   int videoStream = 0;
  27.   AVCodecContext *pCodecCtx = NULL;
  28.   AVCodec *pCodec = NULL;
  29.   AVFrame *pFrame = NULL; 
  30.   AVFrame *pFrameRGB = NULL;
  31.   AVPacket packet;
  32.   int frameFinished;
  33.   int numBytes;
  34.   uint8_t *buffer = NULL;

  35.   AVDictionary *optionsDict = NULL;
  36.   struct SwsContext *sws_ctx = NULL;
  37.   if(argc < 2) {
  38.     printf("Please provide a movie file\n");
  39.     return -1;
  40.   }
  41.   // Register all formats and codecs
  42.   av_register_all();
  43.   // Open video file
  44.   if(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL) != 0)
  45.     return -1; // Couldn't open file
  46.   // Retrieve stream information
  47.   if(avformat_find_stream_info(pFormatCtx, NULL) < 0)
  48.     return -1; // Couldn't find stream information
  49.   // Dump information about file onto standard error
  50.   av_dump_format(pFormatCtx, 0, argv[1], 0);
  51.   // Find the first video stream
  52.   videoStream=-1;
  53.   for(= 0; i < pFormatCtx->nb_streams; i++)
  54.     if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  55.       videoStream = i;
  56.       break;
  57.     }
  58.   if(videoStream == -1)
  59.     return -1; // Didn
OSX下编译方法如下:

点击(此处)折叠或打开

  1. gcc -Wall -ggdb test.-I/usr/include/SDL/ --test.o
  2. gcc -Wall -ggdb test.-L/usr/local/lib -lavdevice -lavfilter -lavformat -lavcodec -lswresample -lswscale -lavutil -framework QTKit -framework Foundation -framework QuartzCore -framework CoreVideo -framework Foundation -framework AVFoundation -framework CoreMedia -framework CoreFoundation -framework VideoDecodeAcceleration -framework QuartzCore -liconv -L/usr//lib -lSDLmain -lSDL -Wl,-framework,Cocoa -lm -lbz2 -lz-pthread -test



Linux下编译方法如下:

点击(此处)折叠或打开

  1. gcc -Wall -ggdb test.-I/usr/include/SDL/ --test.o
  2. gcc -Wall -ggdb test.-test -L/usr/local/lib -lavdevice -lavfilter -lavformat -lavcodec -lswresample -lswscale -lavuti -lm -lbz2 -lz -pthread


点击(此处)折叠或打开

  1. gcc -Wall -ggdb test.-I/usr/include/SDL/ --test.o
  2. gcc -Wall -ggdb test.-test -L/usr/local/lib -lavdevice -lavfilter -lavformat -lavcodec -lswresample -lswscale -lavuti -lm -lbz2 -lz -pthread

点击(此处)折叠或打开

  1. ffmpeg version N-63027-g9e58677 Copyright (c) 2000-2014 the FFmpeg developers
  2.   built on Jun 1 2014 17:05:37 with Apple LLVM version 5.(clang-503.0.40) (based on LLVM 3.4svn)
  3.   configuration: --prefix=/usr/local/
  4.   libavutil 52. 81.100 / 52. 81.100
  5.   libavcodec 55. 60.103 / 55. 60.103
  6.   libavformat 55. 37.102 / 55. 37.102
  7.   libavdevice 55. 13.101 / 55. 13.101
  8.   libavfilter 4. 5.100 / 4. 5.100
  9.   libswscale 2. 6.100 / 2. 6.100
  10.   libswresample 0. 18.100 / 0. 18.100
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值