【FFmpeg学习】视频变慢处理

2024-02-15 17:36

本文主要是介绍【FFmpeg学习】视频变慢处理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

视频慢动作处理是个比较常用的操作,可以在播放的时候处理,这里我们考虑把视频修改为慢动作,使用ffmpeg命令,可以这样

ffmpeg -i test.mp4 -vf "setpts=5*PTS" -an test_slow3.mp4

这里把视频放慢了5倍,生成的文件大小也变大了几倍。

怎么用程序来实现呢,参考一下GPT给出的code,

#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/opt.h>
}// 慢速播放的速度因子
const double SLOWDOWN_FACTOR = 2.0;int main(int argc, char* argv[]) {if (argc < 3) {std::cout << "Usage: " << argv[0] << " input_file output_file" << std::endl;return 1;}const std::string inputFileName = argv[1];const std::string outputFileName = argv[2];av_register_all();AVFormatContext* inputFormatContext = nullptr;if (avformat_open_input(&inputFormatContext, inputFileName.c_str(), nullptr, nullptr) != 0) {std::cerr << "Failed to open input file: " << inputFileName << std::endl;return 1;}if (avformat_find_stream_info(inputFormatContext, nullptr) < 0) {std::cerr << "Failed to retrieve input stream information" << std::endl;avformat_close_input(&inputFormatContext);return 1;}AVFormatContext* outputFormatContext = nullptr;if (avformat_alloc_output_context2(&outputFormatContext, nullptr, nullptr, outputFileName.c_str()) < 0) {std::cerr << "Failed to allocate output format context" << std::endl;avformat_close_input(&inputFormatContext);return 1;}for (unsigned int i = 0; i < inputFormatContext->nb_streams; ++i) {AVStream* inputStream = inputFormatContext->streams[i];AVStream* outputStream = avformat_new_stream(outputFormatContext, nullptr);if (!outputStream) {std::cerr << "Failed to allocate output stream" << std::endl;avformat_close_input(&inputFormatContext);avformat_free_context(outputFormatContext);return 1;}if (avcodec_parameters_copy(outputStream->codecpar, inputStream->codecpar) < 0) {std::cerr << "Failed to copy codec parameters" << std::endl;avformat_close_input(&inputFormatContext);avformat_free_context(outputFormatContext);return 1;}outputStream->time_base = inputStream->time_base;}if (!(outputFormatContext->oformat->flags & AVFMT_NOFILE)) {if (avio_open(&outputFormatContext->pb, outputFileName.c_str(), AVIO_FLAG_WRITE) < 0) {std::cerr << "Failed to open output file: " << outputFileName << std::endl;avformat_close_input(&inputFormatContext);avformat_free_context(outputFormatContext);return 1;}}if (avformat_write_header(outputFormatContext, nullptr) < 0) {std::cerr << "Failed to write output file header" << std::endl;avformat_close_input(&inputFormatContext);avformat_free_context(outputFormatContext);return 1;}AVPacket packet;while (av_read_frame(inputFormatContext, &packet) >= 0) {AVStream* outputStream = outputFormatContext->streams[packet.stream_index];// 慢速播放的时间戳调整packet.pts *= static_cast<int64_t>(SLOWDOWN_FACTOR);packet.dts *= static_cast<int64_t>(SLOWDOWN_FACTOR);packet.duration = static_cast<int>(packet.duration * SLOWDOWN_FACTOR);av_interleaved_write_frame(outputFormatContext, &packet);av_packet_unref(&packet);}av_write_trailer(outputFormatContext);avformat_close_input(&inputFormatContext);avformat_free_context(outputFormatContext);return 0;
}

这个代码执行后并没有实现变慢,参考一下,进行修改后可以实现慢动作处理,如下


int slow() {std::string filename = "test.mp4";     // 输入MP4文件名std::string outputFilename = "test_slow.mp4";  // 输出图片文件名AVFormatContext* ofmt_ctx = nullptr;avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, outputFilename.c_str());const AVOutputFormat* ofmt = ofmt_ctx->oformat;AVFormatContext* formatContext = nullptr;if (avformat_open_input(&formatContext, filename.c_str(), nullptr, nullptr) != 0) {std::cerr << "Error opening input file" << std::endl;return -1;}if (avformat_find_stream_info(formatContext, nullptr) < 0) {std::cerr << "Error finding stream information" << std::endl;avformat_close_input(&formatContext);return -1;}const AVCodec* codec = nullptr;int videoStreamIndex = -1;for (unsigned int i = 0; i < formatContext->nb_streams; i++) {if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {videoStreamIndex = i;codec = avcodec_find_decoder(formatContext->streams[i]->codecpar->codec_id);//AVStream* out_stream = avformat_new_stream(ofmt_ctx, NULL);avcodec_parameters_copy(out_stream->codecpar, formatContext->streams[i]->codecpar);out_stream->codecpar->codec_tag = 0;break;}}avio_open(&ofmt_ctx->pb, outputFilename.c_str(), AVIO_FLAG_WRITE);avformat_write_header(ofmt_ctx, NULL);AVPacket packet;av_init_packet(&packet);// 计算目标时间戳int64_t targetTimestamp = targetSecond * AV_TIME_BASE;// 查找目标时间戳所对应的帧AVFrame* frame = av_frame_alloc();bool foundTargetFrame = false;int count = 0;while (av_read_frame(formatContext, &packet) >= 0) {if (packet.stream_index == videoStreamIndex) {AVStream* inputStream = formatContext->streams[packet.stream_index];AVStream* outputStream = ofmt_ctx->streams[packet.stream_index];// 计算新的时间戳cout << "1, pts=" << packet.pts << endl;packet.pts = av_rescale_q_rnd(packet.pts, inputStream->time_base, outputStream->time_base, AV_ROUND_NEAR_INF);packet.dts = av_rescale_q_rnd(packet.dts, inputStream->time_base, outputStream->time_base, AV_ROUND_NEAR_INF);packet.duration = av_rescale_q(packet.duration, inputStream->time_base, outputStream->time_base);packet.pos = -1;cout << "2, pts=" << packet.pts << endl;// 慢速播放的时间戳调整packet.pts *= 5;packet.dts *= 5;packet.duration *= 5;//          av_log(NULL, AV_LOG_INFO, "...av_write_frame(ofmt_ctx, packet);\n");int ret = av_write_frame(ofmt_ctx, &packet);if (ret < 0) {std::cerr << "Error av_write_frame" << std::endl;}count++;}av_packet_unref(&packet);}av_write_trailer(ofmt_ctx);return 1;
}

通过pts的修改来实现

这篇关于【FFmpeg学习】视频变慢处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/712060

相关文章

Python+FFmpeg实现视频自动化处理的完整指南

《Python+FFmpeg实现视频自动化处理的完整指南》本文总结了一套在Python中使用subprocess.run调用FFmpeg进行视频自动化处理的解决方案,涵盖了跨平台硬件加速、中间素材处理... 目录一、 跨平台硬件加速:统一接口设计1. 核心映射逻辑2. python 实现代码二、 中间素材处

Go异常处理、泛型和文件操作实例代码

《Go异常处理、泛型和文件操作实例代码》Go语言的异常处理机制与传统的面向对象语言(如Java、C#)所使用的try-catch结构有所不同,它采用了自己独特的设计理念和方法,:本文主要介绍Go异... 目录一:异常处理常见的异常处理向上抛中断程序恢复程序二:泛型泛型函数泛型结构体泛型切片泛型 map三:文

SpringSecurity中的跨域问题处理方案

《SpringSecurity中的跨域问题处理方案》本文介绍了跨域资源共享(CORS)技术在JavaEE开发中的应用,详细讲解了CORS的工作原理,包括简单请求和非简单请求的处理方式,本文结合实例代码... 目录1.什么是CORS2.简单请求3.非简单请求4.Spring跨域解决方案4.1.@CrossOr

requests处理token鉴权接口和jsonpath使用方式

《requests处理token鉴权接口和jsonpath使用方式》文章介绍了如何使用requests库进行token鉴权接口的处理,包括登录提取token并保存,还详述了如何使用jsonpath表达... 目录requests处理token鉴权接口和jsonpath使用json数据提取工具总结reques

C# 空值处理运算符??、?. 及其它常用符号

《C#空值处理运算符??、?.及其它常用符号》本文主要介绍了C#空值处理运算符??、?.及其它常用符号,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录一、核心运算符:直接解决空值问题1.??空合并运算符2.?.空条件运算符二、辅助运算符:扩展空值处理

浅析Python中如何处理Socket超时

《浅析Python中如何处理Socket超时》在网络编程中,Socket是实现网络通信的基础,本文将深入探讨Python中如何处理Socket超时,并提供完整的代码示例和最佳实践,希望对大家有所帮助... 目录开篇引言核心要点逐一深入讲解每个要点1. 设置Socket超时2. 处理超时异常3. 使用sele

SpringMVC配置、映射与参数处理​入门案例详解

《SpringMVC配置、映射与参数处理​入门案例详解》文章介绍了SpringMVC框架的基本概念和使用方法,包括如何配置和编写Controller、设置请求映射规则、使用RestFul风格、获取请求... 目录1.SpringMVC概述2.入门案例①导入相关依赖②配置web.XML③配置SpringMVC

解决docker目录内存不足扩容处理方案

《解决docker目录内存不足扩容处理方案》文章介绍了Docker存储目录迁移方法:因系统盘空间不足,需将Docker数据迁移到更大磁盘(如/home/docker),通过修改daemon.json配... 目录1、查看服务器所有磁盘的使用情况2、查看docker镜像和容器存储目录的空间大小3、停止dock

Vue3视频播放组件 vue3-video-play使用方式

《Vue3视频播放组件vue3-video-play使用方式》vue3-video-play是Vue3的视频播放组件,基于原生video标签开发,支持MP4和HLS流,提供全局/局部引入方式,可监听... 目录一、安装二、全局引入三、局部引入四、基本使用五、事件监听六、播放 HLS 流七、更多功能总结在 v

5 种使用Python自动化处理PDF的实用方法介绍

《5种使用Python自动化处理PDF的实用方法介绍》自动化处理PDF文件已成为减少重复工作、提升工作效率的重要手段,本文将介绍五种实用方法,从内置工具到专业库,帮助你在Python中实现PDF任务... 目录使用内置库(os、subprocess)调用外部工具使用 PyPDF2 进行基本 PDF 操作使用