walls.corpus

By Nathan L. Walls

  • Sunset, Feb 25, 2017/Raleigh
  • Bare Trees, Nash Square/Raleigh
  • Sunset, Feb. 9, 2017/Raleigh
  • Bills/Raleigh

Benfits of "throwaway" scripting

I like listening to concerts from some of my favorite artists like Mogwai, Explosions in the Sky and Hot Chip. Some artists have a definitive place to go for concert recordings, such as Reflecting in the Chrome for Nine Inch Nails.

For most artists, I end-up visiting YouTube and finding a concert and recently, I’ve found a bunch on YouTube:

While watching on YouTube is great, I would like to listen to these concerts through iTunes or on my phone.

I looked up how I get YouTube video converted to audio and found this Meta Filter thread.

I ended up using the following idea, highlighted in this comment:

1
2
3
youtube-dl UUGB7bYBlq8
ffmpeg -i UUGB7bYBlq8.WHATEVER -vn \
  -acodec copy 'Artist -Title I Want.mp4'

Three keys here:

  1. Get the IDs of the videos I wanted to convert from YouTube. I did this manually
  2. Install youtube-dl, which I did through Homebrew
  3. Install ffmpeg, also through Homebrew

While there are plenty of online or graphical tools one could use to convert YouTube videos to audio, the benefit of a command line tool is that I could then use these tools in a couple of Ruby scripts.

A lot of times, writing code involves writing tests and solving a problem through an application. Theoretically, I could have done that here. But, that felt like overkill because, right now, I have eight or so concert videos.

I wrote two scripts to help me. The first is download.rb:

1
2
3
4
5
6
7
8
#!/usr/bin/env ruby

file_list = "concerts.md"
files = File.readlines(file_list)

files.each do |file|
  `youtube-dl #{file}`
end

In the file, concerts.md is in the same directory and just contains a list of YouTube video ids.

Once these were all downloaded, I needed another script to convert the video files to audio files. I also wanted to name the resulting files. I could do both with a simple data structure. So, I wrote converter.rb.

Neither of these two files is doing anything particularly difficult. I’m just running those command line utilties. But, I’m not having to run them repeatedly. I was able to use ls and Vim to get the file names into converter.rb, then regular expressions to coerce the file listing into a data structure. I filled out the :destination keys manually. That felt like a pretty decent balance of effort to automation.

If I use this file much more, I may improve both of these scripts into something more mature. But, without waiting for that to happen, I was able to take care of some very pragmatic automation right now to save me some tedium.

I’ll take that.