Search code examples
raspberry-picameravideo-encodinghevch.265

H.265 encoding slow on Raspberry Pi 4


OpenCV is so slow on video encoding, like 20 frames per minute, (On 8GB Pi4, 256MB GPU memory, 850MHz GPU Clock, and 2147MHz on CPU Clock)

I think, threading it to one core for taking frames, another one for saving to video will make a better performance but I don't know how I can do it

The code:

#include <opencv4/opencv2/opencv.hpp>
using namespace cv;

int main() {
    Mat image;
    VideoWriter video("outcpp.mp4", cv::VideoWriter::fourcc('H','V','C','1'), 30, Size(640, 480));
    namedWindow("main");
    VideoCapture cap(0);
    while (1) {
        cap >> image;
        video.write(image);
        imshow("main", image);
        if ((char)waitKey(1) == 27)
            break;
    }
    cap.release();
}

Manjaro ARM, OpenCV with FFMPEG.

First post, if anything is wrong, kindly write, thanks.


Solution

  • You might also need to ensure you are using the hardware video encoding supported by your Pi rather than compressing on the CPU

    Thanks to Alan Birtles, hardware encoding on Pi is way faster than CPU encoding, for who needs, the code (changes are noted in the comment lines):

    Edit: RPi 4 only have OpenMax and MMAL Hardware accel. on FFMpeg, and it looks like OpenCV doesn't supporting them, so, solution is using the cheaper codecs

    #include <opencv4/opencv2/opencv.hpp>
    using namespace cv;
    
    int main() {
        Mat image;
        // Video format changed to '.mkv' and FourCC changed to X264
        VideoWriter video("outcpp.mkv", cv::VideoWriter::fourcc('X', '2', '6', '4'), 30, Size(640, 480));
        VideoCapture cap(0);
        int i = 0;
        // 'i' variable is only for recording 10 seconds, ignore it
        while (i < 300) {
            i++;
            cap >> image;
            imshow("main", image);
            video.write(image);
        }
        cap.release();
        video.release();
    }```