-1

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.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • How to write multithreaded programs is a very large topic. You might have more luck searching for a tutorial than asking here. – john Sep 10 '22 at 19:38
  • There is a lot of tutorials, but the problem is, slow on Pi. – bktech2021 Sep 10 '22 at 19:42
  • At a guess you are running a debug build, use an optimised build. You might also need to ensure you are using the hardware video encoding supported by your Pi rather than compressing on the CPU – Alan Birtles Sep 10 '22 at 19:43
  • don't even go there. the issue isn't multithreading. do not give desperate ideas much weight. the issue is that the raspberry pi 4 is SLOW. It's NOT an OpenCV issue. OpenCV uses ffmpeg, which probably does a CPU encode, not using any hardware codecs unless told to. – Christoph Rackwitz Sep 10 '22 at 20:44

1 Answers1

1

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();
}```