Press "Enter" to skip to content

Create a Radial Movie/TV Barcode Using Polar Coordinates

This post is going to be a little different from my usual, as it relies heavily on calls to ffmpeg. This should work on Linux/OSX just fine, but the system call will need to be modified for Windows.

Avatar: The Last Airbender

Project Setup

The first part of this will rely on you having the video files on your computer. You could recreate this by playing the video and taking screen captures at 1 second intervals, but having the video files will be a lot faster.

For this tutorial, I used the path ./import/avatar/s01 for the video files. The name of the video files shouldn’t matter as long as when sorted alphabetically, they are in the correct order.

Required Libraries

The next step is to load the reqired libraries. Glue just makes the readability much easier.

You will also need ffmpeg installed. You can do this on OSX with brew install ffmpeg or on Linux with sudo apt-get install ffmpeg. This will be needed for the bulk of the work.

Create the Frames Directory

We will be writing the frames to ./export/frames/s01 for this example. I was initially going to save all 3 seasons for the tutorial, but ran out of disk space.

The 1px exports won’t be as useful as the the frames exports, otherwise I would recommend seperating into seasons as well. The 1px folder can just be dumped upon completion or when rerunning with different scaling parameters.

Creating Frames

Now it is time to put ffmpeg to work! We will be reading in all of the files in the s01 directory, extracting the season and episode pattern (ex: S01E01), and creating a frame capture for every second. We are storing the frames in the season and episode directory (ex: S01E01) using a 4 digit numerical string (ex: img0001.png). If you create captures more frequently than every second, or the videos are longer, you may want to increase the digits. For S01E01 the last frame captured for me was img1272.png.

Resizing Frames to 1px Width

Now that we have the frames all ready, you might want to look at your disk space. We just added tens thousands of png files. If you are fine on disk space, we can generate even more images!

The code below reads in all of the frames from each episode and generates a 1×20 pixel image. There is likely a faster way of doing this.

Reading in the Data

Now to the core of the visualization. I found readPNG works best for me. Esentially we are looping through the multidimentional array provided by readPNG, where the last dimention is an array of RGB values for the x and y coordinates of the image.

This will bind the rows in a long format, resulting in a dataframe that will look like this.

columnrowvalues
120#020003
119#010002
118#020002
117#020002
116#010002
115#020002
114#010002
113#020002
112#020002
111#010002
110#010002
19#020002
18#010002
17#010002
16#020002
15#010002
14#010001
13#020002
12#010002
11#010002

The first column (column) is is defines the x position, and the column row defines the y position. Right now, you would get a lot of pixels overlapping if you tried to plot the data.

Avatar: Tile Overlays

While this is pretty cool, it isn’t exactly what we are going for. If we want the episodes to stack vertically, we will need to create an offset for row y (row) field based on the episode number. Since we made the 1px wide images 20px tall, we will offset by 20.

Ready to Graph

Alright, we made it! We are ready to graph! We are using theme_map() just to hide all of the regular plot options, like axis, tick marks, and titles.

Avatar: Rectangle Bar Code

Now this is interesting. The episodes aren’t all of the same length. We can fix this by just sampling an equal number of frames and ordering sequentially between the opening and ending credits. That can be a little bonus for you to implement.

Polar Plunge

Polar coordinates are slow. They are soooooo slow. This is due to resizing all of those little rectangluar tiles based on the position. There are faster ways of doing this, but not in R (that I know of).

We also want to expand the limit of the y-axis for the nice donut effect. The closer to the center of the image, the more warped the frames get. This will also give you some room to label the center. Expanding the y-axis to roughly -25% of the max real_row value is a good start.

Avatar: Rectangle Bar Code

Awesome

Now add some style to the graph, throw on some text, or maybe take it upon yourself to nest all 3 seasons inside of each other. This is a great way to break TV and movies into halves or quarters, and nest an average pallete in rings between the episodes as well. Have fun!

Sharing is caring!

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *