此页面由 AI 自动翻译。查看英文原版
DepthAI
  • DepthAI 组件
    • AprilTags
    • 基准测试
    • Camera
    • 校准
    • DetectionNetwork
    • Events
    • FeatureTracker
    • Gate
    • HostNodes
    • ImageAlign
    • ImageManip
    • IMU
    • 杂项
    • 模型动物园
    • NeuralDepth
    • NeuralNetwork
    • ObjectTracker
    • 点云
    • RecordReplay
    • RGBD
    • Script
    • SpatialDetectionNetwork
    • SpatialLocationCalculator
    • StereoDepth
    • Sync
    • VideoEncoder
    • Visualizer
    • Warp
    • RVC2-specific
  • 高级教程
  • API 参考
  • 工具
软件栈

本页目录

  • Pipeline
  • 源代码

视频录制

Supported on:RVC2RVC4
本示例演示了如何录制视频流,并展示了如何使用 RecordVideo 节点来捕获和保存帧。此示例需要 DepthAI v3 API,请参阅 安装说明

Pipeline

源代码

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}

需要帮助?

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