DepthAI v2 has been superseded by DepthAI v3. You are viewing legacy documentation.
此页面由 AI 自动翻译。查看英文原版
DepthAI 教程
DepthAI API 参考

本页目录

  • 演示
  • 设置
  • 源代码
  • 管道

RGB编码

本示例展示了如何配置 depthai 视频编码器以 h.265 格式对 RGB 摄像头输入进行编码,分辨率为 8MP/4K/2160p (3840x2160),帧率为 30FPS(编码器可能支持的最高分辨率,在较低分辨率下可以实现更高的帧率,例如 1440p 下的 60FPS),并通过 XLINK 将编码后的视频传输到主机,将其保存为视频文件。按 Ctrl+C 将停止录制,然后使用 ffmpeg 将其转换为 mp4,以便播放。请注意,ffmpeg 需要已安装并可运行才能成功转换为 mp4。请注意,此示例会将编码后的视频保存到您的主机存储中。因此,如果您让它一直运行,可能会填满您的主机存储空间。

类似示例:

演示

设置

请运行 安装脚本 以下载所有必需的依赖项。请注意,此脚本必须在 git 上下文中运行,因此您必须先下载 depthai-python 存储库,然后运行脚本
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
有关更多信息,请遵循 安装指南

源代码

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 output
9camRgb = pipeline.create(dai.node.ColorCamera)
10videoEnc = pipeline.create(dai.node.VideoEncoder)
11xout = pipeline.create(dai.node.XLinkOut)
12
13xout.setStreamName('h265')
14
15# Properties
16camRgb.setBoardSocket(dai.CameraBoardSocket.CAM_A)
17camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_4_K)
18videoEnc.setDefaultProfilePreset(30, dai.VideoEncoderProperties.Profile.H265_MAIN)
19
20# Linking
21camRgb.video.link(videoEnc.input)
22videoEnc.bitstream.link(xout.input)
23
24# Connect to device and start pipeline
25with dai.Device(pipeline) as device:
26
27    # Output queue will be used to get the encoded data from the output defined above
28    q = device.getOutputQueue(name="h265", maxSize=30, blocking=True)
29
30    # The .h265 file is a raw stream file (not playable yet)
31    with open('video.h265', 'wb') as videoFile:
32        print("Press Ctrl+C to stop encoding...")
33        try:
34            while True:
35                h265Packet = q.get()  # Blocking call, will wait until a new data has arrived
36                h265Packet.getData().tofile(videoFile)  # Appends the packet data to the opened file
37        except KeyboardInterrupt:
38            # Keyboard interrupt (Ctrl + C) detected
39            pass
40
41    print("To view the encoded data, convert the stream file (.h265) into a video file (.mp4) using a command below:")
42    print("ffmpeg -framerate 30 -i video.h265 -c copy video.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(int argc, char** argv) {
14    using namespace std;
15    std::signal(SIGINT, &sigintHandler);
16
17    // Create pipeline
18    dai::Pipeline pipeline;
19
20    // Define sources and outputs
21    auto camRgb = pipeline.create<dai::node::ColorCamera>();
22    auto videoEnc = pipeline.create<dai::node::VideoEncoder>();
23    auto xout = pipeline.create<dai::node::XLinkOut>();
24
25    xout->setStreamName("h265");
26
27    // Properties
28    camRgb->setBoardSocket(dai::CameraBoardSocket::CAM_A);
29    camRgb->setResolution(dai::ColorCameraProperties::SensorResolution::THE_4_K);
30    videoEnc->setDefaultProfilePreset(30, dai::VideoEncoderProperties::Profile::H265_MAIN);
31
32    // Linking
33    camRgb->video.link(videoEnc->input);
34    videoEnc->bitstream.link(xout->input);
35
36    // Connect to device and start pipeline
37    dai::Device device(pipeline);
38
39    // Output queue will be used to get the encoded data from the output defined above
40    auto q = device.getOutputQueue("h265", 30, true);
41
42    // The .h265 file is a raw stream file (not playable yet)
43    auto videoFile = std::ofstream("video.h265", std::ios::binary);
44    cout << "Press Ctrl+C to stop encoding..." << endl;
45
46    while(alive) {
47        auto h265Packet = q->get<dai::ImgFrame>();
48        videoFile.write((char*)(h265Packet->getData().data()), h265Packet->getData().size());
49    }
50
51    cout << "To view the encoded data, convert the stream file (.h265) into a video file (.mp4) using a command below:" << endl;
52    cout << "ffmpeg -framerate 30 -i video.h265 -c copy video.mp4" << endl;
53
54    return 0;
55}

管道

需要帮助?

请前往 Discussion Forum 获取技术支持或提出您可能有的任何其他问题。