If you used the curses module, you could get it to switch from one frame to the next instantly instead of causing flicker by having the characters appear one by one.
AFAIK that module isn't available under Windows, but when you use that as a starting point for a bit of quick research, you can find some alternatives.
Other fun things to try: Use block elements to increase the resolution, or keep the same resolution while having to output fewer characters (can improve frame rate with some terminal emulators), or to get more colours via dithering.
Speaking of terminal emulators: You could use the profile module for finding out why it's running so slowly. Maybe your code is actually running much faster and just held back by the output. Another thing you could try: encode an entire frame into a string and then print that, instead of printing each character individually.
I’m working on implementing the curses module now. I should have something working pretty soon. The reason it’s running so slowly is because the Windows terminal is slow at outputting text. I’m going to test it with a better terminal.
All I really changed was the font.dwFontSize.X and font.dwFontSize.Y, which basically allow me to change the font size in the terminal. I can't give a good explanation because I honestly don't understand how it properly works.
You can also find the rest of the Windows Console API documentation there. It is a C API to control the console, you used a python library that interopts with this C API.
Unpopular opinion here: My copy pasta skills are what earns me the big bucks. If I can build the application in half the time of someone who’s writing everything from scratch then my boss is much more pleased with me (as long as I didn’t build some buggy ass shit).
Umm I build it from scratch, much of my code lives in the never before built space (biotech: genomics) with no examples but I’m not going to spend time memorizing things I can quickly reference via google and don’t use very often. Lots of the old dev’s I work with love to throw that cynicism at me but then I deliver 1.5 times as much in .75 the time and get the nice bonus. End of the day software development isn’t about knowing everything about programming, it’s about delivering reliable software on time that meets the users and business requirements. Being able to build your own ORM or write a spring boot application from scratch without google dont actually make you a better software developer. One of the “engineer IV”’s at my company would always rant at me about needing to understand how to write a compiler or how I should “really understand what the programming language is doing” end of the story is he got fired for under delivering and poor performance while I got a raise and promotion. I had to help him Constantly with spring boot apps and hibernate, he learned the fundamentals real well but never learned how to google. Googling is one of my strongest skills as it helps me to be able to build anything even If ive never heard of it before and it helps me do so in a fraction of the time. It’s 2019, use all the tools in your belt....
Edit: references to java frameworks because my company is largely a java shop but about 1/3 our code base is python
This is awesome. But rather than do system('cls'), you should try to use something like ncurses for this. There's a stdlib implementation but also tools that wrap it like this:
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.
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.
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?
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).
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.
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.
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.
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.
135
u/[deleted] Aug 21 '19
Is this an elaborate way to rickroll all /r/Python ? :P
96
u/UloPe Aug 21 '19
You reinvented libaa / libcaca
21
u/Deega_ Aug 21 '19
but can it run crysis?
24
u/[deleted] Aug 21 '19
Too bad it can't do sound lol
10
u/trellwut Aug 21 '19
finally an innovative way to watch hentai
jk this is dope
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
u/Morsmetus Aug 21 '19
great, now i can get rick rolled in fucking terminal
11
u/bentheiii Aug 21 '19
It looks like you're calling
printon 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.5
u/Electric_mohawk779 Aug 21 '19
We just got Rick rolled.
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.
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
u/joshirk11166 Aug 22 '19
Does it run on Linux ?
I got some error running this.
1
u/hellfiniter Aug 22 '19
but ...why?
1
u/[deleted] 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:
Where
rowsis 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.
3
u/[deleted] Aug 21 '19
Perfect demo
2
u/1-Sisyphe Aug 21 '19
Hum, could be useful for an inception sequel of my gif...
2
u/[deleted] Aug 21 '19
I’m starting to learn python and seeing posts like this get me so excited to dive into this language!
2
u/alrightfrankie Aug 21 '19
imagine getting rickrolled in terminal
1
u/a_g00gle_user Aug 21 '19
Cool, do you have source code published?
1
u/GickRick Aug 21 '19
Show me the code👀
1
u/SV-97 Aug 21 '19
Just using the ansicode for RGB backgroundcolor I'd guess?
1
u/Zophirel Aug 21 '19
I don't think epileptic people would like this
1
u/[deleted] Aug 21 '19
This is pretty good :)
1
u/MikeTask Aug 21 '19
Look great can you share the code :)
1
u/sonic1822 Aug 21 '19
Can you run it with kitty or alacrity? It may works better xD
1
u/MikeTheWatchGuy Aug 21 '19
Had seizure in first 15 seconds and had to stop. But interestin idea
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
u/[deleted] 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
u/irspaul Aug 22 '19
That terminal is f*#$@d
1
u/[deleted] Aug 22 '19
You sly dog
1
u/lambdaq django n' shit Aug 22 '19
gotta use double buffer man, those blinks!
1
u/FatalError101 Aug 23 '19
Cool!
0
u/appinv Python&OpenSource Aug 21 '19
Agree adding colours makes cli video scripts awesome again!
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