DepthAI v2 has been superseded by DepthAI v3. You are viewing legacy documentation.
DepthAI Tutorials
DepthAI API References

ON THIS PAGE

  • Similar samples:
  • Demo
  • Setup
  • Source code
  • Pipeline

Encoding Max Limit

This example shows how to set up the encoder node to encode the RGB camera and both grayscale cameras (of DepthAI/OAK-D) at the same time, having all encoder parameters set to maximum quality and FPS. The RGB is set to 4K (3840x2160) and the grayscale are set to 1280x720 each, all at 25FPS. Each encoded video stream is transferred over XLINK and saved to a respective file.Pressing Ctrl+C will stop the recording and then convert it using ffmpeg into an mp4 to make it playable. Note that ffmpeg will need to be installed and runnable for the conversion to mp4 to succeed.Be careful, this example saves encoded video to your host storage. So if you leave it running, you could fill up your storage on your host.It's a variation of RGB Encoding and RGB & Mono Encoding.

Similar samples:

Demo

Setup

Please run the install script to download all required dependencies. Please note that this script must be ran from git context, so you have to download the depthai-python repository first and then run the script
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
For additional information, please follow the installation guide.

Source code

Python

Python
GitHub
1#!/usr/bin/env python3
2
3import depthai as dai
4
5# Create pipeline
6pipeline = dai.Pipeline()
7
8# Define sources and outputs
9camRgb = pipeline.create(dai.node.ColorCamera)
10monoLeft = pipeline.create(dai.node.MonoCamera)
11monoRight = pipeline.create(dai.node.MonoCamera)
12ve1 = pipeline.create(dai.node.VideoEncoder)
13ve2 = pipeline.create(dai.node.VideoEncoder)
14ve3 = pipeline.create(dai.node.VideoEncoder)
15
16ve1Out = pipeline.create(dai.node.XLinkOut)
17ve2Out = pipeline.create(dai.node.XLinkOut)
18ve3Out = pipeline.create(dai.node.XLinkOut)
19
20ve1Out.setStreamName('ve1Out')
21ve2Out.setStreamName('ve2Out')
22ve3Out.setStreamName('ve3Out')
23
24# Properties
25camRgb.setBoardSocket(dai.CameraBoardSocket.CAM_A)
26camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_4_K)
27monoLeft.setCamera("left")
28monoRight.setCamera("right")
29
30# Setting to 26fps will trigger error
31ve1.setDefaultProfilePreset(25, dai.VideoEncoderProperties.Profile.H264_MAIN)
32ve2.setDefaultProfilePreset(25, dai.VideoEncoderProperties.Profile.H265_MAIN)
33ve3.setDefaultProfilePreset(25, dai.VideoEncoderProperties.Profile.H264_MAIN)
34
35# Linking
36monoLeft.out.link(ve1.input)
37camRgb.video.link(ve2.input)
38monoRight.out.link(ve3.input)
39
40ve1.bitstream.link(ve1Out.input)
41ve2.bitstream.link(ve2Out.input)
42ve3.bitstream.link(ve3Out.input)
43
44# Connect to device and start pipeline
45with dai.Device(pipeline) as dev:
46
47    # Output queues will be used to get the encoded data from the output defined above
48    outQ1 = dev.getOutputQueue('ve1Out', maxSize=30, blocking=True)
49    outQ2 = dev.getOutputQueue('ve2Out', maxSize=30, blocking=True)
50    outQ3 = dev.getOutputQueue('ve3Out', maxSize=30, blocking=True)
51
52    # Processing loop
53    with open('mono1.h264', 'wb') as fileMono1H264, open('color.h265', 'wb') as fileColorH265, open('mono2.h264', 'wb') as fileMono2H264:
54        print("Press Ctrl+C to stop encoding...")
55        while True:
56            try:
57                # Empty each queue
58                while outQ1.has():
59                    outQ1.get().getData().tofile(fileMono1H264)
60
61                while outQ2.has():
62                    outQ2.get().getData().tofile(fileColorH265)
63
64                while outQ3.has():
65                    outQ3.get().getData().tofile(fileMono2H264)
66            except KeyboardInterrupt:
67                break
68
69    print("To view the encoded data, convert the stream file (.h264/.h265) into a video file (.mp4), using commands below:")
70    cmd = "ffmpeg -framerate 25 -i {} -c copy {}"
71    print(cmd.format("mono1.h264", "mono1.mp4"))
72    print(cmd.format("mono2.h264", "mono2.mp4"))
73    print(cmd.format("color.h265", "color.mp4"))

C++

1#include <csignal>
2#include <iostream>
3
4// Includes common necessary includes for development using depthai library
5#include "depthai/depthai.hpp"
6
7// Keyboard interrupt (Ctrl + C) detected
8static std::atomic<bool> alive{true};
9static void sigintHandler(int signum) {
10    alive = false;
11}
12
13int main() {
14    using namespace std;
15    using namespace std::chrono;
16    std::signal(SIGINT, &sigintHandler);
17
18    // Create pipeline
19    dai::Pipeline pipeline;
20
21    // Define sources and outputs
22    auto camRgb = pipeline.create<dai::node::ColorCamera>();
23    auto monoLeft = pipeline.create<dai::node::MonoCamera>();
24    auto monoRight = pipeline.create<dai::node::MonoCamera>();
25    auto ve1 = pipeline.create<dai::node::VideoEncoder>();
26    auto ve2 = pipeline.create<dai::node::VideoEncoder>();
27    auto ve3 = pipeline.create<dai::node::VideoEncoder>();
28
29    auto ve1Out = pipeline.create<dai::node::XLinkOut>();
30    auto ve2Out = pipeline.create<dai::node::XLinkOut>();
31    auto ve3Out = pipeline.create<dai::node::XLinkOut>();
32
33    ve1Out->setStreamName("ve1Out");
34    ve2Out->setStreamName("ve2Out");
35    ve3Out->setStreamName("ve3Out");
36
37    // Properties
38    camRgb->setBoardSocket(dai::CameraBoardSocket::CAM_A);
39    camRgb->setResolution(dai::ColorCameraProperties::SensorResolution::THE_4_K);
40    monoLeft->setCamera("left");
41    monoRight->setCamera("right");
42
43    // Setting to 26fps will trigger error
44    ve1->setDefaultProfilePreset(25, dai::VideoEncoderProperties::Profile::H264_MAIN);
45    ve2->setDefaultProfilePreset(25, dai::VideoEncoderProperties::Profile::H265_MAIN);
46    ve3->setDefaultProfilePreset(25, dai::VideoEncoderProperties::Profile::H264_MAIN);
47
48    // Linking
49    monoLeft->out.link(ve1->input);
50    camRgb->video.link(ve2->input);
51    monoRight->out.link(ve3->input);
52
53    ve1->bitstream.link(ve1Out->input);
54    ve2->bitstream.link(ve2Out->input);
55    ve3->bitstream.link(ve3Out->input);
56
57    // Connect to device and start pipeline
58    dai::Device device(pipeline);
59
60    // Output queues will be used to get the encoded data from the output defined above
61    auto outQ1 = device.getOutputQueue("ve1Out", 30, true);
62    auto outQ2 = device.getOutputQueue("ve2Out", 30, true);
63    auto outQ3 = device.getOutputQueue("ve3Out", 30, true);
64
65    // The .h264 / .h265 files are raw stream files (not playable yet)
66    auto videoFile1 = ofstream("mono1.h264", ios::binary);
67    auto videoFile2 = ofstream("color.h265", ios::binary);
68    auto videoFile3 = ofstream("mono2.h264", ios::binary);
69    cout << "Press Ctrl+C to stop encoding..." << endl;
70
71    while(alive) {
72        auto out1 = outQ1->get<dai::ImgFrame>();
73        videoFile1.write((char*)out1->getData().data(), out1->getData().size());
74        auto out2 = outQ2->get<dai::ImgFrame>();
75        videoFile2.write((char*)out2->getData().data(), out2->getData().size());
76        auto out3 = outQ3->get<dai::ImgFrame>();
77        videoFile3.write((char*)out3->getData().data(), out3->getData().size());
78    }
79
80    cout << "To view the encoded data, convert the stream file (.h264/.h265) into a video file (.mp4), using a command below:" << endl;
81    cout << "ffmpeg -framerate 25 -i mono1.h264 -c copy mono1.mp4" << endl;
82    cout << "ffmpeg -framerate 25 -i mono2.h264 -c copy mono2.mp4" << endl;
83    cout << "ffmpeg -framerate 25 -i color.h265 -c copy color.mp4" << endl;
84
85    return 0;
86}

Pipeline

Need assistance?

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