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.VideoEncoder requires either NV12 or GRAY8 input format for frames. So other outputs, so eg.isp
output (required for >4K resolution) from ColorCamera, which is YUV420, needs to be converted to NV12 before feeding them to the VideoEncoder. Here's an example of 12MP H265 video encoding @ 20FPS.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
Note
Output message types
bitstream
and out
are mutually exclusive.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 / 12MP@20) 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.
Shared resources
Note the processing resources of the encoder are shared between H.26x and JPEG.
Examples of functionality
Reference
class
depthai.node.VideoEncoder(depthai.Node)
method
getBitrate(self) -> int: int
Get bitrate in bps
method
getBitrateKbps(self) -> int: int
Get bitrate in kbps
method
getFrameRate(self) -> float: float
Get frame rate
method
getHeight(self) -> int: int
Get input height
method
getKeyframeFrequency(self) -> int: int
Get keyframe frequency
method
getLossless(self) -> bool: bool
Get lossless mode. Applies only when using [M]JPEG profile.
method
method
getNumBFrames(self) -> int: int
Get number of B frames
method
getNumFramesPool(self) -> int: int
Get number of frames in pool Returns: Number of pool frames
method
getProfile(self) -> depthai.VideoEncoderProperties.Profile: depthai.VideoEncoderProperties.Profile
Get profile
method
getQuality(self) -> int: int
Get quality
method
getRateControlMode(self) -> depthai.VideoEncoderProperties.RateControlMode: depthai.VideoEncoderProperties.RateControlMode
Get rate control mode
method
getSize(self) -> tuple[int, int]: tuple[int, int]
Get input size
method
getWidth(self) -> int: int
Get input width
method
setBitrate(self, bitrate: int)
Set output bitrate in bps, for CBR rate control mode. 0 for auto (based on frame size and FPS). Applicable only to H264 and H265 profiles
method
setBitrateKbps(self, bitrateKbps: int)
Set output bitrate in kbps, for CBR rate control mode. 0 for auto (based on frame size and FPS). Applicable only to H264 and H265 profiles
method
method
setFrameRate(self, frameRate: float)
Sets expected frame rate Parameter ``frameRate``: Frame rate in frames per second
method
setKeyframeFrequency(self, freq: int)
Set keyframe frequency. Every Nth frame a keyframe is inserted. Applicable only to H264 and H265 profiles Examples: - 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
method
setLossless(self, arg0: bool)
Set lossless mode. Applies only to [M]JPEG profile Parameter ``lossless``: True to enable lossless jpeg encoding, false otherwise
method
setMaxOutputFrameSize(self, maxFrameSize: int)
Specifies maximum output encoded frame size
method
setNumBFrames(self, numBFrames: int)
Set number of B frames to be inserted. Applicable only to H264 and H265 profiles
method
setNumFramesPool(self, frames: int)
Set number of frames in pool Parameter ``frames``: Number of pool frames
method
method
setQuality(self, quality: int)
Set quality for [M]JPEG profile Parameter ``quality``: Value between 0-100%. Approximates quality
method
setRateControlMode(self, mode: depthai.VideoEncoderProperties.RateControlMode)
Set rate control mode Applicable only to H264 and H265 profiles
property
bitstream
Outputs ImgFrame message that carries BITSTREAM encoded (MJPEG, H264 or H265) frame data. Mutually exclusive with out.
property
input
Input for NV12 ImgFrame to be encoded Default queue is blocking with size set by 'setNumFramesPool' (4).
property
out
Outputs EncodedFrame message that carries encoded (MJPEG, H264 or H265) frame data. Mutually exclusive with bitstream.
Need assistance?
Head over to Discussion Forum for technical support or any other questions you might have.