DepthAI
Software Stack

ON THIS PAGE

  • Pipeline
  • Source code

Video record

Supported on:RVC2RVC4
This example demonstrates how to record a video stream and showcases how to use the RecordVideo node for capturing and saving frames.This example requires the DepthAI v3 API, see installation instructions.

Pipeline

Source code

Python

Python
GitHub
1import depthai as dai
2import argparse
3import time
4import signal
5from pathlib import Path
6
7parser = argparse.ArgumentParser()
8parser.add_argument("-o", "--output", default="test_video", help="Output file name (without extension)")
9
10args = parser.parse_args()
11
12with dai.Pipeline() as pipeline:
13    def signal_handler(sig, frame):
14        print("Interrupted, stopping the pipeline")
15        pipeline.stop()
16    signal.signal(signal.SIGINT, signal_handler)
17
18    cam = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_A)
19
20    videoEncoder = pipeline.create(dai.node.VideoEncoder).build(cam.requestOutput((1280, 720), dai.ImgFrame.Type.NV12))
21    videoEncoder.setProfile(dai.VideoEncoderProperties.Profile.H264_MAIN)
22
23    record = pipeline.create(dai.node.RecordVideo)
24    record.setRecordVideoFile(Path(f"{args.output}.mp4"))
25
26    videoEncoder.out.link(record.input)
27
28    pipeline.start()
29    print("Recording video. Press Ctrl+C to stop.")
30    while pipeline.isRunning():
31        time.sleep(1)

C++

1#include <atomic>
2#include <chrono>
3#include <csignal>
4#include <filesystem>
5#include <iostream>
6#include <memory>
7#include <thread>
8
9#include "depthai/depthai.hpp"
10
11// Global flag for graceful shutdown
12std::atomic<bool> quitEvent(false);
13
14// Signal handler
15void signalHandler(int signum) {
16    quitEvent = true;
17}
18
19int main(int argc, char** argv) {
20    // Parse command line arguments
21    std::string outputFile = "test_video";
22    for(int i = 1; i < argc; i++) {
23        std::string arg = argv[i];
24        if(arg == "-o" || arg == "--output") {
25            if(i + 1 < argc) {
26                outputFile = argv[++i];
27            }
28        }
29    }
30
31    // Set up signal handlers
32    signal(SIGTERM, signalHandler);
33    signal(SIGINT, signalHandler);
34
35    try {
36        // Create pipeline
37        dai::Pipeline pipeline;
38
39        // Create color camera node
40        auto cam = pipeline.create<dai::node::Camera>()->build(dai::CameraBoardSocket::CAM_A);
41
42        // Create video encoder node
43        auto videoEncoder = pipeline.create<dai::node::VideoEncoder>();
44        videoEncoder->setProfile(dai::VideoEncoderProperties::Profile::H264_MAIN);
45        cam->requestOutput(std::make_pair(1280, 720), dai::ImgFrame::Type::NV12)->link(videoEncoder->input);
46
47        // Create record node
48        auto record = pipeline.create<dai::node::RecordVideo>();
49        record->setRecordVideoFile(std::filesystem::path(outputFile + ".mp4"));
50        videoEncoder->out.link(record->input);
51
52        // Start pipeline
53        pipeline.start();
54        std::cout << "Recording video. Press Ctrl+C to stop." << std::endl;
55
56        // Main loop
57        while(pipeline.isRunning() && !quitEvent) {
58            std::this_thread::sleep_for(std::chrono::seconds(1));
59        }
60
61        // Cleanup
62        pipeline.stop();
63        pipeline.wait();
64
65    } catch(const std::exception& e) {
66        std::cerr << "Error: " << e.what() << std::endl;
67        return 1;
68    }
69
70    return 0;
71}

Need assistance?

Head over to Discussion Forum for technical support or any other questions you might have.