'C++ | OpenCV unable to capture webcam and resize into different frame, then save as mp4 @ 60 fps

I am currently trying to record a video from a webcam (Logitech StreamCam Pro - Full HD) and then writing the captured feed into an .mp4 file. However, when I open the file with VLC, I get no playback. Just the VLC logo, with video length being 00:00:00 / 00:00:00.

I have noticed that if I change the extension from .mp4 into .avi that it does actually play back in VLC, however, at a ludacris speed (fps), much higher than the 60 fps of the camera.

TLDR - I am unable to play the .mp4 file of my webcam feed 1920x1080 @ 60 fps, and I am wondering if anyone can help me with the problem.

C++ code block below, containing the logic

int MediaCapture::RecordFeed(int cameraID)
{
    std::cout << "Camera selected: " << cameraID << std::endl;

    // Define total frames and start of a counter for FPS calculation
    int totalFrames = 0;

    cv::VideoCapture *capture;
    capture = new cv::VideoCapture(2); 

    if(!capture->isOpened()){
        std::cout << "Error opening video stream or file" << std::endl;
        return -1;
    }

    int frameWidth = capture->get(cv::CAP_PROP_FRAME_WIDTH);
    int frameHeight = capture->get(cv::CAP_PROP_FRAME_HEIGHT);
    int cameraFPS = capture->get(cv::CAP_PROP_FPS);

    cv::VideoWriter video(fs::current_path().string() + "/assets/videos/recording.mp4",video.fourcc('m','p','4','v'),cameraFPS,cv::Size(frameWidth,frameHeight));
    
    int tf = 0;
    time_t start, end;
    time(&start);

    cv::Mat frame;
    cv::Mat resizedFrame;
    
    while (capture->read(frame))
    {
        tf++;
        
        cv::resize(frame,resizedFrame,cv::Size(1920,1080));
        
        std::cout << "Height:" << resizedFrame.size().height << std::endl;
        std::cout << "Width:" << resizedFrame.size().width << std::endl;

        video.write(resizedFrame);

        if (cv::waitKey(1) >= 0)
        {
            break;
        }
    }
    
    // When everything done, release the video capture and write object
    capture->release();
    video.release();

    // Closes all the windows
    cv::destroyAllWindows();
    return 0;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source