RGB Encoding
This example shows how to configure the depthai video encoder in h.265 format to encode the RGB camera input at 8MP/4K/2160p (3840x2160) at 30FPS (the maximum possible encoding resolultion possible for the encoder, higher frame-rates are possible at lower resolutions, like 1440p at 60FPS), and transfers the encoded video over XLINK to the host, saving it to disk as a video file.Pressing Ctrl+C will stop the recording and then convert it using ffmpeg into an mp4 to make it playable. Note that ffmpeg will need to be installed and runnable for the conversion to mp4 to succeed.Be careful, this example saves encoded video to your host storage. So if you leave it running, you could fill up your storage on your host.Similar samples:
- RGB & Mono Encoding
- Encoding Max Limit
- # RGB Encoding & MobilenetSSD
- # RGB Encoding & Mono & MobilenetSSD
- # RGB Encoding & Mono with MobilenetSSD & Depth
Demo
Setup
Please run the install script to download all required dependencies. Please note that this script must be ran from git context, so you have to download the depthai-python repository first and then run the scriptCommand Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
Source code
Python
C++
Python
PythonGitHub
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")
Pipeline
Need assistance?
Head over to Discussion Forum for technical support or any other questions you might have.