本页目录

  • 演示
  • 源代码
  • 管道

编码最大限制

此示例演示如何设置编码器节点,同时编码RGB摄像头和两个灰度摄像头(DepthAI/OAK-D),并将所有编码器参数设置为最高质量和最高帧率。RGB设置为4K(3840x2160),灰度摄像头各设置为1280x720,帧率均为25FPS。每个编码视频流通过XLINK传输并保存到对应文件中。按下Ctrl+C将停止录制,然后使用ffmpeg将其转换为mp4格式以便播放。注意,需要安装并运行ffmpeg才能成功转换为mp4。请注意,此示例会将编码视频保存到主机存储中。如果让其持续运行,可能会耗尽主机的存储空间。该示例是RGB编码RGB & 单色编码的变体。

类似示例:

演示

这个示例需要DepthAI v3 API,参见安装说明

源代码

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}

管道

需要帮助?

请前往 OAKChina 官网 获取技术支持或解答您的任何疑问。