File size: 1,260 Bytes
c6abe34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import os

def read_video(video_path):
    cap = cv2.VideoCapture(video_path)
    fps = cap.get(cv2.CAP_PROP_FPS)
    frames = []
    while True:
        ret, frame = cap.read()
        # frame = cv2.flip(frame,0)
        if not ret:
            break
        frames.append(frame)
    return frames, fps

import cv2
import os
import sys

def write_video(frames, output_path, fps=30):
    # Ensure directory exists
    out_dir = os.path.dirname(output_path)
    if out_dir and not os.path.exists(out_dir):
        os.makedirs(out_dir, exist_ok=True)

    # Setup video writer
    height, width = frames[0].shape[:2]
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))

    total = len(frames)

    def progress_bar(i, total):
        pct = (i / total) * 100
        bar_len = 40
        filled = int((i / total) * bar_len)
        bar = "█" * filled + "-" * (bar_len - filled)
        sys.stdout.write(f"\rWriting video: [{bar}] {pct:6.2f}% ({i}/{total})")
        sys.stdout.flush()

    # Write frames with progress bar
    for i, frame in enumerate(frames, start=1):
        out.write(frame)
        progress_bar(i, total)

    out.release()
    print("\nDone!")  # move to new line