Sometimes I come across stuff on YouTube and think: “I wish I had that as an mp3.” Apparently I’m not alone; there is actually a project on github, youtube-dl, which is a command line tool for downloading youtube videos as FLV files. With some simple manipulation via avconv (or ffmpeg), the audio can be extracted and saved as an mp3. I took this a bit further and created a script that accepts a list of files in the format “http://youtube.com/whatever,Title,Artist,Album” and automatically saves the linked video as an mp3 file with the ID3 information supplied.
First, make sure you have the necessary packages. You will need to add the webupd8 PPA to get the youtube-dl package:
$ sudo add-apt-repository ppa:nilarimogard/webupd8
$ sudo apt-get update
$ sudo apt-get install libav-tools youtube-dl libid3-tools
Then create a new file with $ nano ~/yt2mp3
and copy/paste the following gist into it:
#!/bin/bash
# Simple shell script for downloading audio from YouTube as .mp3 file
# Requires:
# * youtube-dl (http://rg3.github.io/youtube-dl/)
# * id3tag (Ubuntu libid3 package, based on http://id3lib.sourceforge.net/)
if [ -z "$1" ]; then
echo "Usage: yt2mp3 <file>"
echo "File supplied must have a list of URLs to YouTube videos and ID3"
echo "information, comma-separated."
echo "For example: http://youtube.com/whatever,Title,Artist,Album"
exit
fi
IFS=$'\r\n'
VIDEOS=($(cat $1))
for LINE in "${VIDEOS[@]}"
do
# Parse the song file:
URL="$(echo ${LINE} | sed -ne 's/^\(.*\),\(.*\),\(.*\),\(.*\)/\1/p')"
TITLE="$(echo ${LINE} | sed -ne 's/^\(.*\),\(.*\),\(.*\),\(.*\)/\2/p')"
ARTIST="$(echo ${LINE} | sed -ne 's/^\(.*\),\(.*\),\(.*\),\(.*\)/\3/p')"
ALBUM="$(echo ${LINE} | sed -ne 's/^\(.*\),\(.*\),\(.*\),\(.*\)/\4/p')"
FILE="${ARTIST} - ${TITLE}"
# Download the file:
echo -ne "Downloading ${TITLE}..."
x=~/.youtube-dl-$RANDOM-$RANDOM.flv
youtube-dl --output=$x --format=18 "${URL}" > /dev/null 2>&1
# Convert to MP3:
echo -ne "Converting to mp3..."
avconv -i $x -acodec libmp3lame -ac 2 -ab 128k -vn -y "${FILE}.mp3" > /dev/null 2>&1
rm $x
# Add ID3 info:
echo -ne "Adding ID3 info..."
id3tag -s "${TITLE}" -a "${ARTIST}" -A "${ALBUM}" "${FILE}.mp3" > /dev/null 2>&1
echo "Done!"
done
Make sure to give this file executable permissions, then save it to the system:
$ sudo chmod ugo+x ~/yt2mp3
$ sudo install yt2mp3 /usr/local/bin
Now, you can hit YouTube and start finding videos that you would like to get as mp3. As an example, create a new file with $ nano ~/audio
and add this line: http://www.youtube.com/watch?v=oHg5SJYRHA0,Never Gonna Give You Up,Rick Astley,Internetz
Now, simply run yt2mp3 ~/audio
. You should get:
$ ./yt2mp3 ~/audio
Downloading Never Gonna Give You Up...Converting to mp3...Adding ID3 info...Done!
You should now have a file titled “Rick Astley - Never Gonna Give You Up.mp3” in whatever directory you were in when you ran the command. The cool thing about this is that you could create an Excel or OpenOffice spreadsheet of audio files and corresponding ID3 information, then export it as a CSV and pass it to yt2mp3. It will save the entire list to whatever directory you’re in when you run the command. Further, you could easily extend it to add additional information, like genre, year, track number, etc. You could even modify the script to change the file naming convention, or organize the files into folders.
Anyhow, I’ve already gotten a ton of use out of this script, so I thought I would put it out into the world for others to use as well.