日期:2014-05-16  浏览次数:20656 次

Linux下MIPS平台交叉编译FFMpeg库 及使用库截取视频中的某一帧

参考http://www.ffmpeg.com.cn网站

 

一.Linux下MIPS平台交叉编译FFMpeg库:
1.下载ffmpeg库的源代码
(http://sourceforge.net/project/showfiles.php?group_id=205275&package_id=248632):
[root@localhost ffmpeg]# ls
ffmpeg-laster.tar.gz
2.解压:
[root@localhost ffmpeg]# tar zxvf ffmpeg-laster.tar.gz
3.开始配置,并编译:
[root@localhost ffmpeg]# mkdir ffmpeg-release
[root@localhost ffmpeg]# cd ffmpeg
[root@localhost ffmpeg]# ./configure --enable-cross-compile --target-os=linux --cross-prefix=mipsel-linux- --cc=mipsel-linux-gcc --enable-shared --arch=mips --prefix=/opt/brcm/ffmpeg/ffmpeg-release
[root@localhost ffmpeg]# make
[root@localhost ffmpeg]# make install
[root@localhost ffmpeg]# cd ../ffmpeg-release
[root@localhost ffmpeg-release]# tar -zcvf ffjpeg_lib.tar.gz ./lib


板子上运行:
# cp ffjpeg_lib.tar.gz /usr/local/lib/
# cd /usr/local/lib/
# tar -zxvf ffjpeg_lib.tar.gz -C ../
# rm ffjpeg_lib.tar.gz

# cp ffmpeg-release/bin/* /bin/ 
# ffmpeg
FFmpeg version SVN-r21694, Copyright (c) 2000-2010 Fabrice Bellard, et al.
  built on Nov 17 2012 02:25:17 with gcc 4.5.3
  configuration: --enable-cross-compile --target-os=linux --cross-prefix=mipsel-linux- --cc=mipsel-linux-gcc --enable-shared --arch=mips --prefix=/opt/brcm/ffmpeg/ffmpeg-release
  libavutil     50. 9. 0 / 50. 9. 0
  libavcodec    52.52. 0 / 52.52. 0
  libavformat   52.51. 0 / 52.51. 0
  libavdevice   52. 2. 0 / 52. 2. 0
  libswscale     0.10. 0 /  0.10. 0
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Use -h to get full help or, even better, run 'man ffmpeg'
#

到现在为止, 我们就成功的将 ffmpeg 移植到我们的开发板上了。

 

二.Linux下使用ffmpeg库:
1.命令行使用:
ffmpeg -i test.mp4 -y -f image2 -ss 30 -s 95*95 -vframes 1 a.jpg
找一个MP4文件:test.mp4,然后使用上面的命令就可以取得30秒时视频的图像,保存为
95*95像素的JPG文件。

ffmpeg -i 000094.mp4 -vframes 30 -pix_fmt rgb24  -y -f gif a.gif
把前30帧转化为GIF文件。

2.在程序中调用函数截获视频图片:
首先加入库支持,在我们的可执行程序的Makefile文件中,加入:
CFLAGS += -I/opt/brcm/ffmpeg/ffmpeg-release/include/
LFLAGS += -L/opt/brcm/ffmpeg/ffmpeg-release/lib/ -lavutil -lavformat -lavcodec -lswscale
(记住,后面最后四个参数缺一不可,否则编译不通过)
然后关于在编程中使用FFMPEG库的方法,附带一个成熟的Demo。
(test_snap.c和Makefile)

 

三.遇到的问题:

1.找不到关键帧:first frame is no keyframe: 这样的截图为透明色,不满足截图要求。
解决方案: 跟踪截图函数的返回值,如果no keyframe,将开始时间推迟1秒,再截图,直到成功为止。

 

test_snap.c 如下:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include <libavformat/avformat.h>
#include <libswscale/swscale.h>

#undef sprintf
#undef uint8_t
#undef uint16_t
#undef uint32_t
#define uint8_t unsigned char
#define uint16_t unsigned short
#define uint32_t unsigned long

#pragma pack(2)
typedef struct BMPHeader
{
    uint16_t identifier;
    uint32_t file_size;
    uint32_t reserved;
    uint32_t data_offset;
} BMPHeader;

typedef struct BMPMapInfo
{
    uint32_t header_size;
    uint32_t width;
    uint32_t height;
    uint16_t n_planes;
    uint16_t bits_per_pixel;
    uint32_t compression;
    uint32_t data_size;
    uint32_t hresolution;
    uint32_t vresolution;
    uint32_t n_colors_used;
    uint32_t n_important_colors;
}BMPMapInfo;

int CreateBmpImg(AVFrame *pFrame, int width, int height, int iFrame)
{
    BMPHeader bmpheader;
    BMPMapInfo bmpinfo;
    FILE *fp;
    int y;
    char filename[32];
    
    // Open file
    memset(filename, 0x0, sizeof(filename));
    sprintf(filename, "%d.bmp", iFrame+1);
	JPRINTF(("Create BMP File : [%s] \n",filename));
    fp = fopen(filename, "wb");
    if(!fp)