# VideoEncoder

VideoEncoder节点用于将[ImgFrame](https://docs.luxonis.com/software-v3/depthai/depthai-components/messages/img_frame.md)编码为H264、H265或MJPEG流。只支持NV12或GRAY8（会转换为NV12）格式作为输入。所有编解码器都是有损的（除无损MJPEG外），更多信息请参阅[编码质量文档](https://github.com/luxonis/oak-examples/tree/master/gen2-record-replay/encoding_quality)。

来自设备的编码比特流（MJPEG、H264或H265）也可以直接保存到.mp4容器中，无需主机计算开销。更多信息请参阅[此处的演示](https://github.com/luxonis/oak-examples/tree/master/gen2-container-encoding)。

Matroska

除了受专利保护的ffmpeg和.mp4视频容器外，您还可以使用mkvmerge（GUI用法请参阅[MKVToolNix](https://mkvtoolnix.download/doc/mkvmerge.html)）和.mkv视频容器，将编码流混合到所有主流视频播放器（如[VLC](https://www.videolan.org/vlc/)）都支持的视频文件中。

```bash
mkvmerge -o vid.mkv video.h265
```

## 如何放置

#### Python

```python
pipeline = dai.Pipeline()
encoder = pipeline.create(dai.node.VideoEncoder)
```

#### C++

```cpp
dai::Pipeline pipeline;
auto encoder = pipeline.create<dai::node::VideoEncoder>();
```

## 输入与输出

> **注意**
> 输出消息类型
> `bitstream`
> 和
> `out`
> 是互斥的。

## 用法

#### Python

```python
pipeline = dai.Pipeline()

# 预先创建ColorCamera
# 为ColorCamera视频输出设置H265编码
videoEncoder = pipeline.create(dai.node.VideoEncoder)
videoEncoder.setDefaultProfilePreset(cam.getFps(), dai.VideoEncoderProperties.Profile.H265_MAIN)
videoEncoder.setBitrateKbps(500) # 0.5 Mbps

# 创建用于静态图像的MJPEG编码
stillEncoder = pipeline.create(dai.node.VideoEncoder)
stillEncoder.setDefaultProfilePreset(1, dai.VideoEncoderProperties.Profile.MJPEG)
# stillEncoder.setLossless(True) # 仅MJPEG支持无损
stillEncoder.setQuality(90) # 0-100，100为最佳质量（非无损）

cam.still.link(stillEncoder.input)
cam.video.link(videoEncoder.input)
```

#### C++

```cpp
dai::Pipeline pipeline;

// 预先创建ColorCamera
// 为ColorCamera视频输出设置H265编码
auto videoEncoder = pipeline.create<dai::node::VideoEncoder>();
videoEncoder->setDefaultProfilePreset(cam->getFps(), dai::VideoEncoderProperties::Profile::H265_MAIN);
videoEncoder->setBitrateKbps(500); // 0.5 Mbps

// 创建用于静态图像的MJPEG编码
stillEncoder = pipeline.create(dai.node.VideoEncoder);
stillEncoder->setDefaultProfilePreset(1, dai::VideoEncoderProperties::Profile::MJPEG);
// stillEncoder->setLossless(true); // 仅MJPEG支持无损
stillEncoder->setQuality(90); // 0-100，100为最佳质量（非无损）

cam->still.link(stillEncoder->input);
cam->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之间共享。

## 功能示例

 * [视频编码](https://docs.luxonis.com/software-v3/depthai/examples/video_encoder/video_encode.md) -
   将ColorCamera的视频流编码为H264/H265/MJPEG
 * [RGB编码与YOLO](https://docs.luxonis.com/software-v3/depthai/examples/detection_network/detection_network.md) -
   运行YOLOv6n目标检测模型并在视频流上显示结果（边界框），同时将视频流编码为H264。

## 参考

### dai::node::VideoEncoder

Kind: class

VideoEncoder node. Encodes frames into MJPEG, H264 or H265.

#### Input input

Kind: variable

Input for NV12 ImgFrame to be encoded

#### Output bitstream

Kind: variable

Outputs ImgFrame message that carries BITSTREAM encoded (MJPEG, H264 or H265) frame data. Mutually exclusive with out.

#### Output out

Kind: variable

Outputs EncodedFrame message that carries encoded (MJPEG, H264 or H265) frame data. Mutually exclusive with bitstream.

#### std::shared_ptr< VideoEncoder > build(Node::Output & input)

Kind: function

#### void setDefaultProfilePreset(float fps, Properties::Profile profile)

Kind: function

Sets a default preset based on specified frame rate and profile parameters: fps: Frame rate in frames per second; profile:
Encoding profile

#### void setNumFramesPool(int frames)

Kind: function

Set number of frames in pool parameters: frames: Number of pool frames

#### int getNumFramesPool()

Kind: function

Get number of frames in pool return: Number of pool frames

#### void setRateControlMode(Properties::RateControlMode mode)

Kind: function

Set rate control mode.

#### void setProfile(Properties::Profile profile)

Kind: function

Set encoding profile.

#### void setBitrate(int bitrate)

Kind: function

Set output bitrate in bps, for CBR rate control mode. 0 for auto (based on frame size and FPS)

#### void setBitrateKbps(int bitrateKbps)

Kind: function

Set output bitrate in kbps, for CBR rate control mode. 0 for auto (based on frame size and FPS)

#### void setKeyframeFrequency(int freq)

Kind: function

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

#### void setNumBFrames(int numBFrames)

Kind: function

Set number of B frames to be inserted.

#### void setQuality(int quality)

Kind: function

Set quality parameters: quality: Value between 0-100%. Approximates quality

#### void setLossless(bool lossless)

Kind: function

Set lossless mode. Applies only to [M]JPEG profile parameters: lossless: True to enable lossless jpeg encoding, false otherwise

#### void setFrameRate(float frameRate)

Kind: function

Sets expected frame rate parameters: frameRate: Frame rate in frames per second

#### void setMaxOutputFrameSize(int maxFrameSize)

Kind: function

Specifies maximum output encoded frame size

#### Properties::RateControlMode getRateControlMode()

Kind: function

Get rate control mode.

#### Properties::Profile getProfile()

Kind: function

Get profile.

#### int getBitrate()

Kind: function

Get bitrate in bps.

#### int getBitrateKbps()

Kind: function

Get bitrate in kbps.

#### int getKeyframeFrequency()

Kind: function

Get keyframe frequency.

#### int getNumBFrames()

Kind: function

Get number of B frames.

#### int getQuality()

Kind: function

Get quality.

#### float getFrameRate()

Kind: function

Get frame rate.

#### bool getLossless()

Kind: function

Get lossless mode. Applies only when using [M]JPEG profile.

#### int getMaxOutputFrameSize()

Kind: function

#### DeviceNodeCRTP()

Kind: function

#### DeviceNodeCRTP(const std::shared_ptr< Device > & device)

Kind: function

#### DeviceNodeCRTP(std::unique_ptr< Properties > props)

Kind: function

#### DeviceNodeCRTP(std::unique_ptr< Properties > props, bool confMode)

Kind: function

#### DeviceNodeCRTP(const std::shared_ptr< Device > & device, std::unique_ptr< Properties > props, bool confMode)

Kind: function

### 需要帮助？

请前往 [OAKChina 官网](https://www.oakchina.cn/) 获取技术支持或解答您的任何疑问。
