ON THIS PAGE

  • VideoEncoder
  • How to place it
  • Inputs and Outputs
  • Usage
  • Limitations
  • Examples of functionality
  • Reference

VideoEncoder

VideoEncoder node is used to encode ImgFrame into either H264, H265, or MJPEG streams. Only NV12 or GRAY8 (which gets converted to NV12) format is supported as an input. All codecs are lossy (except lossless MJPEG), for more information please see encoding quality docs.Encoded bitstream (either MJPEG, H264, or H265) from the device can also be saved directly into .mp4 container with no computational overhead on the host computer. See demo here. for more information.MatroskaBesides ffmpeg and .mp4 video container (which is patent encumbered), you could also use the mkvmerge (see MKVToolNix for GUI usage) and .mkv video container to mux encoded stream into video file that is supported by all major video players (eg. VLC).
Command Line
1mkvmerge -o vid.mkv video.h265

How to place it

Python
C++

Python

Python
1pipeline = dai.Pipeline()
2encoder = pipeline.create(dai.node.VideoEncoder)

Inputs and Outputs

Usage

Python
C++

Python

Python
1pipeline = dai.Pipeline()
2
3# Create ColorCamera beforehand
4# Set H265 encoding for the ColorCamera video output
5videoEncoder = pipeline.create(dai.node.VideoEncoder)
6videoEncoder.setDefaultProfilePreset(cam.getFps(), dai.VideoEncoderProperties.Profile.H265_MAIN)
7videoEncoder.setBitrateKbps(500) # 0.5 Mbps
8
9# Create MJPEG encoding for still images
10stillEncoder = pipeline.create(dai.node.VideoEncoder)
11stillEncoder.setDefaultProfilePreset(1, dai.VideoEncoderProperties.Profile.MJPEG)
12# stillEncoder.setLossless(True) # Lossless only for MJPEG
13stillEncoder.setQuality(90) # 0-100, 100 being the best quality (not lossless though)
14
15cam.still.link(stillEncoder.input)
16cam.video.link(videoEncoder.input)

Limitations

For H.264 / H.265 encoding, we have the following limits:
  • 248 million pixels/second (4K@30) limit for the encoder. The resolution and frame rate can be divided into multiple streams - but the sum of all the pixels/second needs to be below 248 million.
  • Due to a HW constraint, video encoding can be done only on frames whose width values are multiples of 32.
  • 4096 pixel max width for a frame.
The MJPEG encoder is capable of 16384x8192 resolution at 450 MPix/sec. From our testing, we were able to encode 4K at 30FPS and 2x 800P at 55FPS.Note the processing resources of the encoder are shared between H.26x and JPEG.

Examples of functionality

Reference

class

dai::node::VideoEncoder

#include VideoEncoder.hpp
variable
Input input
Input for NV12 ImgFrame to be encoded
variable
Output bitstream
Outputs ImgFrame message that carries BITSTREAM encoded (MJPEG, H264 or H265) frame data. Mutually exclusive with out.
variable
Output out
Outputs EncodedFrame message that carries encoded (MJPEG, H264 or H265) frame data. Mutually exclusive with bitstream.
function
std::shared_ptr< VideoEncoder > build(Node::Output & input)
function
void setDefaultProfilePreset(float fps, Properties::Profile profile)
Sets a default preset based on specified frame rate and profile
Parameters
  • fps: Frame rate in frames per second
  • profile: Encoding profile
function
void setNumFramesPool(int frames)
Set number of frames in pool
Parameters
  • frames: Number of pool frames
function
int getNumFramesPool()
Get number of frames in pool
Returns
Number of pool frames
function
void setRateControlMode(Properties::RateControlMode mode)
function
void setProfile(Properties::Profile profile)
function
void setBitrate(int bitrate)
function
void setBitrateKbps(int bitrateKbps)
function
void setKeyframeFrequency(int freq)
Set keyframe frequency. Every Nth frame a keyframe is inserted.Applicable only to H264 and H265 profilesExamples:
  • 30 FPS video, keyframe frequency: 30. Every 1s a keyframe will be inserted
  • 60 FPS video, keyframe frequency: 180. Every 3s a keyframe will be inserted
function
void setNumBFrames(int numBFrames)
function
void setQuality(int quality)
Set quality
Parameters
  • quality: Value between 0-100%. Approximates quality
function
void setLossless(bool lossless)
Set lossless mode. Applies only to [M]JPEG profile
Parameters
  • lossless: True to enable lossless jpeg encoding, false otherwise
function
void setFrameRate(float frameRate)
Sets expected frame rate
Parameters
  • frameRate: Frame rate in frames per second
function
void setMaxOutputFrameSize(int maxFrameSize)
Specifies maximum output encoded frame size
function
Properties::RateControlMode getRateControlMode()
function
Properties::Profile getProfile()
function
int getBitrate()
function
int getBitrateKbps()
function
int getKeyframeFrequency()
function
int getNumBFrames()
function
int getQuality()
function
float getFrameRate()
function
bool getLossless()
function
int getMaxOutputFrameSize()
inline function
DeviceNodeCRTP()
inline function
DeviceNodeCRTP(const std::shared_ptr< Device > & device)
inline function
DeviceNodeCRTP(std::unique_ptr< Properties > props)
inline function
DeviceNodeCRTP(std::unique_ptr< Properties > props, bool confMode)
inline function
DeviceNodeCRTP(const std::shared_ptr< Device > & device, std::unique_ptr< Properties > props, bool confMode)

Need assistance?

Head over to Discussion Forum for technical support or any other questions you might have.