Compressing in h265

I’ve been trying to encode a mp4 video using h265, however when I compile the code, it returns the following error: OpenCV: FFMPEG: tag 0x35363268/‘h265’ is not found (format ‘mp4 / MP4 (MPEG-4 Part 14)’)’

The sample code is:

#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main(){
    VideoCapture inputVideo("Sunflower.mp4");
    if (!inputVideo.isOpened()){
        cerr << "Error: Could not open input video file" << endl;
        return -1;

    Size frameSize((int) inputVideo.get(CAP_PROP_FRAME_WIDTH),
                    (int) inputVideo.get(CAP_PROP_FRAME_HEIGHT));
    VideoWriter outputVideo("ouput_video.mp4", 
                            // VideoWriter::fourcc(*'H264'),
                            VideoWriter::fourcc('h', '2', '6', '5'),
                            inputVideo.get(CAP_PROP_FPS), frameSize);

    Mat frame;
        // Here we could apply filters, or modify content

        // Write the frame to the output video
    return 0;

that’s because the proper fourcc is hvc1


But now I have the following error :

[hevc_mf @ 00000223295094c0] could not find any MFT for the given media type
[hevc_mf @ 00000223295094c0] could not create MFT
[ERROR:0@0.085] global cap_ffmpeg_impl.hpp:3076 open Could not open codec hevc_mf, error: Unspecified error (-40)
[ERROR:0@0.085] global cap_ffmpeg_impl.hpp:3093 open VIDEOIO/FFMPEG: Failed to initialize VideoWriter
Error: Could not open output file for writing.

Is there anything you could recommend?

hm that’s new.

some googling suggests that ffmpeg tried to use MS Media Foundation to satisfy the format request. it didn’t try anything else, so this ffmpeg probably lacks the x265 encoder.

licensing of H.265 is a headache. the ffmpeg coming with opencv is not guaranteed to have encoders for such formats.

you could browse the issues on opencv’s github, as well as this forum. there may be guidance on how to select a specific codec, or even request a hardware codec (GPU, CPU) to do this.

what are the hardware specs of your computer?

IIRC opencv can use an ffmpeg of your choice. you could then scare up an ffmpeg that was built with all the popular codecs. you’ll just have to get it with development files. I don’t know if I ever did that. if it’s possible, there will be discussions about that on this forum.