正文
FFmpeg算是音视频学习中不可能不接触的一个航空母舰,这系列文章算是翻译的一系列的文章
An ffmpeg and SDL Tutorial or How to Write a Video Player in Less Than 1000 Lines
,只是他使用的api较老,我的教程基于最新的FFmpeg4.0版本,开发环境时clion,默认使用cmake构建工具.
下载地址:
ffmpeg.org/download.ht…
概述
视频文件有几个基本组件。 首先,文件本身称为容器,容器的类型决定了文件中信息的位置。 比如AVI和Quicktime。 是一系列的streams, 通常有一个音频流和一个视频流。流中的数据元素称为帧。 每个流由不同种类的编解码器编码。 编解码器定义了数据是如何编码和解码的 - 因此称为CODEC。比如DivX和MP3. 数据包是可以包含数据的数据片段,这些数据可以被解码为原始帧,每个数据包包含完整的帧,在音频流下可能包含多个帧。
处理音视频数据不比较简单,主要分为以下几个步骤:
10 打开视频文件,获取streams
20 从video_stram中获取数据包,转换为帧
30 帧未读取完继续执行20
40 处理帧
50 跳转20
使用FFmpeg处理视频非常简单,本文介绍如何将一个视频文件的帧数据保存为一张ppm格式图片(ppm是一种非常简单的原始RGB数据文件,它包含一个非常简单的头信息,其余的数据全部是RGB数据).
打开文件
首先我们要将用到的库引入进来,编写一个错误输出函数:
#include <libavcodec/avcodec.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
#include <libavformat/avformat.h>
#include <libavutil/error.h>
void showErrMsg(int errNum, char *msg, size_t msgSize) {
memset(msg, 0, msgSize);
av_strerror(errNum, msg, msgSize);
fprintf(stderr, msg);
}
该函数将FFmpeg内部的错误代码转换为了字符串,提高可读性.下面编写主函数,要求传入至少一个参数,并将该参数作为文件路径来打开.
fileName = argv[1];
if (argc < 2) {
fprintf(stderr, "Please use FFmpegDemo1 <file> to open a video file");
return -1;
}
if ((errNum = avformat_open_input(&pFormatCtx, fileName, NULL, NULL)) != 0) {
showErrMsg(errNum, errMsg, 400);
return -1;
}
在4版本中av_aregister_all()函数已经被废弃,不需要再编写任何信息.
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options)
函数的作用是打开url指向的文件.
AVFormatContext
是FFmepg非常重要的一个结构体,相当于安卓中的contex,它为FFmpeg的avformat提供了一个上下文,让,开发者可以非常简单的获取这些信息.但是要注意这里传如的是
AVFormatContext
的指针的指针,所以我们的pFromatCtx是一个指针,而传入参数时需要传入该指针的指针.
fmt是该文件的格式,这里传入NULL后FFmpeg会自动判断文件的编码格式.
然后读取头信息,将信息显示出来:
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
showErrMsg(errNum, errMsg, 400);
return -1;
}
av_dump_format(pFormatCtx, 0, fileName, 0);
avformat_find_stream_info
该函数将文件的streams的信息读入
pFormatCtx->streams
,然后通过
av_dump_format
函数打印出这些详细信息.
测试文件输出的详细信息如下:
Input #0, matroska,webm, from '../in.mkv':
Metadata:
title : 灵感工作室 www.lggzs.com
encoder : libebml v1.2.3 + libmatroska v1.3.0
creation_time : 2012-06-30T02:28:33.000000Z
Duration: 00:25:43.08, start: 0.000000, bitrate: 1311 kb/s
Chapter #0:0: start 0.105000, end 1543.082000
Metadata:
title : 00:00:00.105
Stream #0:0: Video: h264 (High), yuv420p(progressive), 1440x1080 [SAR 1:1 DAR 4:3], 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
Stream #0:1(jpn): Audio: aac (LC), 48000 Hz, stereo, fltp
Metadata:
title : 日语
Stream #0:2(chi): Audio: aac (HE-AAC), 44100 Hz, stereo, fltp (default)
Metadata:
title : 国语
Stream #0:3: Subtitle: subrip (default)
在视频文件中包含许多的流,比如视频流,音频流,而音频流可能又包含多种音频,比如上边的输出信息中,视频流时stream0,音频流包含两个,分别是stream1日语和stream2国语.该篇文章的目的是将视频流分解为帧,保存前50帧信息,那么需要找出对应的视频流,代码如下:
for (i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
codecPar = pFormatCtx->streams[i]->codecpar;
videoStream = i;
break;
}
}
if (videoStream == -1) {
return -1;
}
pCodec = avcodec_find_decoder(codecPar->codec_id);
if (pCodec == NULL) {
fprintf(stderr, "Unsupported codec!");
return -1;
}
pCodecCtx = avcodec_alloc_context3(pCodec);
avcodec_parameters_to_context(pCodecCtx, codecPar);
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
return -1;
}
pFormatCtx的streams
中保存了所有的流信息,
AVCodecParameters
,这个结构体中保存了
AVMediaType
,这个枚举类就是流的类型:
AVMEDIA_TYPE_UNKNOWN = -1, ///< Usually treated as AVMEDIA_TYPE_DATA
AVMEDIA_TYPE_VIDEO,
AVMEDIA_TYPE_AUDIO,
AVMEDIA_TYPE_DATA, ///< Opaque data information usually continuous
AVMEDIA_TYPE_SUBTITLE,
AVMEDIA_TYPE_ATTACHMENT, ///< Opaque data information usually sparse
AVMEDIA_TYPE_NB
我们只需要遍历找出视频流即可,并保存
AVCodecParameters
和索引,
AVCodecParameters
除了有
AVMediaType
外,还有一个
AVCodecID
,这个枚举类保存了该视频文件的编码方法,我们需要根据这个ID来寻找对应的解码器:
pCodec = avcodec_find_decoder(codecPar->codec_id);
if (pCodec == NULL) {
fprintf(stderr, "Unsupported codec!");
return -1;
}
寻找解码器后旧需要来初始化解码器的相关内容了,这个主要是liaavcodec库完成,同样它需要一个上下文环境:
pCodecCtx = avcodec_alloc_context3(pCodec);
avcodec_parameters_to_context(pCodecCtx, codecPar);
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
return -1;
}
上面的代码中首先为
pCodecCtx
分配了一块区域,然后将我们之前的codecPar参数传给它,使
pCodecCtx
设置为相对应的参数.然后
avcodec_open2
初始化pCodecCtx,这样打开解码器的工作旧完成了.
数据处理
做了一个最简单的分析图:
首先需要初始化一些数据结构体
pFrame = av_frame_alloc();
AVFrame *pFrameRGB = av_frame_alloc();
if (pFrameRGB == NULL) {
return -1;
}
unsigned char *buffer = NULL;
int numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height,AV_INPUT_BUFFER_PADDING_SIZE);
buffer = (unsigned char *) av_malloc(numBytes * sizeof(unsigned char));
av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, buffer, AV_PIX_FMT_RGB24, pCodecCtx->width,pCodecCtx->height, AV_INPUT_BUFFER_PADDING_SIZE);
struct SwsContext *sws_ctx = NULL;
AVPacket packet;
sws_ctx = sws_getContext(pCodecCtx->width,
pCodecCtx->height,
pCodecCtx->pix_fmt,
pCodecCtx->width,
pCodecCtx->height,
AV_PIX_FMT_RGB24,
SWS_BILINEAR,
NULL, NULL, NULL);
i = 0;
pFrame是用来存储解码后原始数据的一个结构体,对应于途图中的pFrame(empty)和pFrame(full),pFrameRGB是经过SwsContext变换后的数据,对应于图中的pFrame(tosave),我们可以直接存储pFrameRGB为图片.buffer是指向数组的一个指针,它是用来提供给pFrameRGB的一块数据区域.SwsContex是libswscale的上下文环境,用来简单的缩放图片.
下面我们需要来处理数据了:
while (av_read_frame(pFormatCtx, &packet) >= 0) {
if (packet.stream_index == videoStream) {
if (avcodec_send_packet(pCodecCtx, &packet) != 0) {
fprintf(stderr, "there is something wrong with avcodec_send_packet\n");
av_packet_unref(&packet);
continue;
}
if (avcodec_receive_frame(pCodecCtx, pFrame) == 0) {
sws_scale(sws_ctx, (uint8_t
const *const *) pFrame->data,
pFrame->linesize, 0, pCodecCtx->height,
pFrameRGB->data,
pFrameRGB->linesize);
if (++i <= 50) {
SavaFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, i);
}
}
}
av_packet_unref(&packet);
if (i == 50) {
break;
}
}
首先是通过
av_read_frame
来将video_stream中的数据读入packet,然后将这个packet通过
avcodec_send_packet
发送给AVCodecContext,AVCodecContext将这些数据解码为原始图像数据帧,我们可以通过
avcodec_receive_frame
来获取解码后的帧,然后经过SWScaleContext来将图像数据格式化为rgb格式,最后我们需要不断的释放packet.这里注意一点,
avcodec_send_packet
和
avcodec_receive_frame
并不是一一对应的关系,也就是输入数据后并不一定都必须要输出数据,这是因为
AVCodecContext
可能会缓存几帧数据.
保存数据
保存数据较简单,直接看代码:
void SavaFrame(AVFrame *pFrame, int width, int height, int frame) {
FILE *pFile;
char szFilename[32];
int y;
//生成文件名称
sprintf(szFilename, "frame%d.ppm", frame);
//创建或打开文件
pFile = fopen(szFilename, "wb");
if (pFile == NULL) {
return;
}
//写入头信息
fprintf(pFile, "P6\n%d %d\n225\n", width, height);
//写入数据
for (y = 0; y < height; y++)
fwrite(pFrame->data[0] + y * pFrame->linesize[0], 1, width * 3, pFile);
fclose(pFile);
}
释放资源
FFMpeg中的大部分分配资源都提供了一个简单的释放方法,我们逐个调用即可:
av_free(buffer);
av_frame_unref(pFrameRGB);
av_frame_unref(pFrame);
av_free(pFrame);
avcodec_free_context(&pCodecCtx);
avformat_close_input(&pFormatCtx);
运行项目
首先配置以下cmake文件:
cmake_minimum_required(VERSION 3.10)
project(FFmpegDemo C)
set(CMAKE_C_STANDARD 99)
add_executable(FFmpegDemo1 main.c)
target_link_libraries(FFmpegDemo1
avformat
z
m
pthread
avdevice
avfilter
avcodec
avutil
# postproc
swresample
swscale
dl
)
假如没有安装pthread,首先要装好pthread,要不然程序不能运行,还要注意库的链接顺序.
下面是pthread的安装代码:
sudo apt-get install glibc-doc
sudo apt-get install manpages-posix-dev
可以直接使用clion的运行功能来运行程序,但是首先要添加视频文件路径作为参数.我们也可以在命令行直接生成文件文件运行
mkdir build;cd build/ \\执行外部构建
cmake ..
make
FFmpegDemo1 ../in.mkv
执行完成后我们即可看到生成的ppm文件,再clion直接打开即可.