Repository navigation
Expand file tree
/
Copy pathindex.py
More file actions
81 lines (59 loc) · 2.07 KB
/
Copy pathindex.py
File metadata and controls
81 lines (59 loc) · 2.07 KB
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
81
import cv2
import os
import time
import numpy as np
def convert_frame_to_ascii(frame, width=80):
"""
Convert a frame to ASCII art using a character set based on brightness
"""
ascii_chars = " .:-=+*#%@"
height = int(frame.shape[0] * width / frame.shape[1] / 2)
if height == 0:
height = 1
resized_frame = cv2.resize(frame, (width, height))
if len(resized_frame.shape) > 2:
gray_frame = cv2.cvtColor(resized_frame, cv2.COLOR_BGR2GRAY)
else:
gray_frame = resized_frame
normalized = gray_frame / 255.0
ascii_frame = ""
for row in normalized:
for pixel in row:
index = int(pixel * (len(ascii_chars) - 1))
ascii_frame += ascii_chars[index]
ascii_frame += "\n"
return ascii_frame
def play_video_in_terminal(video_path, width=80, fps=30):
"""
Play Ser Harwin Strong a video in the terminal using ASCII characters
"""
if not os.path.exists(video_path):
print(f"Error: Video file '{video_path}' not found.")
return
cap = cv2.VideoCapture(video_path)
video_fps = cap.get(cv2.CAP_PROP_FPS)
frame_delay = 1.0 / video_fps if video_fps > 0 else 1.0 / fps
try:
while True:
ret, frame = cap.read()
if not ret:
break
ascii_art = convert_frame_to_ascii(frame, width)
os.system('cls' if os.name == 'nt' else 'clear')
print(ascii_art)
time.sleep(frame_delay)
except KeyboardInterrupt:
print("\nVideo playback interrupted.")
finally:
cap.release()
if __name__ == "__main__":
video_path = input("Enter the path to the video file: ").strip()
try:
width = int(input("Enter terminal width (default 80): ") or "80")
except ValueError:
width = 80
try:
fps = int(input("Enter FPS (default: use video FPS): ") or "0")
except ValueError:
fps = 0
play_video_in_terminal(video_path, width, fps)