Bash script to download and rip Youtube playlists to mp3

Standard

Recently I’m watching once again Doctor Who. Its soundtrack is simply awesome, so I decided to download it and upload it to google music service to have it both offline and online in all my devices.

On the pirate bay there are only two OSTs of the six seasons. So I searched on youtube and, voilà, there I founded the complete OST for the six seasons. 🙂

So I made a search on AUR to grab a program to download all the videos from those playlist and rip them to mp3. I quickly found youtube-dl and youtube-dl-playlist.

Unfortunately, even though youtube-dl-playlist can donwload (and in my case, most of times, it simply fails to download) a youtube playlist, it does not convert the downloaded items (flv or mp4 videos) to mp3 so we need to use another piece of software in order to get our beloved mp3.

There are so many software for linux we can use to rip off the audio from video, but the simplest one is ffmpeg. In fact, the other software out there requires ffmpeg as dependence… it must be for something…

So I wrote a little bash script that do exactly that: it downloads a entire playlist from youtube and extracts the audio into mp3. Oh, it also takes the image from youtube and put it into mp3 😉

I would like to share it with all of you under the infamous license Do What The Fuck You Want To Public License.

#!/bin/bash
# youtube-dl-playlist-to-mp3
#
# Utility to download Youtube playlist videos and ripp off their audio into mp3
#
# @author Angel Docampo
# @license WTFPL version 2 ( http://sam.zoy.org/wtfpl/COPYING )
echo "This script is useful when you want to download and rip a complete Youtuble playlist into mp3"
echo "Enter a name for the playlist"
read -r NAME
echo "Enter the Youtube playlist URL to begin the process (e.g: http://www.youtube.com/playlist?list=PL3DFF2F30C0A04640 ):"
read PL
mkdir "$NAME" && cd "$NAME"
mkdir .tmp && cd .tmp
youtube-dl -o '%(stitle)s.%(ext)s' $PL
for i in *.flv; do
ffmpeg -i "$i" -acodec libmp3lame -ac 2 -ab 128 "${i%flv}mp3";
done
for i in *.mp4; do
ffmpeg -i "$i" -acodec libmp3lame -ac 2 -ab 128 "${i%mp4}mp3";
done
rm -rf *.flv *.mp4
cd .. && mv .tmp/*.mp3 . && rm -rf .tmp
echo "Conversion finished!"

If you are too lazy to copy and paste it, you can download it from here. Remember to make it executable with chmod +x youtube-dl-playlist-to-mp3 if you named it like that, and put a copy on /usr/bin or /usr/local/bin to make it accessible system-wide.
Of course, you must meet the requirements, those are ffmpeg and youtube-dl installed on your system.

 

EDIT:  You can now download another script that downloads directly the best quality audio file hosted on youtube without the need of transcode it. Credits and complaints to his author

https://github.com/jordoncm/youtube-dl-playlist