1.简介
在日常处理视频文件时常常会用到视频片段的截取功能,FFmpeg支持该功能,拥有视频的起始时间定位以及截取视频长度的接口av_seek_frame。
2.流程
2.1 在使用FFmpeg API之前,需要先注册API,然后才能使用API。当然,新版本的库不需要再调用下面的方法。
av_register_all()
2.2 构建输入AVFormatContext
声明输入的封装结构体,通过输入文件或者流地址作为封装结构的句柄。
AVFormatContext* ifmt_ctx = NULL;
const char* inputUrl = "test.mp4";
///打开输入的流
int ret = avformat_open_input(&ifmt_ctx, inputUrl, NULL, NULL);
if (ret != 0)
{
printf("Couldn't open input stream.\n");
return -1;
}
2.3 查找音视频流信息,通过下面的接口与AVFormatContext中建立输入文件对应的流信息。
//查找;
if (avformat_find_stream_info(inputFmtCtx, NULL) < 0)
{
printf("Couldn't find stream information.\n");
return -1;
}
2.4构建输出AVFormatContext
//输出的文件
AVOutputFormat *ofmt = NULL;
AVFormatContext *ofmt_ctx = NULL;
const char* out_filename = "out.flv";
avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
if (!ofmt_ctx)
{
return -1;
}
2.5申请输出的stream信息。
AVStream* out_stream = NULL;
//创建一个新的流
out_stream = avformat_new_stream(ofmt_ctx, NULL);
if (!out_stream)
{
return -1;
}
2.6信息的复制,输出的stream信息建立完成之后,需要从输入的stream中将信息复制到输出的stream中。
//输入的流 视频、音频、字幕等
AVStream* in_stream = ifmt_ctx->streams[i];
AVCodecParameters* in_codecpar = in_stream->codecpar;
//复制输入的流信息到输出流中
ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
if (ret < 0)
{
return -1;
}
2.7然后打开文件
//打开输出文件
ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
if (ret < 0)
{
return -1;
}
2.8写文件头
//写入头
ret = avformat_write_header(ofmt_ctx, NULL);
if (ret < 0)
{
return -1;
}
2.9 av_seek_frame
av_seek_frame(ifmt_ctx, AVMEDIA_TYPE_VIDEO, 0, AVSEEK_FLAG_BACKWARD);
av_seek_frame有4个参数
- AVFormatContext:句柄
- stream_index:流索引
- timestamp:时间戳
- flags:方法
flags有4种策略
- AVSEEK_FLAG_BACKWARD:向后查找
- AVSEEK_FLAG_BYTE:字节位置查找
- AVSEEK_FLAG_ANY:寻找任何帧,甚至是非关键帧
- AVSEEK_FLAG_FRAME:帧位置
2.10数据包读取和写入
输入和输出均已打开,接下来可以从输入格式中读取数据包,然后将数据包写入到输出文件中,当然,随着输入的封装格式与输出的封装格式差别化,时间戳也需要进行计算改变。
AVPacket pkt;
while (1)
{
AVStream* in_stream = NULL;
AVStream* out_stream = NULL;
//从输入流中读取数据到pkt中
ret = av_read_frame(ifmt_ctx, &pkt);
if (ret < 0)
break;
in_stream = ifmt_ctx->streams[pkt.stream_index];
if (pkt.stream_index >= stream_mapping_size || stream_mapping[pkt.stream_index] < 0)
{
av_packet_unref(&pkt);
continue;
}
pkt.stream_index = stream_mapping[pkt.stream_index];
out_stream = ofmt_ctx->streams[pkt.stream_index];
/* copy packet */
pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
pkt.pos = -1;
ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
if (ret < 0)
{
fprintf(stderr, "Error muxing packet\n");
break;
}
av_packet_unref(&pkt);
}
2.11写文件尾
//写文件尾
av_write_trailer(ofmt_ctx);
2.12收尾,关闭输入输出,释放资源。
//关闭
avformat_close_input(&ifmt_ctx);
if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
avio_closep(&ofmt_ctx->pb);
avformat_free_context(ofmt_ctx);
av_freep(&stream_mapping);
if (ret < 0 && ret != AVERROR_EOF)
{
return -1;
}
3.源码
#include "pch.h"
#include <iostream>
extern "C"
{
#include "libavformat/avformat.h"
#include "libavutil/dict.h"
#include "libavutil/opt.h"
#include "libavutil/timestamp.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
};
int main()
{
//av_register_all();
avformat_network_init();
AVFormatContext* ifmt_ctx = NULL;
const char* inputUrl = "test.mp4";
///打开输入的流
int ret = avformat_open_input(&ifmt_ctx, inputUrl, NULL, NULL);
if (ret != 0)
{
printf("Couldn't open input stream.\n");
return -1;
}
//查找流信息
if (avformat_find_stream_info(ifmt_ctx, NULL) < 0)
{
printf("Couldn't find stream information.\n");
return -1;
}
//输出的文件
AVOutputFormat *ofmt = NULL;
AVFormatContext *ofmt_ctx = NULL;
const char* out_filename = "out.flv";
avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
if (!ofmt_ctx)
{
return -1;
}
int stream_mapping_size = ifmt_ctx->nb_streams;
//为数组分配内存
int* stream_mapping = (int *)av_mallocz_array(stream_mapping_size, sizeof(*stream_mapping));
if (!stream_mapping)
{
return -1;
}
int stream_index = 0;
ofmt = ofmt_ctx->oformat;
for (int i = 0; i < ifmt_ctx->nb_streams; i++)
{
//输出的流
AVStream* out_stream = NULL;
//输入的流 视频、音频、字幕等
AVStream* in_stream = ifmt_ctx->streams[i];
AVCodecParameters* in_codecpar = in_stream->codecpar;
if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO && in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO && in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE)
{
stream_mapping[i] = -1;
continue;
}
stream_mapping[i] = stream_index++;
//创建一个新的流
out_stream = avformat_new_stream(ofmt_ctx, NULL);
if (!out_stream)
{
return -1;
}
//复制输入的流信息到输出流中
ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
if (ret < 0)
{
return -1;
}
out_stream->codecpar->codec_tag = 0;
}
if (!(ofmt->flags & AVFMT_NOFILE))
{
//打开输出文件
ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
if (ret < 0)
{
return -1;
}
}
//写入头
ret = avformat_write_header(ofmt_ctx, NULL);
if (ret < 0)
{
return -1;
}
AVPacket pkt;
av_seek_frame(ifmt_ctx, AVMEDIA_TYPE_VIDEO, 0, AVSEEK_FLAG_BACKWARD); //可以seek到任何位置
while (1)
{
AVStream* in_stream = NULL;
AVStream* out_stream = NULL;
//从输入流中读取数据到pkt中
ret = av_read_frame(ifmt_ctx, &pkt);
if (ret < 0)
break;
in_stream = ifmt_ctx->streams[pkt.stream_index];
if (pkt.stream_index >= stream_mapping_size || stream_mapping[pkt.stream_index] < 0)
{
av_packet_unref(&pkt);
continue;
}
pkt.stream_index = stream_mapping[pkt.stream_index];
out_stream = ofmt_ctx->streams[pkt.stream_index];
AVRational r = { 1,1 };
if (av_compare_ts(pkt.pts, in_stream->time_base, 60, r) >= 0) //判断是不是截取了60s
break;
/* copy packet */
pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
pkt.pos = -1;
ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
if (ret < 0)
{
fprintf(stderr, "Error muxing packet\n");
break;
}
av_packet_unref(&pkt);
}
//写文件尾
av_write_trailer(ofmt_ctx);
//关闭
avformat_close_input(&ifmt_ctx);
if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
avio_closep(&ofmt_ctx->pb);
avformat_free_context(ofmt_ctx);
av_freep(&stream_mapping);
if (ret < 0 && ret != AVERROR_EOF)
{
return -1;
}
return 0;
}