使用 FFmpeg API 从视频中提取图片
这些年来,短视频吸引了无数网民的注意。相对于丰富有趣的内容,我们码农可能更关心其底层技术实现。本系列文章将结合 ffmpeg,讲解几则视频处理案例。
短视频都是以文件的形式保存于服务器上。任何一个便于传播的文件都会有一种定义良好的格式,同样视频也有其格式。这系列我们不会去从微观的角度去分析这些格式,因为其应用意义不是很大。我们将从宏观角度去分析,视频文件应该包含哪些信息?
能确定的是,大部分情况下,我们可以使用眼睛看到 图像 ,使用耳朵听到 声音 。如果我们关闭其中任意一个器官,就将停止接受对应的信息;而没有关闭的器官还和之前一样接受信息,不受影响。
所以目前至少我们可以把视频分为:图像和声音两个模块。那这两个模块是怎么组合的?是不是一个极短时间内的图像和声音(比如我们此时此刻正看到的图像和听到的声音)融合在一个 区块 中?
从设计的角度说, 耦合 是非常不好的。如果将图像和声音信息融合在一个区块中,就是一种很强的耦合。一种良好的设计就像我们小时候在电影院看的电影文件(不知道现在电影播放的原理):一个文件用于播放图像,一个文件用于播放声音。这样我们可以配一个普通话版,一个英语版、一个法语版……的音频文件,而不用去修改播放的图像文件。但是我们在 PC 上看到的视频文件是一个独立文件,那是怎么搞的?
于是在设计就要在 易用 和 可维护 之间做个平衡:宏观层面融合图像和声音文件,微观层面图像和声音信息是分离的。对应到 ffmpeg 上来说就是:
- 图像文件和声音文件分别是一个流——AVStream 结构;
- 图像文件和声音文件微观分离体现在它们都是独立的包——AVPacket;
- 图像文件和声音文件宏观融合是通过 视音频复用器——Muxer 融合的;
以 ffmpeg4.0.2 版本的 API 为例
void get_video_pictures(const char* file_path) { std::unique_ptr<AVFormatContext, std::function<void(AVFormatContext*)>> avfmt_ctx_t( avformat_alloc_context(), [](AVFormatContext *s) { if (s) { avformat_close_input(&s); } } );
AVFormatContext* && avfmt_ctx = avfmt_ctx_t.get(); if (avformat_open_input(&avfmt_ctx, file_path, NULL, NULL)) { std::cerr << "avformat_open_input error"; return; }</code>
首先我们需要构造一个 AVFormatContext 对象,它用于承载我们分析文件的上下文。Context(上下文)这个概念在 ffmpeg 中非常重要,我们可以通过它的一些参数干预 ffmpeg 底层的行为,还可以通过它获得对应层面的信息。之后我们会遇到各种 Context。这类 Context 的使用有比较固定的套路:
- 使用 XXXXX_alloc_context分配空间。AVFormatContext 对应的就是 avformat_alloc_context。
- 使用 XXXXX_openXXX初始化。AVFormatContext 对应的就是 avformat_open_input。
- 使用 XXXXX_free_context释放空间。AVFormatContext 对应的就是 avformat_free_context。由于 avformat_close_input 包含了更多的释放操作,且其底层也会调用 avformat_free_context,所以此处我们使用了它。
AVFormatContext 有个两个和 流 ——AVStream 相关的信息:nb_streams 和 streams。后者是一个 AVStream 数组的首地址,前者是该数组的元素个数。我们可以遍历所有流
for (unsigned int i = 0; i < avfmt_ctx->nb_streams; i++) {
AVStream *st = avfmt_ctx->streams[i];
之前我们谈到,图像和声音分别属于不同的流,于是我们可以通过 AVStream::codecpar::codec_type 辨别流
enum 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
};
在这组枚举类型中,我们还看到 AVMEDIA_TYPE_SUBTITLE,它是 字幕流 类型。可以见得,字幕并不是刻印在图像上的。在现实生活中,我们在播放器中可以选择不同的字幕,不同的语言配音(英文/中文),这些都是以流的形式保存在视频文件这个容器中的,而且它们还可以是多份的。比如中文配音是一个流,英文配音是一个流,中文字幕是一个流,英文字幕是一个流。
如本文标题,我们需要从图像流中提取图片,于是切入 AVMEDIA_TYPE_VIDEO 类型的流进行操作
if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
std::unique_ptr<AVCodecContext, std::function<void(AVCodecContext*)>> avcodec_ctx( avcodec_alloc_context3(NULL), [](AVCodecContext *avctx) { if (avctx) { avcodec_free_context(&avctx); } } ); if (0 > avcodec_parameters_to_context(avcodec_ctx.get(), st->codecpar)) { std::cerr << "avcodec_parameters_to_context error.stream " << i; continue; } AVCodec *avcodec = avcodec_find_decoder(avcodec_ctx->codec_id); if (avcodec_open2(avcodec_ctx.get(), avcodec, NULL) < 0) { std::cerr << "Failed to open codec" << std::endl; continue; } save_video_pic(avfmt_ctx, i, avcodec_ctx.get()); } }
}
对于每个流,也有其自身的格式。我们需要使用解码器对该流进行解码分析,所以这次会涉及到 AVCodecContext 结构。和之前的 Context 使用套路一致:
- 使用 avcodec_alloc_context3 申请空间;
- 使用 avcodec_free_context 释放空间;
- 通过 avcodec_parameters_to_context 以流中解码器信息初始化;
- 通过 avcodec_find_decoder 找到对应的解码器;
- 使用 avcodec_open2 和上述找到的解码器,打开这个上下文;
这次我们没有使用 avcodec_open2 对应的 avcodec_close 方法,是因为该方法在 4.0.2 版本中被声明为 即将废弃
/**
* Close a given AVCodecContext and free all the data associated with it
* (but not the AVCodecContext itself).
*
* Calling this function on an AVCodecContext that hasn't been opened will free
* the codec-specific data allocated in avcodec_alloc_context3() with a non-NULL
* codec. Subsequent calls will do nothing.
*
* @note Do not use this function. Use avcodec_free_context() to destroy a
* codec context (either open or closed). Opening and closing a codec context
* multiple times is not supported anymore -- use multiple codec contexts
* instead.
*/
int avcodec_close(AVCodecContext *avctx);
类似的,我们没有直接使用 AVSteam 中的 AVCodecContext *codec,也是因为它 即将废弃
attribute_deprecated
AVCodecContext *codec;
通过 avcodec_open2 打开一个和解码器相关的上下文后,我们就可以开始解码了。在这之前需要熟悉两个比较微观的结构——AVPacket 和 AVFrame。AVPacket 是编码后(未解码)的数据结构,AVFrame 是编码前(未编码)的结构。所以我们从一个视频文件中,通过 av_read_frame 读出来的是一个尚未解码的数据——AVPacket。
void save_video_pic(AVFormatContext *avfmt_ctx, int stream_index, AVCodecContext *avcodec_ctx) {
int err = av_seek_frame(avfmt_ctx, -1, avfmt_ctx->start_time, 0);
do {
std::unique_ptr<AVPacket, std::function<void(AVPacket*)>> avpacket_src(
av_packet_alloc(),
[](AVPacket *pkt) {
if (pkt) {
av_packet_free(&pkt);
}
}
);
av_init_packet(avpacket_src.get());
if (av_read_frame(avfmt_ctx, avpacket_src.get()) < 0) {
break;
}
if (avpacket_src->stream_index != stream_index) {
continue;
}
注意第 16 行,它通过判断读出来的 AVPacket 的 stream_index 是否为之前分析出来的视频流下标,决定是否继续执行。 这个流程说明不同流的 AVPacket 在文件中可以是穿插分布的。这种设计存在一定的合理性。因为在同一时刻,图像、声音、字幕等都要展现出来,顺序性读取并解析可以减少频繁的跳转。
因为编解码过程比较类似,我将过程中结果保存组织在一个模板类中
template<typename Component> class AvComponentStore { public: virtual void save(Component *d) = 0; };
template<typename Component>
class TransStore :
public AvComponentStore<Component>
{
public:
TransStore(std::function<Component*(const Component*)> clone, std::function<void(Component**)> free) {
_clone = clone;
_free = free;
}
~TransStore() { for (auto it = _store.begin(); it != _store.end(); it++) { if (*it) { _free(&*it); } } }
public:void traverse(std::function<void(Component*)> t) {
if (!t) {
return;
}
for (auto it = _store.begin(); it != _store.end(); it++) {
if (_it) {
t(_it);
}
}
}
public:
virtual void save(Component d) {
Component p = _clone(d);
_store.push_back(p);
}
private:
std::vector<Component> _store;
std::function<Component(const Component)> _clone;
std::function<void(Component*)> _free;
};
using PacketsStore = TransStore<AVPacket>;
using FramesStore = TransStore<AVFrame>;
FrameStore 用于保存 AVPacket 的解码结果。对于中间产生的 AVFrame 结构,我们使用 av_frame_clone 深度拷贝。FrameStore 对象释放时,将通过 av_frame_free 释放这些空间和资源。
std::shared_ptr<FramesStore> frames_store = std::make_shared<FramesStore>(av_frame_clone, av_frame_free); decode_packet(avcodec_ctx, avpacket_src.get(), frames_store); frames_store->traverse(traverse_frame);
} while (true);
}
解码 AVPacket 通过 avcodec_send_packet 和 avcodec_receive_frame 实现。从语义上说,我们将一个解码前的数据发送给一个解码器上下文,然后从这个解码器上下文中获得解码后的数据。
int decode_packet(AVCodecContext *avctx, AVPacket *pkt, std::shared_ptr<FramesStore> store) { int ret = avcodec_send_packet(avctx, pkt); if (ret < 0 && ret != AVERROR_EOF) { return ret; }
std::unique_ptr<AVFrame, std::function<void(AVFrame*)>> frame( av_frame_alloc(), [](AVFrame *frame) { if (frame) { av_frame_free(&frame); } } ); ret = avcodec_receive_frame(avctx, frame.get()); if (ret >= 0) { store->save(frame.get()); } else if (ret < 0 && ret != AVERROR(EAGAIN)) { return ret; } return 0;
}
对于每个解码后的数据,我们需要通过图片编码器将其编码成一个图片文件。
和之前生成解码器上下文相似,我们要构造一个编码器上下文。这次我们要使用 avcodec_find_encoder 去寻找编码器
void traverse_frame(AVFrame* avframe) {
AVCodec *avcodec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
然后使用 avcodec_open2 去打开一个和该编码器相关的上下文
std::unique_ptr<AVCodecContext, std::function<void(AVCodecContext*)>> avcodec_ctx_output( avcodec_alloc_context3(avcodec), [](AVCodecContext *avctx) { if (avctx) { avcodec_free_context(&avctx); } } );
avcodec_ctx_output->width = avframe->width; avcodec_ctx_output->height = avframe->height; avcodec_ctx_output->time_base.num = 1; avcodec_ctx_output->time_base.den = 1000; avcodec_ctx_output->pix_fmt = AV_PIX_FMT_YUVJ420P; avcodec_ctx_output->codec_id = avcodec->id; avcodec_ctx_output->codec_type = AVMEDIA_TYPE_VIDEO; if (avcodec_open2(avcodec_ctx_output.get(), avcodec, nullptr) < 0) { std::cerr << "Failed to open codec" << std::endl; return; }</code>
encode_frame 方法将把每个 AVFrame 打包成若干个 AVPacket,并保存在 PacketsStore 对象中
std::shared_ptr<PacketsStore> packets_store = std::make_shared<PacketsStore>(av_packet_clone, av_packet_free);
if (encode_frame(avcodec_ctx_output.get(), avframe, packets_store) < 0) {
std::cerr << "encode_frame error" << std::endl;
return;
}
编码的过程使用 avcodec_send_frame 和 avcodec_receive_packet 方法。从语义上就是将一个解码前的数据发送到一个编码器上下文,然后从这个上下文中获得编码后的数据。
int encode_frame(AVCodecContext *c, AVFrame *frame, std::shared_ptr<PacketsStore> store) { int ret; int size = 0;
std::unique_ptr<AVPacket, std::function<void(AVPacket*)>> pkt( av_packet_alloc(), [](AVPacket *pkt) { if (pkt) { av_packet_free(&pkt); } } ); av_init_packet(pkt.get()); ret = avcodec_send_frame(c, frame); if (ret < 0) { return ret; } do { ret = avcodec_receive_packet(c, pkt.get()); if (ret >= 0) { store->save(pkt.get()); size += pkt->size; av_packet_unref(pkt.get()); } else if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) { return ret; } } while (ret >= 0); return size;
}
在编码完数据后,我们将其保存到一个文件中。
std::string&& file_name = gen_pic_name(avframe);
std::unique_ptr<std::FILE, std::function<int(FILE*)>> file(std::fopen(file_name.c_str(), "wb"), std::fclose);
packets_store->traverse(
[&file](AVPacket* packet){
fwrite(packet->data, 1, packet->size, file.get());
}
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论