我想从指定的起点和终点剪辑 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/