Skip to content

Commit bacd545

Browse files
author
LTAuro84
committed
Add: Ascii terminal animation
1 parent 2b48175 commit bacd545

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

Ascii terminal animation/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
# Ascii terminal animation
3+
4+
## Description
5+
It takes any video in .mp4 format, converts it to ASCII, and plays it in the terminal. I included one little video that I tested out and it works nicely. However, I haven't tried with any other videos, so results might be different. I did this all on windows, but im sure it will work on macOS and Linux too.
6+
7+
## Installation
8+
I used OpenCV. Just run this command:
9+
10+
```bash
11+
pip install opencv-python
12+
```
13+
## Usage
14+
Make sure you're inside the folder, open up your terminal and just type:
15+
16+
```bash
17+
python main.py
18+
```
19+
## Technologies
20+
This was coded in Python using OpenCV
21+

Ascii terminal animation/main.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import cv2, os, time, sys, shutil
2+
from enum import Enum
3+
4+
class PlayMode(Enum):
5+
VIDEO = 1
6+
7+
# Important so the ascii can fit the screen
8+
def terminal_size():
9+
return shutil.get_terminal_size()
10+
11+
# This is basically what turns the video into ASCII
12+
def to_ascii(frame, chars, w, h):
13+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
14+
gray = cv2.resize(gray, (w, h))
15+
scale = (len(chars) - 1) / 255
16+
17+
return "\n".join("".join(chars[int(px * scale)] for px in row)
18+
for row in gray
19+
)
20+
21+
def play_in_terminal(video_path_or_cam_idx, black2white_chars, height, width, mode, max_fps):
22+
23+
cap = cv2.VideoCapture(video_path_or_cam_idx) # <- Opens the video
24+
delay = 1 / max_fps
25+
26+
sys.stdout.write("\033[?25l")
27+
sys.stdout.flush()
28+
29+
try:
30+
while cap.isOpened():
31+
ret, frame = cap.read()
32+
if not ret:
33+
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
34+
continue
35+
36+
ascii_frame = to_ascii(frame, black2white_chars, width, height - 1)
37+
38+
sys.stdout.write("\033[H")
39+
sys.stdout.write(ascii_frame)
40+
sys.stdout.flush()
41+
42+
time.sleep(delay)
43+
except KeyboardInterrupt:
44+
pass
45+
finally:
46+
cap.release()
47+
sys.stdout.write("\033[?25h\n")
48+
sys.stdout.flush()
49+
50+
# The main execution
51+
if __name__ == "__main__":
52+
h, w = terminal_size().lines, terminal_size().columns
53+
print(f"Terminal size (h, w): ({h}, {w})")
54+
time.sleep(1)
55+
56+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
57+
video_path = os.path.join(BASE_DIR, "video", "lain_dance.mp4") # Add any video you want. Just change lain_dance.mp4 to the video you got
58+
59+
60+
play_in_terminal(
61+
video_path_or_cam_idx = video_path,
62+
black2white_chars = [' ', '`', '.', '~', '+', '*', 'o', 'O', '0', '#', '@'],
63+
height = h,
64+
width = w,
65+
mode = PlayMode.VIDEO,
66+
max_fps = 60
67+
)
569 KB
Binary file not shown.

0 commit comments

Comments
 (0)