Can't read video saved with videoWriter

Hi,
I’m would like to save a video obtained from another video after applying some filters and functions.
My problem is that, once I saved the video, I’m unable to read it using a media player (Windows Media Player can’t open it and VLC just start and don’t display anything) .
Here is my code :

int videoProcessing(char videoPath[], char destVideoPath[], int thresh,bool save)
{
    vector<vector<Point>> contours;
    VideoCapture cap(videoPath, CAP_ANY);
    VideoWriter out;
    Mat imgOut;
    Mat imgBin;
    Size frameSize;

    // Check if camera opened successfully
    if (!cap.isOpened()) 
    {
   
        cout << "Error opening video stream or file" << endl;
        return -1;
        
    }

    while (cap.isOpened())
    {
        Mat frame;
        cap >> frame;
        if (save == true && !out.isOpened())//If we decide to record the output
        {
            out.open(destVideoPath, VideoWriter::fourcc('M', 'J', 'P', 'G'), (double)40, frame.size(), false);

        }

        
        if (frame.empty())
        {
            cap.release();
            destroyAllWindows();

            return 0;
        }
        else
        {
            if(toBinary(&frame,&imgBin,&thresh) == 0)
            {
                shapeDetect(&imgBin,&imgOut,&contours);

                out.write(frame);
                imshow("Video", imgOut);
                waitKey(25);
            }
            else
            {
                imshow("Video", frame);
                waitKey(25);
            }

        }
        // Press  ESC on keyboard to exit
        char c = (char)waitKey(25);
        
        if (c == 27) break;

    }
    cap.release();
    destroyAllWindows();

    return 0;
}

I looked at this example to write mine. The file I create ends with the .avi extension.

I think the problem might come from the codec, I don’t really understand how to work with codecs.
Should I have one installed on my computer ? Or do I have to choose one depending on my OS ?

I’m working on Windows 10 and I use the lastest OpenCV version 4.6.0

where do you call out.release()?

I wasn’t since it isn’t called in the example I read, but adding a call to out.release() didn’t fix my problem tho,

what example did you read? I’d like a link to it.

what’s the size of the resulting file in bytes?

what is the value of destVideoPath?

why do you pass false for isColor?

what is the width, height, number of channels, and element type of frame?

There is a link in my first post (this) : OpenCV: samples/cpp/videowriter_basic.cpp

The resulting file size is 17.5 Kbytes. DestVideoPath is : “C:\temp\data\BinaryVideo08_24_39.avi”
I forgot to say that this function is placed inside a dll called by a C# program.
isColor is false as the output after the process I use is colorless.

Anyway, I spotted the problem, the thing is that I used the variable ‘frame’ instead of imgOut (a careless mistake of mine) when calling out.write(), frame being a coloured image. Now that I changed it, it works fine.
I noticed that the file is created but doesn’t work if isColor is set to false with a colored frame and vice versa. I thought it would work on both sides …

Thank you for your help ^^

the isColor information is required at instantiation because it determines how the video codec is initialized. during operation (write() calls), you must give color/grayscale as you promised during instantiation. there is no automatic conversion.