# 视频回放

本示例演示如何回放先前录制的视频，并展示如何使用 [ReplayVideo](https://docs.luxonis.com/software-v3/depthai/depthai-components/host_nodes/replay_video.md)
节点将帧重新输入到 DepthAI 流水线中。

这个示例需要DepthAI v3 API，参见[安装说明](https://docs.luxonis.com/software-v3/depthai.md)。

## 源代码

#### Python

```python
import depthai as dai
import argparse
import time
from pathlib import Path
import cv2

parser = argparse.ArgumentParser()
parser.add_argument("-v", "--inputVideo", default="test_video.mp4", help="Input video name")
parser.add_argument("-m", "--inputMetadata", default="test_video.mcap", help="Input metadata name")

args = parser.parse_args()

# Check if the input video file exists
if not Path(args.inputVideo).exists():
    print("First record a video using the record_video.py script")
    raise FileNotFoundError(f'Input video file not found: {args.inputVideo}')

with dai.Pipeline() as pipeline:
    replay = pipeline.create(dai.node.ReplayVideo)
    replay.setReplayVideoFile(Path(args.inputVideo))
    if Path(args.inputMetadata).exists():
        replay.setReplayMetadataFile(Path(args.inputMetadata))
    replay.setOutFrameType(dai.ImgFrame.Type.NV12)
    replay.setLoop(False)

    videoOut = replay.out.createOutputQueue()

    pipeline.start()
    while pipeline.isRunning() and replay.isRunning():
        try:
            outFrame : dai.ImgFrame = videoOut.get()
        except dai.MessageQueue.QueueException:
            # Replay stopped the pipeline
            break
        outFrameCv = outFrame.getCvFrame()
        cv2.imshow("video", outFrameCv)
        if cv2.waitKey(1) == ord('q'):
            print("Stopping pipeline")
            pipeline.stop()
            break
```

#### C++

```cpp
#include "depthai/depthai.hpp"
#include "depthai/pipeline/node/host/Display.hpp"
#include "depthai/pipeline/node/host/Replay.hpp"

int main(int argc, char** argv) {
    std::string vidName = "test_video.avi";
    if(argc > 1) vidName = argv[1];
    dai::Pipeline pipeline(false);

    auto replay = pipeline.create<dai::node::ReplayVideo>();
    auto display = pipeline.create<dai::node::Display>();

    replay->setReplayVideoFile(vidName);
    replay->setFps(30);

    replay->out.link(display->input);

    pipeline.run();  // Let the display node stop the pipeline
}
```

### 需要帮助？

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