# Video Replay

This example demonstrates how to replay a previously recorded video and showcases how to use the
[ReplayVideo](https://docs.luxonis.com/software-v3/depthai/depthai-components/host_nodes/replay_video.md) node for feeding frames
back into a DepthAI pipeline.

This example requires the DepthAI v3 API, see [installation instructions](https://docs.luxonis.com/software-v3/depthai.md).

## Source code

#### 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
}
```

### Need assistance?

Head over to [Discussion Forum](https://discuss.luxonis.com/) for technical support or any other questions you might have.
