本页目录

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

VideoEncoder

Supported on:RVC2RVC4
VideoEncoder节点用于将ImgFrame编码为H264、H265或MJPEG流。只支持NV12或GRAY8(会转换为NV12)格式作为输入。所有编解码器都是有损的(除无损MJPEG外),更多信息请参阅编码质量文档来自设备的编码比特流(MJPEG、H264或H265)也可以直接保存到.mp4容器中,无需主机计算开销。更多信息请参阅此处的演示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)的限制。分辨率和帧率可分配给多个流——但所有流的总像素/秒需低于2.48亿。
  • 由于硬件约束,视频编码仅能作用于宽度值为32的倍数的帧。
  • 帧的最大宽度为4096像素。
MJPEG编码器可支持16384x8192分辨率,450百万像素/秒。根据我们的测试,能够以30FPS编码4K,并以55FPS编码2个800P流。请注意编码器的处理资源在H.26x和JPEG之间共享

功能示例

  • 视频编码 - 将ColorCamera的视频流编码为H264/H265/MJPEG
  • RGB编码与YOLO - 运行YOLOv6n目标检测模型并在视频流上显示结果(边界框),同时将视频流编码为H264。

参考

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)

需要帮助?

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