FFmpeg入门教程:视频裁剪及API调用

FFmpeg入门教程:视频裁剪及API调用

相关函数API

  • av_seek_frame();//跳转到指定帧
  • av_rescale_q_rnd()//时间基转换函数
  • av_read_frame()//读取输入流
    在这里插入图片描述

处理逻辑

  • API注册
  • 创建输入、输出上下文
  • 初始化上下文
  • 创建流及参数拷贝
  • 打开输出文件,写入头信息
  • 根据时间基函数判断裁剪位置写入数据
    在这里插入图片描述

相关代码

#include<stdio.h>
#include <stdlib.h>
#include <iostream>
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavformat/avio.h"
#include <libavutil/log.h>
#include <libavutil/timestamp.h>
}
#define ERROR_STR_SIZE 1024
void cutVideo(double start_seconds, double end_seconds, const char *inputFileName, const char *outputFileName) {
	av_register_all();
	AVFormatContext * inputfile = NULL; //创建输入上下文
	AVFormatContext * outputfile = NULL;//创建输出上下文
	AVOutputFormat *ofmt = NULL; //创建格式
	AVPacket pkt;//创建包
	int error_code;//创建错误代码
	int ret;
	av_log_set_level(AV_LOG_DEBUG);
	if (error_code = avformat_open_input(&inputfile, inputFileName, NULL, NULL) < 0) {
		av_log(NULL, AV_LOG_ERROR, "Could not open src file, %s, %d(%s)\n", inputFileName);
		//goto end;
		avformat_close_input(&inputfile);
	}
	if (error_code = avformat_alloc_output_context2(&outputfile, NULL, NULL, outputFileName) < 0) {
		av_log(NULL, AV_LOG_ERROR, "Could not open src file, %s, %d(%s)\n", inputFileName);
		//goto end;
		avformat_close_input(&inputfile);
	}
	for (int i = 0; i < inputfile->nb_streams; i++) {
		AVStream * inputStream = inputfile->streams[i];//创建输入流(根据序号是音频流还是视频流)。。这里通过循环的方式就可以不用av_find_best_stream()方法
		AVStream * outputStream = avformat_new_stream(outputfile, NULL);//创建输出流
		if (!outputStream) {
			av_log(NULL, AV_LOG_ERROR, "Could not create The Stream\n");
			//goto end;
			avformat_close_input(&inputfile);
		}
		avcodec_parameters_copy(outputStream->codecpar, inputStream->codecpar);
		outputStream->codecpar->codec_tag = 0;
	}
	avio_open(&outputfile->pb, outputFileName, AVIO_FLAG_WRITE);
	avformat_write_header(outputfile, NULL);
	//跳转到指定帧
	ret = av_seek_frame(inputfile, -1, start_seconds * AV_TIME_BASE, AVSEEK_FLAG_ANY);//求一个起止点根据开始时间计算出是从多少PTS开始的返回值
	if (ret < 0) {
		fprintf(stderr, "Error seek\n");
		//goto end;
		avformat_close_input(&inputfile);
	}
	int64_t *dts_start_from = (int64_t*)malloc(sizeof(int64_t) * inputfile->nb_streams);//动态分配内存空间
	memset(dts_start_from, 0, sizeof(int64_t) * inputfile->nb_streams);//内存空间初始化
	int64_t *pts_start_from = (int64_t*)malloc(sizeof(int64_t) * inputfile->nb_streams);
	memset(pts_start_from, 0, sizeof(int64_t) * inputfile->nb_streams);
	while (1) {
		AVStream *in_stream, *out_stream;
		ret = av_read_frame(inputfile, &pkt);
		if (ret < 0)
			break;
		in_stream = inputfile->streams[pkt.stream_index];
		out_stream = outputfile->streams[pkt.stream_index];
		if (av_q2d(in_stream->time_base) * pkt.pts > end_seconds) {
			av_free_packet(&pkt);
			break;
		}
		if (dts_start_from[pkt.stream_index] == 0) {
			dts_start_from[pkt.stream_index] = pkt.dts;//记录裁剪初始位置的DTS  pkt.stream_index 为了区分音频流和视频流等信息
			printf("dts_start_from:\n");
		}
		if (pts_start_from[pkt.stream_index] == 0) {
			pts_start_from[pkt.stream_index] = pkt.pts;//记录裁剪初始位置的PTS
			printf("pts_start_from:\n");
		}
		/* copy packet */ // 时间基转换函数求出新的位置下的PTS\DTS
		pkt.pts = av_rescale_q_rnd(pkt.pts - pts_start_from[pkt.stream_index], 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 - dts_start_from[pkt.stream_index], in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
		if (pkt.pts < 0) {
			pkt.pts = 0;
		}
		if (pkt.dts < 0) {
			pkt.dts = 0;
		}
		pkt.duration = (int)av_rescale_q((int64_t)pkt.duration, in_stream->time_base, out_stream->time_base);
		pkt.pos = -1;
		printf("\n");
		ret = av_interleaved_write_frame(outputfile, &pkt);
		if (ret < 0) {
			fprintf(stderr, "Error muxing packet\n");
			break;
		}
		av_free_packet(&pkt);
	}
	//释放资源
	free(dts_start_from);
	free(pts_start_from);

	//写文件尾信息
	av_write_trailer(outputfile);
}
int main(int argc, char const *argv[])
{
	cutVideo(39.0, 100.0, "C:\\Users\\haizhengzheng\\Desktop\\mercury.mp4", "C:\\Users\\haizhengzheng\\Desktop\\mercut.mp4");
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值