r/Python • u/epic000 • Aug 21 '19
I created a python script that can (technically) play videos inside of the terminal!
16
u/onlyanegg_ Aug 21 '19
What terminals have you tried running this on? I imagine some would perform better than others. Alacritty uses the GPU. Maybe it would work best.
11
u/epic000 Aug 21 '19
That’s a good point. The default Windows terminal is pretty slow at outputting text. I’ll have to try it later.
4
u/skamansam Aug 21 '19
Yes. Output is crazy slow. I suggest creating a string, the printing only that string once per frame. Should be much faster.
135
Aug 21 '19
Is this an elaborate way to rickroll all /r/Python ? :P
16
10
u/Fywq Aug 21 '19
Next attempt he will have the music playing on something like this to match the video: https://www.youtube.com/watch?v=G081hD0nwWE
6
2
96
u/UloPe Aug 21 '19
You reinvented libaa / libcaca
56
11
u/marcelofrau Aug 21 '19
I remember using mplayer to play videos on terminal with libaa, then I met libcaca.. man.. it got much better :D but this was from long time ago..
http://itswapshop.com/tutorial/linux-watch-movies-ascii-text-mode-mplayer
4
u/spyingwind Aug 21 '19
https://pypi.org/project/python-aalib/ and https://pypi.org/project/pycaca/ are available. :)
21
24
Aug 21 '19
Too bad it can't do sound lol
1
u/Low_Sector Aug 22 '19
oh man... it should try to reproduce sounds but as square/triangle waves to sound like an old NES game or something
10
9
u/willm Aug 21 '19
If you could do it without that os.system("cls") I would expect it to be a lot smoother.
6
11
u/bentheiii Aug 21 '19
It looks like you're calling print on each pixel individually, which slows down the program tremendously. If you were to write all the characters of a frame to a buffer, and print them all at once, it would run much more smoothly.
1
Aug 24 '19
Could also switch to raw mode (maybe not supported in cmd.exe) and only change the pixels that get updated on each frame.
5
1
u/Boring_Cholo Aug 21 '19
Could you share your source code? Would like to try it on other videos. Thanks
1
u/ahmedmokhles Aug 21 '19
Why is it so slow? I mean, I know its drawing each pixel as a character, and that python isn't the fastest language, but I expect something like this to be a lot faster.
2
u/epic000 Aug 21 '19
It’s because the Windows terminal is really slow at outputting text. If I used a faster terminal, it would be a lot better.
1
u/ahmedmokhles Aug 21 '19
Oh ok. Is the linux terminal faster? I wonder how fast it would run on linux, too bad I don't have any linux distros I can try it on. Also, would screen refresh rate and cpu speed also affect this?
1
u/concon2015 Aug 22 '19
C’mon r/python I’m sure we can get this up to 100+FPS
Great job on the script. Can’t wait to dig through the source code.
1
1
1
1
Aug 25 '19
Hi, maintainer of Chalk from Node.js here (terminal coloring from Node.js).
One of the improvements you could make is to do the coloring yourself. More importantly, build up the string yourself and print it all at once to prevent the flickering.
Instead of using a huge library, just output the following:
lines = []
for row in rows:
line = []
for p in row:
line.append(f"\x1b[48;2;{p.r};{p.g};{p.b}m ")
lines.append(''.join(line) + '\x1b[0m')
frame = '\n'.join(lines)
print(f"\x1b[1,1H\x1b[2J{frame}")Where rows is an array of arrays, and each inner array (p) is [r, g, b] pixel values from 0-255 (truncated, so rounded down).
Haven't tested, but should work.
EDIT: The new reddit editor is horrible.
1
u/epic000 Aug 26 '19
Hi! I originally did the colouring myself (which is what I used in the example here), but I changed it and used curses because it runs significantly better since I don’t have to redraw the frame every time. You are right about printing everything at once though.
The main thing preventing this from running faster is the Windows terminal. It is really slow at outputting text. Running the program on Linux I got much better results.
1
Aug 26 '19
Ah okay, that explains that. Curses will definitely be faster.
On Linux, that's because most terminals are hardware accelerated. The windows terminal is not AFAIK, unless you're using a third party emulator like Hyper.
3
2
2
Aug 21 '19
I’m starting to learn python and seeing posts like this get me so excited to dive into this language!
2
1
1
1
1
1
1
1
1
1
u/tMaize Aug 22 '19
Legit want the script. Would be fun to use as a loading screen while my main script runs. Cool stuff.
1
Aug 22 '19
Am I the only person tired of these ascii/terminal video submissions every day? Like seriously guys there is one of these literally every day.
1
1
1
1
0
0
u/jantari Aug 21 '19
good job
If you didn't clear the screen in between frames it would run faster and look a lot smoother
76
u/epic000 Aug 21 '19
I have the source code here: https://github.com/epic0007224/Terminal-Video-Player
It's pretty badly written, and I copy and pasted a bit lol.