Almost a year ago Mark Mandel and I started the Google Cloud Platform Podcast, a weekly podcast that covers topics related to Google Cloud Platform, among other things. It’s been a pretty successful podcast, but that’s not what I want to write about today.
After a while we started receiving emails from listeners that wanted to access our podcast on YouTube. Even though this might seem strange for those that love podcasts and have their favorite app on their phones, we decided that the customer is always right: we should post every episode to YouTube.
Ok, so … how? Well, to create a video I need to merge the mp3 audio from an episode with a static image. Let’s include the title of the episode and the Google Cloud Platform Podcast logo.
But once we post the video to YouTube we’re going to need more than that! We need a description, some tags, and probably a link to the episode (SEO FTW!).
Where can we get that information from? Let’s think about this for a minute. Where are others getting this information from? The RSS feed! Would it be possible to create a tool to which I could say “post the video for episode 46″ and a couple minutes later the video appeared on YouTube? That’d be awesome! Let’s do that!
The application I wrote parses an RSS feed and given the episodes to publish it downloads the metadata and audio for an episode, generates the corresponding videos, and pushes them to YouTube.
|Diagram of the flow of data in podcast-to-youtube|
The hardest parts here are the creation of the image and the video. The rest is sending HTTP requests right and left.
Image Maker: rendering images in pure Go
After trying a couple of different tools I decided that the easiest was to create the image from scratch in Go using the image package from the standard library and a freetype library available on GitHub.
Probably the most fun part was to be able to choose a font that would make the title fit the image correctly regardless of the length in characters. I ended up creating a loop that:
- chooses a font and measures the width of the resulting text
- if it’s too wide, decreases the font size by one and repeats.
Surprisingly, for me, this is actually a pretty common practice!
It is also worth mentioning the way I test the package: Using a standard image that I compare to the one generated by the package, then showing a “diff” image where all the pixels that differ are highlighted in red.
|Diff image generated when using a wrong DPI.|
The code for this package is available here.
Video maker: ffmpeg is awesome
From the beginning I knew I would end up using ffmpeg to create my video. Why? Well, because it is as simple as running this command:
$ ffmpeg -i image.png -i audio.mp3 video.mp4
Easy right? Well, this is once ffmpeg has been installed and correctly configured, which is actually not that simple and would make this tool hard to install on any machine.
That’s why the whole tool runs on Docker. Docker is a pretty widespread technology, and thanks to Makefile I’m able to provide a tool that can be run like this:
$ make run
It took me a couple of days to write the tool and get it to a point where I could open source it, but it was totally worth it. I know that others will be able to easily reuse it, or even extend it. Who knows, maybe this should be exposed as a web application so anyone can use it, no Docker or Makefile needed!
Any questions? I’m @francesc on Twitter.
By Francesc Campoy, Developer Advocate