Search code examples
c++opencvvideovisual-studio-2013slowmotion

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


I have a 100fps .mp4 GoPro video and I want to create from it a slow Motion one with 25fps. I'm trying since two days but to no avail. I could play the video, save a video from the GoPro's WiFi stream, but when I try to read the 100fps and save it in another video file in 25fps I get empty files! I'm suspecting the Codec used to encodee the new mp4 video, but I'm not sure.

Here's the code (I use OpenCV 3.0.0 with Visual C++ on Visual Studio 2013 Community on Windows 10 preview).

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

Here are the results:

output

Output


Solution

  • As I suspected, it's the Coded! I used many of them, but then I found this question: Create Video from images using VideoCapture (OpenCV) then I used the coded MJPG in:

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

    and it worked!

    Here's the result: enter image description here