-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_terminal.py
54 lines (38 loc) · 1.57 KB
/
to_terminal.py
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
import time
import cv2
import to_ascii
def play(folder, video):
"""Prints a video to the console."""
# options
fps = video.get(cv2.CAP_PROP_FPS)
frames = video.get(cv2.CAP_PROP_FRAME_COUNT)
sec_time = frames / fps
ns_between_frames = 1 / fps * 10 ** 9
ns_init_sleep = 1_000_000 # time to initiate sleep
result = 0
frame = 0
dropped_frames = []
begin = time.time_ns()
while result is not None:
before = time.time_ns()
result = to_ascii.print_frame(str(folder / f"{frame}.png"))
# to ensure video plays at 30 fps we need to calculate the time it has taken to print and reduce this
# from wait so the next frame plays on time
elapsed_time = time.time_ns() - before
# initiating sleep causes some ns to pass. remove this from time_to_wait to reduce inaccuracy from
# ~2.5% (224 secs in execution) to ~-0.3% (218 secs in execution)
ns_to_wait = ns_between_frames - elapsed_time - ns_init_sleep
# if the ns to wait is negative, aka the reading took too long, add this frame to the "dropped'
if ns_to_wait < 0:
dropped_frames.append(frame)
# can't wait negative amount of time
ns_to_wait = 0
frame += 1
time.sleep(ns_to_wait * 10 ** -9)
end = time.time_ns()
# measure time it took to actually play video
deltasecs = (end - begin) / 10 ** 9
# stats
print(f"Took: {deltasecs} | "
f"Accuracy: {(deltasecs - sec_time) / sec_time * 100}% | "
f"Dropped frames: {dropped_frames}")