FFMpeg是一个开源免费的视频和音频解决方案。它提供了录制、转换以及流化音视频的完整解决方案。FFMpeg支持在Linux平台下开发,同时也支持其它操作系统环境中编译,包括windows、Mac OS等。
1、首先来介绍一下FFmpeg文件夹中的文件:
libavformat :用于各种音视频封装格式的生成和解析,包括获取解码所需信息以生成解码上下文结构和读取音视频帧等功能;
libavcodec :用于各种类型声音/图像编解码;
libavutil :包含一些公共的工具函数;
libswscale :用于视频场景比例缩放、色彩映射转换;
libpostproc:用于后期效果处理;
ffmpeg :该项目提供的一个工具,可用于格式转换、解码或电视卡即时编码等;
ffsever :一个 HTTP 多媒体即时广播串流服务器;
ffplay :是一个简单的播放器,使用ffmpeg 库解析和解码,通过SDL显示;
2、部分重点结构体和函数
1)、数据结构:
(1) AVFormatContext
(2) AVOutputFormat
(3) AVInputFormat
(4) AVCodecContext
(5) AVCodec
(6) AVFrame
(7) AVPacket
(8) AVPicture
(9) AVStream
2)、初始化函数:
(1) av_register_all()
(2) avcodec_open()
(3) avcodec_close()
(4) av_open_input_file()
(5) av_find_input_format()
(6) av_find_stream_info()
(7) av_close_input_file()
3)、音视频编解码函数:
(1) avcodec_find_decoder()
(2) avcodec_alloc_frame()
(3) avpicture_get_size()
(4) avpicture_fill()
(5) img_convert()
(6) avcodec_alloc_context()
(7) avcodec_decode_video()
(8) av_free_packet()
(9) av_free()
4)、文件操作:
(1) avnew_steam()
(2) av_read_frame()
(3) av_write_frame()
(4) dump_format()
5)、其他函数:
(1) avpicture_deinterlace()
(2) ImgReSampleContext()
一个读取摄像头一帧数据例子,源自网络的程序:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/videodev2.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
void captureOneFrame()
{
AVFormatContext *fmtCtx = NULL;
AVFormatParameters inputFmtParameter;
AVPacket *pcaket;
//输入格式(V4L2)
AVInputFormat *inputFmt = av_find_input_format ("video4linux2");
if (inputFmt == NULL)
{
printf("can not find_input_format\n");
return;
}
memset (&inputFmtParameter, 0, sizeof(inputFmtParameter));
//采集图像的高度
inputFmtParameter.height = 480;
//采集图像的宽度
inputFmtParameter.width = 640;
//打开摄像头设备
if (av_open_input_file ( &fmtCtx, "/dev/video0", inputFmt,
sizeof(inputFmtParameter),&inputFmtParameter) < 0)
{
printf("can not open_input_file\n");
return;
}
//从摄像头获取一帧图像
av_read_frame(fmtCtx, pcaket);
//输出图像的大小
printf("data length = %d\n",pcaket->size);
FILE *fp;
//打开(新建)文件
fp = fopen("out.jpg", "wb");
if (fp < 0)
{
printf("open frame data file failed\n");
return ;
}
//将数据写入文件
fwrite(pcaket->data, 1, pcaket->size, fp);
//关闭文件
fclose(fp);
//关闭设备文件
av_close_input_file(fmtCtx);
}
int main()
{
avcodec_init();
avcodec_register_all();
avdevice_register_all();
captureOneFrame();
return 0;
}