Skip to content

Commit 906abad

Browse files
authored
Merge pull request #74 from diegodorado/json-output-format
Add JSON output format as a simple array of frames
2 parents 8e1aa26 + d983bd6 commit 906abad

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

bin/video-to-ascii

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ PARSER.add_argument('-f', '--file', type=str, dest='file', help='input video fil
1111
PARSER.add_argument('--strategy', default='ascii-color', type=str, dest='strategy',
1212
choices=["ascii-color", "just-ascii", "filled-ascii"], help='choose an strategy to render the output', action='store')
1313
PARSER.add_argument('-o', '--output', type=str, dest='output', help='output file to export', action='store')
14+
PARSER.add_argument('--output-format', default='sh', type=str, dest='output_format',
15+
choices=["sh", "json"], help='choose an output format to render the output', action='store')
1416
PARSER.add_argument('--with-audio', dest='with_audio', help='play audio', action='store_true')
1517

1618
ARGS = PARSER.parse_args()
1719

18-
player.play(ARGS.file, strategy=ARGS.strategy, output=ARGS.output, play_audio=ARGS.with_audio)
20+
player.play(ARGS.file, strategy=ARGS.strategy, output=ARGS.output, output_format=ARGS.output_format, play_audio=ARGS.with_audio)

video_to_ascii/player.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import tempfile
44
from . import video_engine as ve
55

6-
def play(filename, strategy=None, output=None, play_audio=False):
6+
def play(filename, strategy=None, output=None, output_format=None, play_audio=False):
77
"""
88
Play or export a video from a file by default using ascii chars in terminal
99
"""
@@ -20,4 +20,4 @@ def play(filename, strategy=None, output=None, play_audio=False):
2020
engine.with_audio = True
2121
if strategy is not None:
2222
engine.set_strategy(strategy)
23-
engine.play(output)
23+
engine.play(output, output_format)

video_to_ascii/render_strategy/ascii_strategy.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def apply_pixel_to_ascii_strategy(self, pixel):
7070
def apply_end_line_modifier(self, msg):
7171
return msg
7272

73-
def render(self, cap, output=None, with_audio=False):
73+
def render(self, cap, output=None, output_format=None, with_audio=False):
7474
"""
7575
Iterate each video frame to print a set of ascii chars
7676
@@ -114,9 +114,11 @@ def render(self, cap, output=None, with_audio=False):
114114

115115
if output is not None:
116116
file = open(output, 'w+')
117-
file.write("#!/bin/bash \n")
118-
file.write("echo -en '\033[2J' \n")
119-
file.write("echo -en '\u001b[0;0H' \n")
117+
118+
if output_format == 'sh':
119+
file.write("#!/bin/bash \n")
120+
file.write("echo -en '\033[2J' \n")
121+
file.write("echo -en '\u001b[0;0H' \n")
120122

121123
time_delta = 1./fps
122124
counter=0
@@ -158,11 +160,27 @@ def render(self, cap, output=None, with_audio=False):
158160
print("\u001b[2A")
159161
else:
160162
print("\x1b[2A")
161-
resized_frame = self.resize_frame(frame)
162-
msg = self.convert_frame_pixels_to_ascii(resized_frame, new_line_chars=True)
163-
file.write("sleep 0.033 \n")
164-
file.write("echo -en '" + msg + "'" + "\n" )
165-
file.write("echo -en '\u001b[0;0H' \n")
163+
164+
if output_format == 'sh':
165+
resized_frame = self.resize_frame(frame)
166+
msg = self.convert_frame_pixels_to_ascii(resized_frame, new_line_chars=True)
167+
file.write("sleep 0.033 \n")
168+
file.write("echo -en '" + msg + "'" + "\n" )
169+
file.write("echo -en '\u001b[0;0H' \n")
170+
elif output_format == 'json':
171+
# scale each frame according to terminal dimensions
172+
resized_frame = self.resize_frame(frame, (cols, rows))
173+
msg = self.convert_frame_pixels_to_ascii(resized_frame, (cols, rows), new_line_chars=True)
174+
lines = msg.split("\n")
175+
# remove last line breaks (\n\r) which generate two extra unwanted array elements
176+
lines = lines[0:-2]
177+
# opening brackets
178+
file.write("[[\n" if counter == 0 else ",[\n")
179+
for i in range(len(lines)):
180+
file.write(f"\"{lines[i]}\"")
181+
# closing brackets
182+
file.write("]\n" if i == (len(lines) - 1) else ",\n")
183+
166184
counter += 1
167185
if with_audio:
168186
stream.close()
@@ -172,6 +190,10 @@ def render(self, cap, output=None, with_audio=False):
172190
else:
173191
os.system('cls') or None
174192

193+
# close the frame array
194+
if output is not None and output_format == 'json':
195+
file.write(f"]\n")
196+
175197
def build_progress(self, progress, total):
176198
"""Build a progress bar in the terminal"""
177199
progress_percent = int(progress / total * 100)

video_to_ascii/video_engine.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ def load_video_from_file(self, filename):
2828
cap = cv2.VideoCapture(filename)
2929
self.read_buffer = cap
3030

31-
def play(self, file=None):
31+
def play(self, output=None, output_format=None):
3232
"""
3333
Play the video captured using an specific render strategy
3434
"""
3535

3636
self.render_strategy.render(self.read_buffer,
37-
output=file,
37+
output=output,
38+
output_format=output_format,
3839
with_audio=self.with_audio)

0 commit comments

Comments
 (0)