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

Advertisements

7 thoughts on “Bash script to download and rip Youtube playlists to mp3

  1. Stormer97

    Not sure if this blog is still active, but this script does not work for me (details below)

    the script itself, as I have it saved: http://pastebin.com/MQzYFpK3

    permissions on the script: -rwxr-x–x 1 main main 819 Jan 10 13:02 dlPlaylist.sh

    uname -a: Linux laptop 3.11.0-15-generic #23-Ubuntu SMP Mon Dec 9 18:17:04 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

    The OS is ubuntu 13.10

    the error message: http://pastebin.com/0WeWXp3q

    I set this comment to email me if it updates, so I should get any responses. Please let me know if there is a fix, or if you need any extra debug information!

    • Stormer97

      Okay, so I found the problem. First, I did not have the correct codec installed, and second, youtube-dl was failing to download a video. I fixed the problem by installing libavcodec-extra-53 (NOTE to future readers, if you cannot apt-get this, try running sudo apt-get update && sudo apt-cache search libavcodec-extra). I also made some improvements to your script, you are free to put it back up in the main article, modify it further, or not… whatever floats your boat.

      Link: http://pastebin.com/sg6Xcubz

    • Well, four years ago youtube-dl had some problems downloading playlists, but you’re right. In fact, last week I installed youtube-dl-qt to my woman’s computer and it works perfectly with playlists and with a simple GUI for the normal user. It lacks some output conversion but that can be achieved with soundconverter, soundkonverter or winff.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s