youtube2iTunes, a very simple shell script

Posted on Mon 02 September 2013 in development

A while back when updating my homebrew packages I came across youtube-dl but never had enough motivation to check it out. I hardly ever need to download videos from Youtube, but I was in need of this again when spending more time using a metered internet connection (read: mobile broadband). To be able to use as much of the traffic volume for useful things (like pictures of cats) I usually try to cache as much video content as deemed necessary on my laptop. That involves downloading from Youtube, preferable in a format that my iOS devices understand so I can sync everything without having to convert between different video formats and video containers.

I formerly used JDownloader for uhm, various activities which I don't do anymore. The software remained on my machine for the purpose of downloading from video sites, primarily Youtube. Now, for different reasons I am no fan of Java on OS X. Java regularly has security issues. The technology is largely outdated in browsers. Few applications are written in Java and properly uphold the patterns in terms of usage and interface design that are present in the Cocoa framework. A notable exception is Cyberduck, a file-transfer solution for multiple protocols I'm very fond of. JDownloader, as I have complained in the past is ugly and doesn't care about platform conventions, regardless of the platform. I was willing to replace that with something better any day.

However, to use my iOS devices with youtube-dl some little trick had to be done, because I wanted to only download the specified formats. Also I didn't like the thought that I would need to manually import the files into iTunes every time. That wasn't necessary though since iTunes can import from a special folder since version 9. All that was left for me was to string those things up into one nice script and be done.

echo "Downloading $1"
youtube-dl -f 37/22/18 -o "$HOME/Downloads/%(title)s-%(id)s.%(ext)s" $1
mv ~/Downloads/*.mp4 ~/Music/iTunes/iTunes\ Media/Automatically\ Add\ to\ iTunes/

This nice script does exactly that:

  • outputs a little confirmation to the terminal with the URL you handed it
  • downloads the file as MP4, trying to download 1080p, 720p and 270p/360p resolution versions of the video. (should you need other versions check the wikipedia table)
  • moves all .MP4 files in your ~/Downloads folder to the special iTunes folder which automatically adds them to your library.

Combine this script with the iTunes sync preference Movies / Sync Movies / Automatically include X most recent unwatched movies along with selected movies and everything you download gets synced to your devices whenever you sync to iTunes.

You use the script by saving it to a file, making that executable with chmod +x <filename>, adding it to your PATH and calling it from Terminal via youtube2itunes <URL>.


Thanks to Thomas for helping me find the correct way to specify the home directory in the youtube-dl command.