ffmpeg跟sdl的学习过程:
一、版本信息:
ffmpeg-3.0.2.tar.bz2
SDL2-2.0.4.tar.gz
二、编译过程:
1、ffmgeg的编译:
./configure --enable-shared --disable-yasm --prefix=/usr/local/ffmpeg
make
make install

2、sdl的编译:
./configure --prefix=/usr/local/sdl
make
make install

3、系统环境配置:
查看/etc/ld.so.conf,知道 ”系统共享库路径”

方法一:把编译好的sdl的动态库,ffmpeg的动态库,复制到 “系统共享库路径“ 中,复制完成后,创建好软连接。
(完成之后调用指令ldconfig ???)

方法二:修改/etc/ld.so.conf
把下面的两行追加到ld.so.conf文件中
/usr/local/ffmpeg/lib
/usr/local/sdl/lib
调用ldconfig指令。
ldconfig做的这些东西都与运行程序时有关,跟编译连接sdl库,编译连接ffmpeg库一点关系都没有。编译的时候还是该加-L就得加,不要混淆了

方法三:临时环境变量
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/ffmpeg/lib:/usr/local/sdl/lib

方法四:修改shell文件:
$ vi ~/.bash_profile
没有LD_LIBRARY_PATH的话,添加:
LD_LIBRARY_PATH=/usr/local/ffmpeg/lib:/usr/local/sdl/lib
export LD_LIBRARY_PATH

有LD_LIBRARY_PATH的话,
在LD_LIBRARY_PATH之后进行路径添加即可。
LD_LIBRARY_PATH=.......:/usr/local/ffmpeg/lib:/usr/local/sdl/lib

修改完shell文件之后,然后运行
$ source ~/.bash_profile 就行了。
------------------------------------------------------------------
在shell下尝试设置LD_LIBRARY_PATH,以下面这种形式设置,老是报错bash: LD_LIBRARY_PATH: command not found,
LD_LIBRARY_PATH=/usr/local/lib
LD_LIBRARY_PATH = $ LD_LIBRARY_PATH:/usr/local/lib
可能是因为系统之前没有设置过LD_LIBRARY_PATH,于是改成这样:
export LD_LIBRARY_PATH=/usr/local/lib
然后用 echo $LD_LIBRARY_PATH检查一下是否真的设置成功
------------------------------------------------------------
三、编写代码,调用ffmpeg动态库。
代码,读取视屏文件,保存帧图片为ppm文件

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <stdio.h>

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[])
{
  AVFormatContext *pFormatCtx;
  int i, videoindex;
  AVCodecContext *pCodecCtx;
  AVCodec *pCodec;
  av_register_all();
  avformat_network_init();
  pFormatCtx = avformat_alloc_context();
  if(avformat_open_input(&pFormatCtx,argv[1],NULL,NULL)!=0)
  {
    printf("open file error\n");
    return -1;
  }

  if ( avformat_find_stream_info(pFormatCtx,NULL) < 0 )
  {
    return -1;
  }

  i = 0;
  int videostream = -1;
  printf("pFormatCtx->nb_streams=%d\n", pFormatCtx->nb_streams);
  for(i=0;i<pFormatCtx->nb_streams;i++)
  {
    if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
    {
      videostream = i;
      break;
    }
  }
  printf("videostream=%d\n", videostream);

  if (-1 == videostream)
  {
    printf("error no video stream\n");
    return;
  }

  pCodecCtx = pFormatCtx->streams[videostream]->codec;

  pCodec = avcodec_find_decoder( pCodecCtx->codec_id );

  if(NULL == pCodec)
  {
    printf("couldn't find the decode\n");
    return -1;
  }

  if( avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
  {
    printf("open decode error\n");
    return -1;
  }

  AVFrame *pFrame,*pFrameRGB;
  pFrame = av_frame_alloc();
  pFrameRGB = av_frame_alloc();
  uint8_t *out_buffer;

  int num = avpicture_get_size(AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
  printf("num=%d\n", num);

  out_buffer = (uint8_t *)av_malloc(num*sizeof(uint8_t));
  avpicture_fill((AVPicture *)pFrameRGB, out_buffer, AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);

  AVPacket packet;
  int ret = -1;
  i = 0;
  struct SwsContext *img_convert_ctx = NULL;
  img_convert_ctx = sws_getContext(pCodecCtx->width,pCodecCtx->height,pCodecCtx->pix_fmt , pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
  while(av_read_frame(pFormatCtx, &packet)>=0)
  {
    //printf("i=%d, videoindex=%d, packet.stream_index=%d\n", i++, videoindex, packet.stream_index);

    if(packet.stream_index == videostream)
    {
      //printf("111111\n");
      int got_picture = -1;
      ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, &packet);

      if(ret < 0)
      {
        printf("decode error\n");
        return -1;
      }

      //printf("got_picture:%d\n",got_picture);
      if(got_picture)
      {
        sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
        if(++i<=20)
        {
          SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, i);
        }

      }
    }

  av_free_packet(&packet);
  }

  free(out_buffer);
  av_free(pFrameRGB);

  // Free the YUV frame
  av_free(pFrame);

  // Close the codec
  avcodec_close(pCodecCtx);

  // Close the video file
  avformat_close_input(&pFormatCtx);

  return 0;
}

编译指令:
gcc -o test main.c -I/usr/local/ffmpeg/include -L/usr/local/ffmpeg/lib -lavutil -lavformat -lavcodec -lswscale -lz

sdl,ffmpeg都调用的编译指令:
gcc -o test main.c -I/usr/local/ffmpeg/include -I/usr/local/sdl/include -L/usr/local/ffmpeg/lib -L/usr/local/sdl/lib -lavutil -lavformat -lavcodec -lswscale -lz -lSDL2

读取视屏文件,保存帧图片为ppm文件的更多相关文章

  1. 使用ffmpeg将BMP图片编码为x264视频文件,将H264视频保存为BMP图片,yuv视频文件保存为图片的代码

    ffmpeg开源库,实现将bmp格式的图片编码成x264文件,并将编码好的H264文件解码保存为BMP文件. 实现将视频文件yuv格式保存的图片格式的測试,图像格式png,jpg, gif等等測试均O ...

  2. 读取.properties配置文件并保存到另一个.properties文件内

    代码如下 import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileOutputSt ...

  3. PDF 补丁丁 0.4.1.804 测试版发布:合并文件夹的图片和PDF文件,自由生成多层次书签

    新的测试版增强了合并文件的功能,可以合并文件夹内的图片和PDF文件,还可以在合并文件列表上直接指定与合并文件对应的PDF书签标题.通过拖放文件项目生成多层次的PDF书签.如下图所示: 另外,新的测试版 ...

  4. [R] 保存pheatmap图片对象到文件

    一般我们使用pheatmap通过Rstudio交互得到的图片在plots的Export导出即可,如何保存对象到文件呢?这个需求在自动化流程中很常见,作者似乎也没说明. 生成示例数据: test = m ...

  5. python中读取mongodb数据并保存为csv格式的文件

    import pandas as pd import matplotlib.pyplot as plt import pymongo %matplotlib inline # 连接mongodb数据库 ...

  6. arcengine 将地图文件保存为图片(包括各种图片格式)

    1,最近做了个地图文件输出图片的功能,思想主要就是利用MapControl的ActiveView中的out方法: 2代码如下:欢迎交流指正 SaveFileDialog m_save = new Sa ...

  7. android中保存Bitmap图片到指定文件夹中的方法

    /** 保存方法 */  public void saveBitmap() {   Log.e(TAG, "保存图片");   File f = new File("/s ...

  8. logback配置每天生成一个日志文件,保存30天的日志文件

    <?xml version="1.0" encoding="UTF-8"?> <configuration> <!-- 文件输出格 ...

  9. vue项目build 之后,css文件加载图片或者字体文件时404的解决。

    ExtractTextWebpackPlugin 提供了一个 options.publicPath 的 api,可以为css单独配置 publicPath . 对于用 vue-cli 生成的项目,di ...

随机推荐

  1. 【SPFA】 最短路计数

    最短路计数 [问题描述]   给出一个N个顶点M条边的无向无权图,顶点编号为1-N.问从顶点1开始,到其他每个点的最短路有几条. [输入格式]   输入第一行包含2个正整数N,M,为图的顶点数与边数. ...

  2. 安装SQLServer2005错误无法在com+目录中安装和配置程序集

    无法在com+目录中安装和配置程序集c:\program files\Microsoft SQL Server\90\DTS\tasks\microsoft.sqlserver.MSMQTASK.DL ...

  3. SQL server 创建 修改表格 及表格基本增删改查 及 高级查询 及 (数学、字符串、日期时间)函数[转]

    SQL server 创建 修改表格 及表格基本增删改查 及 高级查询 及 (数学.字符串.日期时间)函数   --创建表格 create table aa ( UserName varchar(50 ...

  4. android 学习随笔二十八(应用小知识点小结 )

    去掉标题栏的方法 第一种:也一般入门的时候经常使用的一种方法requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏注意这句一定要写在setConte ...

  5. django models数据类型

    Django Models的数据类型 AutoField IntegerField BooleanField true/false CharField maxlength,必填 TextField C ...

  6. 【python cookbook】【字符串与文本】2.在字符串的开头或结尾处做文本匹配

    问题:在字符串的开头或结尾处按照指定的文本模式做检查,例如检查文件的扩展名.URL协议类型等: 解决方法:使用str.startswith()和str.endswith()方法 >>> ...

  7. ASP开发入门+实战电子书共50本 —下载目录

    小弟为大家整理50个ASP电子书籍,有入门,也有实战电子书,做成了一个下载目录,欢迎大家下载. 资源名称 资源地址 ASP.NET开发实战1200例_第I卷 http://down.51cto.com ...

  8. HDU 5794:A Simple Chess(Lucas + DP)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5794 题意:让一个棋子从(1,1)走到(n,m),要求像马一样走日字型并只能往右下角走.里 ...

  9. Oracle中instr 函数的详解

    INSTR    (源字符串, 目标字符串, 起始位置, 匹配序号)    在Oracle/PLSQL中,instr函数返回要截取的字符串在源字符串中的位置.只检索一次,就是说从字符的开始    到字 ...

  10. [UML]转:浅谈UML的概念和模型之UML九种图

    转自:http://blog.csdn.net/jiuqiyuliang/article/details/8552956 目录: UML的视图 UML的九种图 UML中类间的关系 上文我们介绍了,UM ...