rgb_encoding.cpp
Go to the documentation of this file.
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
8 static std::atomic<bool> alive{true};
9 static void sigintHandler(int signum) {
10  alive = false;
11 }
12 
13 int 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);
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 }
dai::node::XLinkOut
XLinkOut node. Sends messages over XLink.
Definition: XLinkOut.hpp:14
dai::Pipeline
Represents the pipeline, set of nodes and connections between them.
Definition: Pipeline.hpp:100
main
int main(int argc, char **argv)
Definition: rgb_encoding.cpp:13
dai::CameraBoardSocket::CAM_A
@ CAM_A
dai::node::ColorCamera
ColorCamera node. For use with color sensors.
Definition: ColorCamera.hpp:16
dai::ColorCameraProperties::SensorResolution::THE_4_K
@ THE_4_K
3840 × 2160
dai::Device::getOutputQueue
std::shared_ptr< DataOutputQueue > getOutputQueue(const std::string &name)
Definition: Device.cpp:86
depthai.hpp
dai::Pipeline::create
std::shared_ptr< N > create()
Definition: Pipeline.hpp:145
sigintHandler
static void sigintHandler(int signum)
Definition: rgb_encoding.cpp:9
dai::ImgFrame
Definition: ImgFrame.hpp:25
dai::node::ColorCamera::video
Output video
Definition: ColorCamera.hpp:62
dai::Device
Definition: Device.hpp:21
dai::VideoEncoderProperties::Profile::H265_MAIN
@ H265_MAIN
alive
static std::atomic< bool > alive
Definition: rgb_encoding.cpp:8
std
Definition: Node.hpp:366
dai::node::VideoEncoder
VideoEncoder node. Encodes frames into MJPEG, H264 or H265.
Definition: VideoEncoder.hpp:14
dai::Node::Output::link
void link(const Input &in)
Definition: Node.cpp:84
dai::node::VideoEncoder::input
Input input
Definition: VideoEncoder.hpp:25


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19