File size: 2,148 Bytes
117e206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import numpy as np
import argparse
from pathlib import Path
import cv2
import click
import tqdm


@click.command()
@click.option("--infile", required=True, help="input file")
@click.option(
    "--outfile",
    default=None,
    help="Output file. If not given will be saved in the same directory as the input file.",
)
def split_stereo_video(infile, outfile):
    """
    Script to split stereo video into left/right videos.
    """

    infile = Path(infile)

    if outfile is not None:
        outfile = Path(outfile)
    else:
        outfile = infile.parent
    outfile.mkdir(exist_ok=True)

    if not infile.exists():
        print("input file not found")
        exit(0)

    cap = cv2.VideoCapture(str(infile))
    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

    if not cap.isOpened():
        print("Error opening video file")
        exit()

    enc = cv2.VideoWriter_fourcc(*"XVID")
    vid_format = ".avi"
    left_out_path = outfile / (infile.with_suffix("").name + "_left" + vid_format)
    right_out_path = outfile / (infile.with_suffix("").name + "_right" + vid_format)
    print(f"writing to {left_out_path}")

    left_out = None
    right_out = None
    index = 0

    with tqdm.tqdm(total=total_frames) as pbar:
        while cap.isOpened():
            ret, frame = cap.read()
            if index == 0:
                frame_width = frame.shape[1] // 2
                frame_height = frame.shape[0]
                left_out = cv2.VideoWriter(str(left_out_path), enc, 25, (frame_width, frame_height))
                right_out = cv2.VideoWriter(
                    str(right_out_path), enc, 25, (frame_width, frame_height)
                )
            if ret:
                left = frame[:, :frame_width, :]
                right = frame[:, frame_width:, :]
                left_out.write(left)
                right_out.write(right)
            else:
                break
            index += 1
            pbar.update(1)

    cap.release()
    left_out.release()
    right_out.release()
    cv2.destroyAllWindows()

    print("finish processing videos")


if __name__ == "__main__":
    split_stereo_video()