Write your own Youtube music downloader script

by
Share this Article: Facebook0Google+0Twitter6LinkedIn0Reddit0StumbleUpon0

It’s an undeniable fact that Youtube is one of the top three most visited websites in the world. Many people upload their personal moments, funny stuff, cover songs, podcasts, screencasts etc, all globally shared over the Internet.

These videos are supposed to be public property and for personal use only. But honestly, I can’t think of how many times I’ve visited Youtube to listen my favourite song or just keep following a random channel’s playlist full of copyrighted music. So the questions is ….

Is it legal to download videos from Youtube ?

Yes and no. Technically if you take a look under temp files of Ubuntu, the video you ‘re watching is already downloaded or at least there are some bunch of hidden data related with it. But just saying it is yours is a completely another thing. In general, if you already own the material, then it is okay (e.g. like ripping music from CDs to import mp3 files into your portable player). Although, nowadays there are so many different laws depending on your country/region, Youtube’s policy, music industry, MPAA/RIAA, ACTA, civil rights … and make things even more complicated. So please, before you download anything, make sure that this video is not copyrighted before you start downloading.

This article aims to show you the power of terminal, not enabling you into illegal activities. Thus, downloading copyrighted video or using it for personal profit (i.e. selling CDs with copyrighted material) is not acceptable.

Please use this guide for educational purposes only

Gathering dependencies

To download video from Youtube we are going to use the Youtube-dl and in order to convert the video file into audio (mp3) we will use the ffmpeg. So make sure you have both of these two applications installed correctly on your system. The following instructions are based in Ubuntu Precise Pangolin 12.04 LTS / Oneiric Ocelot 11.10.

sudo apt-get install youtube-dl
sudo apt-get install ffmpeg libavcodec-extra-53

Writing the script

Open your favourite text editor (VI, Emacs, Nano, Kate, whatever tastes you better). Personally, I prefer gedit. Please type the following command:

gedit myscript.sh

It’s super easy to understand the commands I used here –  please do and feel free to make any customizations according to your needs. This script asks you what artist and song do you want, then it searches for it and then downloads it from Youtube. Last but not least, ffmpeg converts your video (probably *.flv type) into an mp3 file using parameters for proper naming. Finally, your mp3 now moves into your /home/yourname/Music folder.

#!/bin/bash
echo What is the artist of the song?
read ARTIST
echo What is the name of the song?
read NAME
echo "##################################"
echo "#### P L E A S E      W A I T  ###"
echo "##################################"
mkdir temp && cd temp
youtube-dl "ytsearch:$ARTIST $NAME album version"
ffmpeg -i *.flv -aq 2 "${ARTIST} - ${NAME}.mp3"
rm -rf *.flv
cd .. && mv temp/*.mp3 $PWD && rm -rf temp
mv *.mp3 ~/Music/
echo Your song has been downloaded successfully

Running the script

In order to run the script you have to own the execution privileges. To do so, please type the following commands to add +x (execution) and run (./) the script:

chmod +x music1.sh
./music

As you can see from the script’s commands, we used only legal means to download music from Youtube. However this doesn’t make your download activity legal. Blame the act, not the tools. We just aim to show you how much powerful the command line is ;)

Proof of Concept

Now, if you knew me better, you would know that I love Pink Floyd and rock/metal music in general. I have all of their albums and a couple of T-Shirts from the “Wish you were Here” and “The Wall”. So does this make me owner-enough in order to download Comfortably Numb? I have paid the money for purchasing the CD, but again, law can be tricky sometimes, so we don’t recommend you to fool around with any copyrighted material. Let me say it once more: Unixmen.com has no responsibility for any illegal activities using this script.

Make your own customizations

Please feel free to make any necessary changes according to your needs. Let me share some of my own alternatives here:

Create a new txt file with all Youtube links you want to download and convert into mp3 and put it in the same directory with the script file. Here goes the code:

#!/bin/bash
echo "Type the filename:"
read FILENAME
mkdir temp && cp $FILENAME temp/ && cd temp
youtube-dl -i --max-quality=FMT -o "%(stitle)s" -a $FILENAME
rm $FILENAME && cd ..
for file in temp/*; do ffmpeg -i $file -aq 2 $PWD/$file.mp3 ; done
mv temp/*.mp3 $PWD && rm -rf temp
echo "Your videos are finally converted into mp3!"

For those who love the playlist stuff here is another way ;)

#!/bin/bash
echo Enter the youtube PLAYLIST url to begin download
read PLAYLIST
mkdir temp && cd temp
youtube-dl -cit $PLAYLIST
for file in *; do ffmpeg -i $file -aq 2 $file.mp3 ; done
rm -rf *.flv *.mp4
cd .. && mv temp/*.mp3 $PWD && rm -rf temp
echo Done! Enjoy!

Troubleshooting

The most common errors are spotted in the ffmpeg missing codec. Please google your error and fix your ffmpeg problem or compile the package manually. If ffmpeg is not a problem, then try to update yourube-dl. If non of these worked and you still have problem, take a look at the code. There are many available video formats in Youtube, but this scripts works only with flv. So if you encounter any mpeg, make your own changes in the code.

For questions please refer to our Q/A forum at : http://ask.unixmen.com/


Share this Article: Facebook0Google+0Twitter6LinkedIn0Reddit0StumbleUpon0