使用 C++/OpenCV 从 100fps GoPro .mp4 视频创建 25fps 慢动作视频

Creating a 25fps slow motion video from a 100fps GoPro .mp4 video with C++/OpenCV

我有一个 100fps .mp4 GoPro 视频,我想用它创建一个 25fps 的慢动作视频。 我已经尝试了两天,但无济于事。我可以播放视频,从 GoPro 的 WiFi 流中保存视频,但是当我尝试读取 100fps 并将其保存在另一个 25fps 的视频文件中时,我得到空文件! 我怀疑用于编码新 mp4 视频的编解码器,但我不确定。

这是代码(我在 Visual Studio 2013 社区 Windows 10 预览版上使用 OpenCV 3.0.0 和 Visual C++)。

#include <iostream>
#include <vector>
#include <random>
#include <functional>  
#include <algorithm>   
#include <string>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;

int  main()
{
    VideoCapture inputVideo("GOPR1016.MP4");   // Open the video File
if (!inputVideo.isOpened()) {
    cout << "Error opening the video" << endl;
    return -1;  
}

int frames_num = int(inputVideo.get(CAP_PROP_FRAME_COUNT));  // Get the number of frames in the video
cout << "Num of frames: " << frames_num << endl;
int fps = int(inputVideo.get(CAP_PROP_FPS));  // get the frame rate
cout << "FPS: " << fps << endl;
int frame_width = inputVideo.get(CAP_PROP_FRAME_WIDTH);
int frame_height = inputVideo.get(CAP_PROP_FRAME_HEIGHT);

VideoWriter outputVideo;
string name = "outputVideo.avi";
Size size = Size((int)inputVideo.get(CAP_PROP_FRAME_WIDTH), (int)inputVideo.get(CAP_PROP_FRAME_HEIGHT)); // get the resolution
outputVideo.open(name, CV_FOURCC('3', 'I', 'V', 'X'), 25, size, true); // create a new videoFile with 25fps

Mat src;
for (int i = 0; i < frames_num; i++)
{
    inputVideo >> src; // read 
    if (src.empty()) {
        break; // in case ther's nothing to read
    }
    outputVideo << src;   // write
}

waitKey(0); // key press to close window    
return 1;
}

结果如下:

如我所料,是 Coded!我用了很多,但后来我发现了这个问题:Create Video from images using VideoCapture (OpenCV) 然后我在以下位置使用了编码的 MJPG:

outputVideo.open(name, CV_FOURCC('M', 'J', 'P', 'G'), 25, size, true); // create a new videoFile with 25fps

成功了!

结果如下: