转换视频格式步骤
1、打开输入文件;
2、创建并打开一个空文件存储 flv 格式音视频数据;
3、遍历输入文件的每一路流,每个输入流对应创建一个输出流,并将输入流中的编解码参数直接拷贝到输出流中;
4、写入新的多媒体文件的头;
5、在循环遍历输入文件的每一帧,对每一个packet进行时间基的转换;
6、写入新的多媒体文件;
7、给新的多媒体文件写入文件尾;
8、释放相关资源。
相关API
源码:
#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>
#include "ConverterVideo.h"
static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt, const char *tag) {
AVRational *time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;
printf("%s: pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
tag,
av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, time_base),
av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, time_base),
av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, time_base),
pkt->stream_index);
}
int converterVideo(const char *in_filename, const char *out_filename) {
AVOutputFormat *outfmt = NULL; //输出格式
AVFormatContext *infmt_ctx = NULL, *outfmt_ctx = NULL;//输入、输出上下文
AVPacket pkt;
int ret , i;
int stream_index = 0;
int *stream_mapping = NULL;//数组用于存放输出文件流的Index
int stream_mapping_size = 0