r/Python • • Aug 21 '19

I created a python script that can (technically) play videos inside of the terminal!

1.5k Upvotes

79 comments sorted by

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.

14

u/oslash Aug 21 '19

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.

4

u/epic000 Aug 21 '19

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.

5

u/aes110 Aug 21 '19

Where did you get all of the "font stuff" from? https://github.com/epic0007224/Terminal-Video-Player/blob/master/TerminalVideo.py#L28

I'd like to see some explanation of it

8

u/epic000 Aug 21 '19

I got it from this stackoverflow question: https://stackoverflow.com/questions/3592673/change-console-font-in-windows

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.

4

u/jantari Aug 21 '19

The documentation for that is here:

https://docs.microsoft.com/en-us/windows/console/console-font-infoex

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.

123

u/SadWebDev Aug 21 '19

Even though it's badly written, Python will never gonna give you up, never gonna let you down.

25

u/sztomi Aug 21 '19

But is it gonna turn around? And desert you?

11

u/SadWebDev Aug 21 '19

Of course not. It would never do that

9

u/CompSciSelfLearning Aug 21 '19

But what if Python makes me cry? Or says goodbye?

6

u/SadWebDev Aug 21 '19

He's a good guy. Always follows his PEPs

3

u/metaobject Aug 21 '19

Well, Python2 will.

5

u/needed_an_account Aug 21 '19

while True: it works

2

u/ben174 Aug 21 '19

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

it works

1

u/ben174 Aug 21 '19

I see what you did there :)

4

u/Jonno_FTW hisss Aug 21 '19 edited Aug 21 '19

I made something like this here: https://github.com/JonnoFTW/img_term I get ~20fps (at 96pixels wide) on my windows terminal. On linux I get ~35 fps

I get decent framerates on windows, perhaps you can look at my code to see how I got it to be fast (hint: use numba).

2

u/ElevatedAngling Aug 21 '19

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).

5

u/[deleted] Aug 21 '19

What happens when he asks you to build something that doesn’t have an example you can lift from google?

3

u/ElevatedAngling Aug 21 '19 edited Aug 21 '19

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

1

u/Low_Sector Aug 22 '19

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:

https://pypi.org/project/ezcurses/

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

u/[deleted] Aug 21 '19

Is this an elaborate way to rickroll all /r/Python ? :P

16

u/[deleted] Aug 21 '19

Yea OP I’m gonna need this script

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

2

u/ready-ignite Aug 21 '19

Bahaha. Brilliant.

96

u/UloPe Aug 21 '19

You reinvented libaa / libcaca

56

u/[deleted] Aug 21 '19

hey, reinvent and open-source are eternal lovers!

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

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

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

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 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

u/[deleted] 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

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.

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

u/epic000 Aug 22 '19

I’m working on improving it :)

1

u/joshirk11166 Aug 22 '19

Does it run on Linux ?

I got some error running this.

1

u/epic000 Aug 22 '19

Replace os.system(“cls”) with os.system(“clear”). I think that should fix it.

1

u/hellfiniter Aug 22 '19

but ...why?

1

u/epic000 Aug 22 '19

Because I was bored lol.

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:

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

u/[deleted] 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

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!

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