【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使用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

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF

基于Linux的ffmpeg python的关键帧抽取

《基于Linux的ffmpegpython的关键帧抽取》本文主要介绍了基于Linux的ffmpegpython的关键帧抽取,实现以按帧或时间间隔抽取关键帧,文中通过示例代码介绍的非常详细,对大家的学... 目录1.FFmpeg的环境配置1) 创建一个虚拟环境envjavascript2) ffmpeg-py

电脑提示xlstat4.dll丢失怎么修复? xlstat4.dll文件丢失处理办法

《电脑提示xlstat4.dll丢失怎么修复?xlstat4.dll文件丢失处理办法》长时间使用电脑,大家多少都会遇到类似dll文件丢失的情况,不过,解决这一问题其实并不复杂,下面我们就来看看xls... 在Windows操作系统中,xlstat4.dll是一个重要的动态链接库文件,通常用于支持各种应用程序

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Golang 日志处理和正则处理的操作方法

《Golang日志处理和正则处理的操作方法》:本文主要介绍Golang日志处理和正则处理的操作方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录1、logx日志处理1.1、logx简介1.2、日志初始化与配置1.3、常用方法1.4、配合defer