C++ 指定起始帧数、结束帧数、
剪辑视频
C++ 无法直接用H264,只能用avi编码格式
#include <iostream>
#include <opencv2/opencv.hpp>int main() {// 读取视频:创建了一个VideoCapture对象,参数为摄像头编号std::string path = "E:/zyk_lab/pai/";std::string mp4_name = "04010003569000000.mp4";std::string inputVideoPath = path + mp4_name;std::string outputVideoPath = path + "img/" + "ce.avi";cv::VideoCapture inputVideo(inputVideoPath);if (!inputVideo.isOpened()) {std::cerr << "Error opening input video." << std::endl;return -1;}cv::Size frameSize(static_cast<int>(inputVideo.get(cv::CAP_PROP_FRAME_WIDTH)),static_cast<int>(inputVideo.get(cv::CAP_PROP_FRAME_HEIGHT)));double fps = inputVideo.get(cv::CAP_PROP_FPS);cv::VideoWriter outputVideo(outputVideoPath, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), fps, frameSize);int currentFrame = 0;cv::Mat frame;double startFrame = ((60 * 48) + 22) * fps; // 开始帧double endFrame = ((60 * 48) + 55) * fps; // 结束帧while (inputVideo.read(frame)) {if (currentFrame >= (int)startFrame && currentFrame <= (int)endFrame) {outputVideo.write(frame);}currentFrame++;std::cout << std::to_string(currentFrame) << std::endl;if (currentFrame > (int)endFrame) {break;}}inputVideo.release();outputVideo.release();std::cout << "结束" << std::endl;return 0;
}