Wishful Coding

Didn't you ever wish your computer understood you?

Make all the Star Wars memes

Normal people watch movies or play games to relax. Me on the other hand, I write code about movies or games to relax.

I came across some memes that posed the question if every line from the Star Wars prequels is meme material, accompanied by a shot from said prequel with a character confirming said question. Easily nerd-sniped as I am, I thought, surely there are lines in the movie that are completely boring?

I figured it should be doable to extract the subtitles from the movie, and use those to generate every possible Star Wars meme. Well, at least all the ones adhering to the format described above.

Extracting the subtitles and timestamps

It turns out srt subtitles are a pretty easy format to grep, but in case the subtitles are embedded inside the video file, or in some other binary format, ffmpeg got you covered. Once you have the srt file, a simple grep command can be used to extract the timestamps.

Note the extra space and lack of decimal point. I’m lazy, and this seemed the easiest way to get a timestamp that refers to a frame when the subtitle is displayed. This sometimes fails, so a mean between the start and end time is obviously better.

ffmpeg -y -txt_format text -i sw.mkv out.srt;
timestamps=$(grep -oE " [0-9]{2}:[0-9]{2}:[0-9]{2}" out.srt);

Generating images for every subtitle line

Again, ffmpeg can do the job, but it requires a bit more than a straight copy-paste from the manual. Let’s go over the options after the full command, that I run inside a for loop.

ffmpeg -y -ss $ts -copyts -i sw.mkv -vf subtitles=sw.mkv -frames:v 1 frames/out$i.jpg;
  • -y answer yes to all prompts.
  • -ss timestamp seek to the specified timestamp. It is important this comes before the input file, otherwise it’ll render the whole movie up to that point. However, doing so changes the timestamps, which we need for the subtitles.
  • -copyts preserve the timestamp. This was a life-saver, thanks to the ffmpeg IRC channel.
  • -i the input file…
  • -vf subtitles=file specifies a filter that “burns” the subtitle into the movie.
  • -frames:v 1 save a single frame to the specified output file

Add the meme caption

With the hard part behind us, now we’re back to straight copy-pasting from the ImageMagick manual. The only interesting bit I added is the pointsize.

convert frames/out$i.jpg -background White -pointsize 32 label:'Insert funny caption' +swap  -gravity Center -append memes/meme$i.jpg;

Putting it all together

The whole script can be found here. I did not expect much, but it was already at frame 6 that I was pleasantly surprised.

Yes, of course

But then I thought

Surely you can do better

So I created a Twitter bot and a search page.

The Twitter bot is just an IFTTT applet that posts an image, served up by a mind-numbingly simple PHP script that I copied from somewhere.

For the search script, I wrote a little Python script that converts the srt file to a csv file that I can import into a MySQL database, which I then query using SELECT * FROM subtitles WHERE MATCH(sub) AGAINST ('words').

That’s all for now.

May the force be with you

Pepijn de Vos

Generate Cities: Skylines height maps

Over the Christmas holiday I played a bit of Cities: Skylines. I wondered if it would be possible to import a real-world location from Google Maps or Open Street Map or something. I quickly found terrain.party, and downloaded a couple of locations.

I found some videos of people building polders, so I thought it’d be fun to import a real Dutch polder to play on. But to my disappointment, the height map from terrain.party was quite bad around the coastlines and did not seem to deal well with land below the sea level.

However, the readme on the files explained the data sources and the file format used by Cities: Skylines. So it seemed pretty easy to write a simple script to generate my own tiles. Nothing is ever as easy as it seems, but a week later, I have a somewhat working script that generates playable maps that look nicer than terrain.party.

comparison

My script is based on exactly the same SRTM3 data als used by terrain.party (amongst other sources), but produces much nicer shorelines as well as smoothing the raw data to avoid that pixelated look. There are also a lot of other parameters to tweak.

After some searching, I found the Mapzen’s S3 bucket which offers easy access to the void-filled, 30m resolution SRTM3. But the real key to smooth shorelines is the SRTM Water Body Data, which I used to mask out the water with a negative altitude. As a finishing touch I applied a light Gaussian blur to the upscaled data to remove the hard edges.

Download the script here

Initially I developed the script under Linux, but switching to Windows for testing with the actual game was surprisingly painless, once I installed Anaconda. Anaconda takes care of compiling all the annoying C extensions, which is a huge relief. I highly recommend it.

For my personal use, some parameters and assumptions were just hardcoded, but I’ve since added a simple command-line interface so you can adjust the size of the map, how much blur is applied, height scale and offset, and disable shoreline fixes.

Pepijn de Vos

The Science of Logic and the Science of Experiment

When discussing ideas about our universe that are in some way controversial, the scientific argument is often brought up. In the words of everyones favorite astrophysicist:

The good thing about Science is that it’s true whether or not you believe in it.

This notion has been on my mind quite a bit lately. As an engineer and somewhat rational being, I depend on a lot of science, but as a human being, I also find some of its explanations somewhat lacking.

It occurs to me that there are two kinds of science. The science of logic, which provides undeniable formal proofs based on a set of axioms (from the Greek ἀξίωμα, “that which commends itself as evident”). The evidence for these axioms is provided by the Science of Experiment, where it is key to note that experiments and the humans conducting them are fallible.

So to have an alternative view of the universe does not mean to reject science and logic, it means to reject the outcome of an experiment that lead to an axiom that lead to a proof. The question then becomes, which experiments do you trust?

The easy ones are the ones you experience on a daily basis or can easily verify yourself. I’m pretty sure Newtonian physics work pretty well on the human scale, but I’ve not personally seen an atom, traveled near the speed of light, or even seen the earth from space, so how can I know about those things for certain?

One step down are experiments that others have done and verified, that make a lot of sense. I did not personally verify the experiment, but as a personal hypothesis (ὑπόθεσις, “to suppose”) they explain the things I see pretty well. Suppose electrons exist and move towards the lowest potential, my electric circuits make a lot of sense. Suppose the earth is round, it makes a lot of sense I can’t see the Eiffel tower from my window. (and a lot of other things make sense that way, too)

Then there are things that I have no idea about, one way or another. In these cases it just comes down to who I trust. Ideally, peer-reviewed journals should provide a high degree of trust, but in the face of the current reproducibility crisis, prestige-driven research, and questionable industry ties, one might start to wonder how trustworthy some research really is.

I have absolutely no clue about quarks, quantum entanglement, black matter, the big bang, and many other things. When you start telling me the world is really 12 dimensions but they’re rolled up in a ball, that’s very, very far removed from anything I can see and touch. I have no reason to doubt these things, so I trust them to be more or less true until a better story comes around.

A more problematic case is the Healthcare industry, which makes money as long as you’re not healthy. There is a lot of questionable and contradictory information out there form all sides. So it’s not surprising there is an antivacc movement that trusts anecdotal evidence above published research.

And finally, there are things where the scientific community kind of misses the point. It might be that homeopathy is just placebo effect, but it glosses over some remarkable detail: The way you think about a medicine apparently influences how well you feel.

This seems to me like a profound concept, indicating that body and mind influence each other. This is what I would call the holistic axiom, that everything influences everything. It underpins many alternative concepts such as homeopathy and astrology, that have elevated the study of these connections to an art form.

Pepijn de Vos