【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

相关文章

Spring Boot 中的默认异常处理机制及执行流程

《SpringBoot中的默认异常处理机制及执行流程》SpringBoot内置BasicErrorController,自动处理异常并生成HTML/JSON响应,支持自定义错误路径、配置及扩展,如... 目录Spring Boot 异常处理机制详解默认错误页面功能自动异常转换机制错误属性配置选项默认错误处理

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

Java堆转储文件之1.6G大文件处理完整指南

《Java堆转储文件之1.6G大文件处理完整指南》堆转储文件是优化、分析内存消耗的重要工具,:本文主要介绍Java堆转储文件之1.6G大文件处理的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言文件为什么这么大?如何处理这个文件?分析文件内容(推荐)删除文件(如果不需要)查看错误来源如何避

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

Java docx4j高效处理Word文档的实战指南

《Javadocx4j高效处理Word文档的实战指南》对于需要在Java应用程序中生成、修改或处理Word文档的开发者来说,docx4j是一个强大而专业的选择,下面我们就来看看docx4j的具体使用... 目录引言一、环境准备与基础配置1.1 Maven依赖配置1.2 初始化测试类二、增强版文档操作示例2.

MyBatis-Plus通用中等、大量数据分批查询和处理方法

《MyBatis-Plus通用中等、大量数据分批查询和处理方法》文章介绍MyBatis-Plus分页查询处理,通过函数式接口与Lambda表达式实现通用逻辑,方法抽象但功能强大,建议扩展分批处理及流式... 目录函数式接口获取分页数据接口数据处理接口通用逻辑工具类使用方法简单查询自定义查询方法总结函数式接口

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2