DepthAI v2 has been superseded by DepthAI v3. You are viewing legacy documentation.

本页目录

  • 如何放置
  • 输入与输出
  • 使用方法
  • 限制
  • 功能示例
  • 参考

VideoEncoder

VideoEncoder 节点用于将 ImgFrame 编码为 H264、H265 或 MJPEG 流。输入仅支持 NV12 或 GRAY8(会转换为 NV12)格式。所有编解码器都是有损的(无损 MJPEG 除外),更多信息请参阅 编码质量文档来自设备的编码比特流(MJPEG、H264 或 H265)也可以直接保存到 .mp4 容器中,无需主机计算开销。更多信息请参见 此示例VideoEncoder 需要 NV12 或 GRAY8 输入格式。因此其他输出,例如 ColorCamera 的 isp 输出(需要 >4K 分辨率)为 YUV420,在输入 VideoEncoder 前需要转换为 NV12。这里有一个 12MP H265 视频编码 @ 20FPS 的示例Matroska除了 ffmpeg.mp4 视频容器(受专利限制)外,您还可以使用 mkvmerge(GUI 使用请参见 MKVToolNix)和 .mkv 视频容器,将编码流混流为所有主流视频播放器(如 VLC)支持的视频文件。
Command Line
1mkvmerge -o vid.mkv video.h265

如何放置

Python

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

C++

C++
1dai::Pipeline pipeline;
2auto encoder = pipeline.create<dai::node::VideoEncoder>();

输入与输出

使用方法

Python

Python
1pipeline = dai.Pipeline()
2
3# 预先创建 ColorCamera
4# 为 ColorCamera 视频输出设置 H265 编码
5videoEncoder = pipeline.create(dai.node.VideoEncoder)
6videoEncoder.setDefaultProfilePreset(cam.getFps(), dai.VideoEncoderProperties.Profile.H265_MAIN)
7videoEncoder.setBitrateKbps(500) # 0.5 Mbps
8
9# 为静态图像创建 MJPEG 编码
10stillEncoder = pipeline.create(dai.node.VideoEncoder)
11stillEncoder.setDefaultProfilePreset(1, dai.VideoEncoderProperties.Profile.MJPEG)
12# stillEncoder.setLossless(True) # 仅 MJPEG 支持无损
13stillEncoder.setQuality(90) # 0-100,100 为最佳质量(但不是无损)
14
15cam.still.link(stillEncoder.input)
16cam.video.link(videoEncoder.input)

C++

C++
1dai::Pipeline pipeline;
2
3// 预先创建 ColorCamera
4// 为 ColorCamera 视频输出设置 H265 编码
5auto videoEncoder = pipeline.create<dai::node::VideoEncoder>();
6videoEncoder->setDefaultProfilePreset(cam->getFps(), dai::VideoEncoderProperties::Profile::H265_MAIN);
7videoEncoder->setBitrateKbps(500); // 0.5 Mbps
8
9// 为静态图像创建 MJPEG 编码
10stillEncoder = pipeline.create(dai.node.VideoEncoder);
11stillEncoder->setDefaultProfilePreset(1, dai::VideoEncoderProperties::Profile::MJPEG);
12// stillEncoder->setLossless(true); // 仅 MJPEG 支持无损
13stillEncoder->setQuality(90); // 0-100,100 为最佳质量(但不是无损)
14
15cam->still.link(stillEncoder->input);
16cam->video.link(videoEncoder->input);

限制

对于 H.264 / H.265 编码,有以下限制:
  • 2.48 亿像素/秒(4K@30 / 12MP@20)的编码器限制。分辨率和帧率可以分配到多个流中,但所有像素/秒的总和需低于 2.48 亿。
  • 由于硬件限制,视频编码仅能在宽度值为 32 的倍数的帧上进行。
  • 每帧最大宽度为 4096 像素。
MJPEG 编码器 支持 16384x8192 分辨率,速度为 450 MPix/秒。根据我们的测试,我们能够以 30FPS 编码 4K,并以 55FPS 编码 2 路 800P。

功能示例

参考

class

depthai.node.VideoEncoder(depthai.Node)

method
method
method
method
method
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
method
method
method
method
method
setBitrate(self, bitrate: typing.SupportsInt)
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: typing.SupportsInt)
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: typing.SupportsFloat)
Sets expected frame rate  Parameter ``frameRate``:     Frame rate in frames per second
method
setKeyframeFrequency(self, freq: typing.SupportsInt)
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: typing.SupportsInt)
Specifies maximum output encoded frame size
method
setNumBFrames(self, numBFrames: typing.SupportsInt)
Set number of B frames to be inserted. Applicable only to H264 and H265 profiles
method
setNumFramesPool(self, frames: typing.SupportsInt)
Set number of frames in pool  Parameter ``frames``:     Number of pool frames
method
method
setQuality(self, quality: typing.SupportsInt)
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.

需要帮助?

请前往 OAKChina 官网 获取技术支持或解答您的任何疑问。