视频逆序
方法一:调用ffmpeg命令
ffmpeg -i "D:/desktop/1095594024.mp4" -vf reverse -af areverse "D:/desktop/reversed.mp4"
方法二 :opencv
注:opencv 无法处理音频。所以用opencv逆序后 视频没有声音。仅有视频。
#include<opencv2/opencv.hpp> #include <opencv2/core.hpp> #include <iostream> using namespace cv; using namespace std; int main() { // 打开视频文件 string videoPath = "D:/desktop/10123754.mp4"; cv::VideoCapture cap(videoPath); if (!cap.isOpened()) { std::cerr << "Error: Unable to open the video file." << std::endl; return -1; } // 获取视频帧数和帧率 int frameCount = cap.get(cv::CAP_PROP_FRAME_COUNT); double fps = cap.get(cv::CAP_PROP_FPS); // 创建视频编写器 cv::VideoWriter writer("D:/desktop/reversed.mp4", cv::VideoWriter::fourcc('X', '2', '6', '4'), fps, cv::Size(cap.get(cv::CAP_PROP_FRAME_WIDTH), cap.get(cv::CAP_PROP_FRAME_HEIGHT))); // 从最后一帧开始逆序处理并写入新视频 for (int i = frameCount - 1; i >= 0; i--) { cap.set(cv::CAP_PROP_POS_FRAMES, i); cv::Mat frame; cap >> frame; if (frame.empty()) break; writer.write(frame); } // 释放资源 cap.release(); writer.release(); std::cout << "Video reversed and saved as MP4 successfully." << std::endl; return 0; }#c++##视频##opencv##ffmpeg#