opencv - 从指定的起点和终点剪辑 C++ OpenCV 中的视频

标签 opencv video ffmpeg

我想从指定的起点和终点剪辑 C++ OpenCV 中的视频。我知道我们可以为此目的使用以下 VideoCapture 属性。

CV_CAP_PROP_POS_MSEC or `CV_CAP_PROP_POS_FRAMES`

但是,我如何使用上述任何一个属性准确剪辑视频并将其写入另一个视频文件?我找不到任何方法可以从指定的相对位置(以毫秒为单位)或从相对帧索引读取视频文件。

最佳答案

我最近正在研究一些像这样的简单工具。它为我构建和运行没有问题。干得好:

// Code borrowed heavily from the OpenCV samples

#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/videoio.hpp>

#include <cctype>
#include <stdio.h>
#include <string.h>
#include <time.h>

using namespace cv;
using namespace std;
int main( int argc, char** argv )
{    
    // Command line argument parsing
    cv::CommandLineParser parser(argc, argv,
        "{help h usage ?|               | print this message        }"
        "{@vid_name     |               | video file                }"
        "{@frame_num    | 0             | start frame               }"
        "{@frame_count  | 100           | frames to extract         }"
        );

    if (parser.has("help"))
    {
        parser.printMessage();
        return 0;
    }

    string inputFilename = parser.get<string>("@vid_name");
    int frameNumber = parser.get<int>("@frame_num");
    int frameCount = parser.get<int>("@frame_count");
    if (!parser.check())
    {
        parser.printMessage();
        parser.printErrors();
        return -1;
    }

    // Open video
    VideoCapture capture;
    capture.open(inputFilename);
    if ( !capture.isOpened() )
    {
        fprintf(stderr, "Failed to open video\n");
        return -1;
    }
    double fps = capture.get(CV_CAP_PROP_FPS);
    size_t width = (size_t)capture.get(CV_CAP_PROP_FRAME_WIDTH);
    size_t height = (size_t)capture.get(CV_CAP_PROP_FRAME_HEIGHT);
    Size imgSz(width,height);
    size_t totalFrames = (size_t)capture.get(CV_CAP_PROP_FRAME_COUNT);

    // Open output video files        
    VideoWriter vidwriter;
    int codec = CV_FOURCC('M', 'J', 'P', 'G');
    vidwriter.open("clip.avi", codec, fps, imgSz, true);
    if ( !vidwriter.isOpened() ) 
    {
        fprintf(stderr, "Could not open an output video file for write\n");
        return -1;
    }

    // Extract subset of frames
    Mat frame;
    capture.set(CV_CAP_PROP_POS_FRAMES, frameNumber);
    for (int frameInd = frameNumber; frameInd < frameNumber+frameCount; ++frameInd)
    {
        capture >> frame;
        if ( frame.empty() ) 
            break;
        vidwriter.write(frame);
    }
    capture.release();

    printf("Success.\n");

    return 0;
}

关于opencv - 从指定的起点和终点剪辑 C++ OpenCV 中的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44461630/

相关文章:

opencv - opencv 级联分类器的 stage.xml 和 cascade.xml 中值的含义是什么

android - 在 Android 中使用 FFMpeg 稳定视频

command-line - ffmpeg cutet 视频在最后几秒失去声音

c++ - 从gpu特征描述符转换的opencv特征描述符的问题

c++ - 如何使用带有 Code::Blocks 的 OpenCV 2.4.3 编译程序?

iphone - 将图像保存到 iPhone 时出错

video - 找出 m3u8 url 引用视频的文件大小

objective-c - iOS Movie Player 可以在多大程度上进行自定义和样式化?

ios - 在 iOS 上隐藏 HTML5 <video> 播放按钮

ffmpeg - 将 PCM 音频复用到 WMA 会显着增加文件大小