|
| 1 | +#include <atomic> |
| 2 | +#include <chrono> |
| 3 | +#include <csignal> |
| 4 | +#include <fstream> |
| 5 | +#include <iostream> |
| 6 | +#include <optional> |
| 7 | +#include <thread> |
| 8 | + |
| 9 | +#include "depthai/depthai.hpp" |
| 10 | +#include "depthai/pipeline/datatype/MessageGroup.hpp" |
| 11 | + |
| 12 | +constexpr std::pair<int, int> SIZE = {1280, 720}; |
| 13 | +constexpr int FPS = 480; |
| 14 | + |
| 15 | +std::atomic<bool> quitEvent(false); |
| 16 | + |
| 17 | +void signalHandler(int signum) { |
| 18 | + quitEvent = true; |
| 19 | +} |
| 20 | + |
| 21 | +class VideoSaver : public dai::node::CustomNode<VideoSaver> { |
| 22 | + public: |
| 23 | + VideoSaver() : fileHandle("video_hfr.encoded", std::ios::binary) { |
| 24 | + if(!fileHandle.is_open()) { |
| 25 | + throw std::runtime_error("Could not open video_hfr.encoded for writing"); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + ~VideoSaver() { |
| 30 | + if(fileHandle.is_open()) { |
| 31 | + fileHandle.close(); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + std::shared_ptr<dai::Buffer> processGroup(std::shared_ptr<dai::MessageGroup> message) override { |
| 36 | + if(!fileHandle.is_open()) return nullptr; |
| 37 | + |
| 38 | + auto frame = message->get<dai::EncodedFrame>("data"); |
| 39 | + unsigned char* frameData = frame->getData().data(); |
| 40 | + size_t frameSize = frame->getData().size(); |
| 41 | + fileHandle.write(reinterpret_cast<const char*>(frameData), frameSize); |
| 42 | + |
| 43 | + return nullptr; |
| 44 | + } |
| 45 | + |
| 46 | + private: |
| 47 | + std::ofstream fileHandle; |
| 48 | +}; |
| 49 | + |
| 50 | +int main() { |
| 51 | + signal(SIGTERM, signalHandler); |
| 52 | + signal(SIGINT, signalHandler); |
| 53 | + |
| 54 | + dai::Pipeline pipeline; |
| 55 | + |
| 56 | + auto platform = pipeline.getDefaultDevice()->getPlatform(); |
| 57 | + if(platform != dai::Platform::RVC4) { |
| 58 | + std::cerr << "This example is only supported on RVC4 devices\n" << std::flush; |
| 59 | + return -1; |
| 60 | + } |
| 61 | + |
| 62 | + auto camRgb = pipeline.create<dai::node::Camera>()->build(dai::CameraBoardSocket::CAM_A); |
| 63 | + auto* output = camRgb->requestOutput(SIZE, std::nullopt, dai::ImgResizeMode::CROP, static_cast<float>(FPS)); |
| 64 | + |
| 65 | + // ImageManip is added to workaround a limitation with VideoEncoder with native resolutions |
| 66 | + // This limitation will be lifted in the future |
| 67 | + auto imageManip = pipeline.create<dai::node::ImageManip>(); |
| 68 | + imageManip->initialConfig->setOutputSize(SIZE.first, SIZE.second + 10); |
| 69 | + imageManip->setMaxOutputFrameSize(static_cast<int>(SIZE.first * (SIZE.second + 10) * 1.6)); |
| 70 | + imageManip->inputImage.setMaxSize(12); |
| 71 | + output->link(imageManip->inputImage); |
| 72 | + auto encodedInput = imageManip->out; |
| 73 | + |
| 74 | + auto benchmarkIn = pipeline.create<dai::node::BenchmarkIn>(); |
| 75 | + benchmarkIn->setRunOnHost(true); |
| 76 | + |
| 77 | + auto encoded = pipeline.create<dai::node::VideoEncoder>(); |
| 78 | + encoded->setDefaultProfilePreset(static_cast<float>(FPS), dai::VideoEncoderProperties::Profile::H264_MAIN); |
| 79 | + encodedInput.link(encoded->input); |
| 80 | + encoded->out.link(benchmarkIn->input); |
| 81 | + |
| 82 | + auto saver = pipeline.create<VideoSaver>(); |
| 83 | + encoded->out.link(saver->inputs["data"]); |
| 84 | + |
| 85 | + pipeline.start(); |
| 86 | + |
| 87 | + std::cout << "Started to save video to video_hfr.encoded" << std::endl; |
| 88 | + std::cout << "Press Ctrl+C to stop" << std::endl; |
| 89 | + |
| 90 | + while(pipeline.isRunning() && !quitEvent) { |
| 91 | + std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 92 | + } |
| 93 | + |
| 94 | + pipeline.stop(); |
| 95 | + pipeline.wait(); |
| 96 | + |
| 97 | + std::cout << "To view the encoded data, convert the stream file (.encoded) into a video file (.mp4) using a command below:" << std::endl; |
| 98 | + std::cout << "ffmpeg -framerate " << FPS << " -i video_hfr.encoded -c copy video_hfr.mp4" << std::endl; |
| 99 | + |
| 100 | + std::cout << "If the FPS is not set correctly, you can ask ffmpeg to generate it with the command below" << std::endl; |
| 101 | + |
| 102 | + std::cout << "\nffmpeg -fflags +genpts -r " << FPS << " -i video_hfr.encoded \\\n -vsync cfr -fps_mode cfr \\\n -video_track_timescale " << FPS |
| 103 | + << "00 \\\n -c:v copy \\\n video_hfr.mp4\n"; |
| 104 | + |
| 105 | + return 0; |
| 106 | +} |
0 commit comments