# VideoEncoder

VideoEncoder 节点用于将 [ImgFrame](https://docs.luxonis.com/software/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)。

VideoEncoder 需要 NV12 或 GRAY8 输入格式。因此其他输出，例如 ColorCamera 的 isp 输出（需要 >4K 分辨率）为 YUV420，在输入 VideoEncoder 前需要转换为 NV12。这里有一个 [12MP H265
视频编码 @ 20FPS 的示例](https://gist.github.com/Erol444/a281bbc6cbb8e759e9442a57543edbcc#file-video_enc_12mp_h26x-py)。

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 / 12MP@20）的编码器限制。分辨率和帧率可以分配到多个流中，但所有像素/秒的总和需低于 2.48 亿。
 * 由于硬件限制，视频编码仅能在宽度值为 32 的倍数的帧上进行。
 * 每帧最大宽度为 4096 像素。

MJPEG 编码器 支持 16384x8192 分辨率，速度为 450 MPix/秒。根据我们的测试，我们能够以 30FPS 编码 4K，并以 55FPS 编码 2 路 800P。

> **共享资源**
> 请注意编码器的处理资源
> **在 H.26x 和 JPEG 之间共享**
> 。

## 功能示例

 * [RGB 编码](https://docs.luxonis.com/software/depthai/examples/rgb_encoding.md)
 * [编码最大限制](https://docs.luxonis.com/software/depthai/examples/encoding_max_limit.md)
 * [RGB 编码 & MobilenetSSD](https://docs.luxonis.com/software/depthai/examples/rgb_encoding_mobilenet.md)

## 参考

### depthai.node.VideoEncoder(depthai.Node)

Kind: Class

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

#### getBitrate(self) -> int: int

Kind: Method

Get bitrate in bps

#### getBitrateKbps(self) -> int: int

Kind: Method

Get bitrate in kbps

#### getFrameRate(self) -> float: float

Kind: Method

Get frame rate

#### getHeight(self) -> int: int

Kind: Method

Get input height

#### getKeyframeFrequency(self) -> int: int

Kind: Method

Get keyframe frequency

#### getLossless(self) -> bool: bool

Kind: Method

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

#### getMaxOutputFrameSize(self) -> int: int

Kind: Method

#### getNumBFrames(self) -> int: int

Kind: Method

Get number of B frames

#### getNumFramesPool(self) -> int: int

Kind: Method

Get number of frames in pool

Returns:
Number of pool frames

#### getProfile(self) -> depthai.VideoEncoderProperties.Profile: depthai.VideoEncoderProperties.Profile

Kind: Method

Get profile

#### getQuality(self) -> int: int

Kind: Method

Get quality

#### getRateControlMode(self) -> depthai.VideoEncoderProperties.RateControlMode: depthai.VideoEncoderProperties.RateControlMode

Kind: Method

Get rate control mode

#### getSize(self) -> tuple[int, int]: tuple[int, int]

Kind: Method

Get input size

#### getWidth(self) -> int: int

Kind: Method

Get input width

#### setBitrate(self, bitrate: typing.SupportsInt)

Kind: Method

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

#### setBitrateKbps(self, bitrateKbps: typing.SupportsInt)

Kind: Method

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

#### setDefaultProfilePreset()

Kind: Method

#### setFrameRate(self, frameRate: typing.SupportsFloat)

Kind: Method

Sets expected frame rate

Parameter ``frameRate``:
Frame rate in frames per second

#### setKeyframeFrequency(self, freq: typing.SupportsInt)

Kind: Method

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

#### setLossless(self, arg0: bool)

Kind: Method

Set lossless mode. Applies only to [M]JPEG profile

Parameter ``lossless``:
True to enable lossless jpeg encoding, false otherwise

#### setMaxOutputFrameSize(self, maxFrameSize: typing.SupportsInt)

Kind: Method

Specifies maximum output encoded frame size

#### setNumBFrames(self, numBFrames: typing.SupportsInt)

Kind: Method

Set number of B frames to be inserted. Applicable only to H264 and H265 profiles

#### setNumFramesPool(self, frames: typing.SupportsInt)

Kind: Method

Set number of frames in pool

Parameter ``frames``:
Number of pool frames

#### setProfile()

Kind: Method

#### setQuality(self, quality: typing.SupportsInt)

Kind: Method

Set quality for [M]JPEG profile

Parameter ``quality``:
Value between 0-100%. Approximates quality

#### setRateControlMode(self, mode: depthai.VideoEncoderProperties.RateControlMode)

Kind: Method

Set rate control mode Applicable only to H264 and H265 profiles

#### bitstream

Kind: Property

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

#### input

Kind: Property

Input for NV12 ImgFrame to be encoded Default queue is blocking with size set by
'setNumFramesPool' (4).

#### out

Kind: Property

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

### 需要帮助？

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