How to save video from camera?

I want to save video from camera to local disk. How may I do it (using some existing tool or C/C++ and SDK)? I use Buildroot Linux. Thanks!

BTW saving not video, but still images variant is also interesting.


English version (Polite and Formal):
Regrettably, if you wish to utilize opencv-mobile for video saving, the video writing functionality within the videoIO module has been deprecated in the milky-duo version of opencv-mobile. This decision may have been influenced by the significant computational resources required for video encoding. However, there are two alternatives you might consider: one option is to recompile opencv yourself, ensuring the video writing capabilities of the videoio module are enabled in the process; another is to save the images in a sequence format and then upload them to a PC for video encoding by using FFMPEG. Here is an example of saving images in sequence and encoding them into a video:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <unistd.h>   // For sleep()
#include <string>     // For std::string, std::to_string

int main()
    cv::VideoCapture cap;
    cap.set(cv::CAP_PROP_FRAME_WIDTH, 640);
    cap.set(cv::CAP_PROP_FRAME_HEIGHT, 360);;

    if (!cap.isOpened()) {
        fprintf(stderr, "Error: Couldn't open video capture device.\n");
        return -1;

    const int w = cap.get(cv::CAP_PROP_FRAME_WIDTH);
    const int h = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
    fprintf(stderr, "%d x %d\n", w, h);

    cv::Mat frame;
    int frameNumber = 0; // Counter for frame number

    while (true)
        cap >> frame; // Capture a frame from the video device
        if (frame.empty()) break; // If no frame is captured, exit the loop

        // Construct the filename for the frame, format: frame_<number>.jpg
        std::string filename = "frame_" + std::to_string(frameNumber++) + ".jpg";

        // Save the current frame to file
        cv::imwrite(filename, frame);

        // Break the loop if 'q' is pressed
        if (cv::waitKey(1) == 'q') break;

    cap.release(); // Release video device and resources

    return 0;


exaples for ffmpeg:

ffmpeg -framerate 10 -i /path/to/your/images/img%03d.jpg -c:v libx264 -pix_fmt yuv420p slideshow.mp4

Thank you very much for your help and the provided code samples, @ericz !!!

1 Like

Hey everyone,

I’m using this code sample provided by @ericz, however the latency is way too high for my use case (avg 800ms per image). Is there a way to speed up the process?

1 Like

Glad to see you could work with ffmpeg on this tiny board! Have you tried to capture video from duo’s camera using this framework? I’ve spent a lot of time to figure out how to do that, but to no avail.

1 Like