Skip to content

Instantly share code, notes, and snippets.

@robsears
Created November 20, 2013 17:27
Show Gist options
  • Save robsears/7567308 to your computer and use it in GitHub Desktop.
Save robsears/7567308 to your computer and use it in GitHub Desktop.
A simple shell script for downloading audio from YouTube and saving to MP3 format, with ID3 information.
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment