自学内容网 自学内容网

C++ 起始帧数、结束帧数、剪辑视频

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;
}



原文地址:https://blog.csdn.net/qq_42102546/article/details/142339066

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!