Skip to content

Instantly share code, notes, and snippets.

@candycode
Last active December 19, 2015 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save candycode/5900316 to your computer and use it in GitHub Desktop.
Save candycode/5900316 to your computer and use it in GitHub Desktop.
Download youtube audio from ubuntu: adds artist and title as metadata and copies everything into /Music/<artist> folder. Supports both flv and mp4 formats.
#!/bin/bash
#Original script:
#http://www.linuxjournal.com/content/grabbing-your-music-youtube-do-it-your-way
#modified to work with avconv
#Ubuntu >= 13.04: requires youtube-dl and the following packages:
#sudo apt-get install python-pip
#pip install youtube-dl
#sudo apt-get install libavcodec-extra-53 libav-tools
address=$1
regex='v=(.*)'
if [[ $address =~ $regex ]]; then
video_id=${BASH_REMATCH[1]}
video_id=$(echo $video_id | cut -d'&' -f1)
video_title="$(youtube-dl --get-title $address)"
youtube-dl --id $address
ext="flv"
if [ ! -f "$video_id"."$ext" ]; then
ext="mp4"
fi
rxauthor_title='\s*(.+)\s+-\s*(.+)'
if [[ $video_title =~ $rxauthor_title ]]; then
author=${BASH_REMATCH[1]}
title=${BASH_REMATCH[2]}
avconv -i "$video_id"."$ext" -aq 256k -metadata title="$title" -metadata artist="$author" "$video_title".mp3
if [ ! -d "$HOME"/Music/"$author" ]; then
mkdir "$HOME"/Music/"$author"
fi
mv "$video_title".mp3 "$HOME"/Music/"$author"/"$title".mp3
else
avconv -i "$video_id"."$ext" -aq 256k "$video_title".mp3
mv "$video_title".mp3 "$HOME"/Music/
fi
rm "$video_id"."$ext"
echo 'done'
else
echo "Sorry but the system encountered a problem."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment