← back

Another way that videos should be watched

The idea

There's this incredible thing with terminals like iterm2: you can have a background image. I had interacted with this in the past but hadn't really given it much thought... but everything changed when i realized that you can control it with a program (the true question is wether there's anything you can't control with a program! computers are great!). The immediate thought when faced with this information is to create animations.

This is the point of this project: Try to create animations or videos in the terminal's background.

The fist prototypes

So that's what I did. My first draft was an animation with a ridiculous picture of me that a friend of mine had told me to put as a background. I created more frames by drawing my eyelids shutting and opening. This would happen at random intervals. It was quite creepy. I'm nice enough to spare your eyes from a terrifying uncanny valley animation of me blinking so instead, i've created a video of their Momoness blinking at you.


Lord Momo of the Momo Dynasty is here to keep you in check while you code

Creating a video

All right! This is a good start, but who says animation says video! I think that I might need to continue what might become a tradition and try to render a video in another place that really wasn't meant for it.

Ok so this time, it could be a lot simpler. We just have to transform a video into frames that we can then feed the program.

The first step is really simple. We just need to run a long ffmpeg command that does everything for us:

ffmpeg -i input.mov \ -vf "fps=5,scale=800:600:force_original_aspect_ratio=decrease,eq=contrast=1.2:brightness=0.1" \ -q:v 8 \ "frames/frame_%04d.png"

The simple command would just be ffmpeg -i input_video.mkv path/to/output/frame_%04d.png and the rest is just to try to avoid ending up with hundreds of frames and huge file sizes.

With a directory full of frames, running a video becomes trivial. We just need a simple script that can read through the file names and run a command in the terminal to make frames become the background images at regular intervals.

This is the result:


This looks like a perfectly normal rickroll video but keep in mind that this is all in the terminal

Going down a rabbit hole because of a misconception on how terminals work

Ok so this is quite fun. Being able to run essentially any video in the background of a terminal is quite fun but you might have observed that there aren't many indications of the video being a terminal. For all you know, i might be running the video in subtitles and lying about the terminal thing!

This is for a very good reason: terminal instances are essentially signal-process things (EDITOR'S NOTE: It turns out that this is false as I later discover. I'm still leaving the text where I made the mistake because I still find it interesting). In one instance, you can only run one process at a time. Thankfully, it's possible to create multiple tabs and windows and run many things simultaneously. The reason for which this is relevant is that running a script to change the terminal backgrounds takes a whole process and the echo "\033]1337;SetBackgroundImageFile=$(echo "/path/to/img.jpg" | base64)\a" command only works on the current instance. This means that you can either have your normal instance or have a rickroll but not both with the current setup. This is sad, as I want to get rickrolled while coding or else it just isn't interesting.

Thankfully, there are two solutions (at least two)!

Using osascript

The first is to change commands and use osascript -e 'tell application "iTerm2" to tell current session of current window to set background image to "/path/to/img.jpg"'. It's a very similar command to the first one but it runs on all of the terminal instances at once. In theory this means that we could run it in one instance and enjoy coding in the best code editor in the world all while getting rickrolled. In practice, the drawback of osascript and the reason for which I haven't been using it since the beginning is simply that it's very slow.

Implementing the version with osascript is really easy as I essentially just need to change a line: swap echo for osacript and see the framerate go down but the process be multiplied to every instance of a terminal. Sadly, osascript only changes the background on terminal windows that are active. This means that it's absolutely possible to watch a video while coding, but my hopes of having many iterm windows with rickroll running were dashed by this fact. The closest I could get to this dream was trying to cycle through the windows quickly but that didn't work well at all.

The other method

The other method: i'll describe it quickly because I never actually enacted it because I realized that it was useless as you'll soon read. The second method would have simply been to make the python script that controlled the background create an instance of a terminal that i could have controlled. The python script would have essentially worked as some kind of proxy or middle layer between me and the terminal version that I could control.

The realization of my foolishness

While working on this project, I faced many moments when the ctr + c command didn't actually stop a process because it was updating at too many frames per second. The osascript version was impossible to stop so I looked online and found the ctr + z command. It didn't quite do what I wanted it to do but it showed me the incredible world of jobs. I have become a changed person. This is incredible! It opens up a whole world of possibilities for plenty of different things that I would usually do another way when coding but more than that: it solves the single-process problem! I was completely wrong: terminal instances aren't at all single-process systems.

This means that you can just start the process and then run cmd + z and make it a background task and then go on using the terminal to order coffee or to play tetris.

This gives a great result illustrated bellow:


Editing the code while it's running

Or an even funnier result:


I love the internet. Thank you ascii.live/rick

The great thing with this kind of project is that you discover many things. For example, I discovered the jobs part of the terminal which lets you run multiple tasks at the same time. I also discovered the ascii.live project: their github.

I am very satisfied with this project as it has managed to do what I wanted it to do: improve my coding experience tenfold by making coding more addictive:


The holly grail of setups: nvim + stupid mind-slush videos

I know that there isn't much of a difference between the current situation and just playing a video on the side, but this project really shines on long lines of code that remind you that you cannot escape the terminal video. It will always be there, trying its best to distract you.

Afternote

I've realized something rather interesting: iterm2 verifies that the user really wants to let a program change the background. For each one of the videos above, I've had to click More Actions -> Always for every single frame. This has severely limited the number of videos that I've tested this setup with.

The verification message that iterm2 gives for every frame that the program tries to set as background

With a bit more experimentation, I realized that iterm has no way of knowing what the actual image file is, it only remembers the path and file name. This means that instead of accepting for every frame, I could just create a program that would change the contents of a single image file that has the permissions to run any video of any length!

I think that there are many interesting things that could be done with this project and I'll probably come back to it.