Grabbing Your Music from YouTube: Do It Your Way

 in

A few months ago my father-in-law said that his company was renewing their computers. When I heard that some second-hand PCs were about to be available, I decided to take some of them, thinking that a few old PCs would not hurt when it comes to enlarging my home network and doing experiments with GNU/Linux. When my father-in-law asked if it would be possible to reformat one of those computers so that he could use it at his home, I jumped at the opportunity to bring another user to the world of GNU/Linux. A few days passed and he was a happy user of his new computer running Ubuntu, and he was enjoying his Firefox while he explored the web. I don’t know what other people think or usability studies say, but he said that he had no problem using the system and he was surprised that I did not have to install an anti-virus.

After a few weeks he said that he discovered the wonderful YouTube system, and being an avid jazz listener he liked to explore lots of classical pieces from 1950s and 60s. He wondered if it would be possible to grab the music out of those YouTube videos and put them on a CD so that he could listen to them while he drove his car. First I thought that some Firefox add- on probably existed for that, but I was too lazy to search for it. Instead, I decided to see if I could hack my way through my favorite development environment, Bash, and come up with something that can handle the task.

The first step was to think about which components I needed so that I could integrate them using Bash, the perfect glue:

  • A utility that can download the video files from YouTube (or other video services)
  • A utility to extract the sound from the video file
  • And a utility that can convert the sound file to the formats I wanted such as MP3

Remembering that one of the key aspects of UNIX and GNU/Linux utilities is “do one thing and do it very well”, I came up with the following utilities that did one thing and did it very well:

  • youtube-dl: a nice Python script that can download videos from YouTube and other sites
  • ffmpeg: the powerful utility to process and convert any kind of video or sound file
  • lame: the perfect choice for encoding sound data into MP3

The first component, youtube-dl, is a very simple-to-use command line utility to download the video files from YouTube. For example in order to download Frank Sinatra’s “My Way” from http://www.youtube.com/watch?v=6E2hYDIFDIU all you have to do is issue the command:

$ youtube-dl http://www.youtube.com/watch?v=6E2hYDIFDIU

to have the video file stored at the some location. In this particular case you should be able to see 6E2hYDIFDIU.flv at the directory you ran the youtube-dl command. You also have the capability of getting the title of the video automatically with the help of youtube-dl:


$ youtube-dl --get-title http://www.youtube.com/watch?v=6E2hYDIFDIU 

Frank Sinatra, My Way, With Lyrics

Once you have video file, the second step is to extract the audio data from it which can be easily achieved with a simple ffmpeg command:

$ ffmpeg -i 6E2hYDIFDIU.flv 6E2hYDIFDIU.wav

This will immediately extract the audio information from the already downloaded file into a WAV encoded file. And once you have audio data in that file, it is ready to be converted into and MP3 file which can be done using the lame encoder utility. As with the previous utilities the basic usage of lame is very simple:

$ lame 6E2hYDIFDIU.wav 6E2hYDIFDIU.mp3

Voilà! You have your MP3 file ready for your listening pleasure. You can delete the big .wav file and burn your .mp3 file to a CD to listen to it on your car stereo.

Once we have all the components ready and tested we can create a very simple Bash script that can take the input as the YouTube web address and produce the MP3 file as the output:

1 #!/bin/bash 
2	# A very simple Bash script to download a YouTube video 
3	# and extract the music file from it. 
4 address=$1 
5 regex='v=(.*)' 
6	if [[ $address =~ $regex ]]; then 
7	video_id=${BASH_REMATCH[1]}
8	video_id=$(echo $video_id | cut -d'&' -f1) 
9	video_title="$(youtube-dl --get-title $address)" 
10	youtube-dl $address 
11	ext="flv" 
12	ffmpeg -i $video_id.$ext "$video_title".wav 
13	lame "$video_title".wav "$video_title".mp3 
14	rm $video_id.$ext "$video_title".wav 
15 else 
16	echo "Sorry but the system encountered a problem." 
17 fi

If you save this script as youtube2mp3.sh and turn it into an executable by issuing the

chmod +x youtube2mp3.sh

You can run run it to download “My Way” using the following command:

./youtube2mp3.sh http://www.youtube.com/watch?v=6E2hYDIFDIU

The fourth line of the script is where we get the YouTube video address from the command line as the first parameter. Lines 5, 6, 7 and 8 rely on the use of regular expressions in Bash and the cut utility. We want the part which is starting after ‘v=’ and we don’t need the parameters that may follow it such as ‘&feature=related’. Once they are processed we get the unique code that YouTube uses to identify the videos and store it in the video_id variable. Line 9 is where we retrieve the title of the video file with the help of youtube-dl and store it in the video_title variable. For reasons of simplicity I assumed that the extension of the video file will be ‘.flv’ and stored it in the variable ext. Lines 12 and 13 finish the task of extracting of sound data and storing it in an MP3 file and finally at line 14 we do the house cleaning by deleting the video file as well as the .wav file that is no longer required. If the script encounters any problem with with the YouTube address format that was provided line 16 reports a very simple error message.

The simple Bash script above is far from perfect. Its purpose was to be as simple as possible without taking lots of important things into account such as realistic error handling, different YouTube video formats and a simple help system to report the usage in case the parameters were not entered correctly or missing. A slightly more complicated Bash script which also makes use of a simple graphical user interface with the help of wonderful xenity utility is available at https://github.com/emres/youtube2mp3 (feel free to hack the code and send pull requests to the author ;-) As usual I had a lot of fun exploring those utilities and combining them to create a solution that can be useful for other people, too. Of course that would be almost impossible without the flexibility of GNU/Linux and the free software world in which we live. I hope you’ll enjoy your music while you drive your car as much as I and my father-in-law do.

Happy hacking.

______________________

Emre Sevinç currently works as a software developer and researcher. He's been involved with GNU/Linux since 1994 when he first met it at the math department of Istanbul Technical University.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Grrrrrreat!

r0lling's picture

Everything worked great except the fact that youtube-dl downloaded and saved as a .mp4 instead of a .flv ??? But a very minor change to the little streamline shell script you made and works like a charm. xD

Youtube

Melissa Norman's picture

Just the info I needed.I've been trying to download some music from YouTube.But this looks pretty promising.

Lots of Info

ACT Safety's picture

Thanks for discussing this. Will help us as well.

Thanks
Daniel
http://bx.businessweek.com/profile/daniel-hamilton/dhamilton374/

Download helper + ffmpeg

dtholmes's picture

When I need to do this for a legitimate purpose:
I just use firefox download helper and get the mp4 or flv or what have you.
Then I use:
ffmpeg -i input_file.mp4 -ab 128 output_file.mp3

+1

Anonymous's picture

+1

ways to grab music from YouTube

Lindsay's picture

Hi, thanks for your information.
I use a little to program to grab or burn audio from YouTube. I am not sophicated about using computer technology. Simply I like using audio recorders like Audacity to record audio from YouTube straightly.

wow that's complicated!

Mike_B's picture

MAN! that is way too compplicated!
I just used a little program called Clive. Install it, type in clive ( video ip address) at the command line and it downloads the video to your download folder. Open the vid with Audacity and save tfhe audio to MP3. Done!

Aggree +1

Pure SEO Auckland's picture

Yeah, finding too complicated as well.. lolzz

The way you mentioned is really easy.. we work in youtube marketing and other online marketing strategies where we need to get audio from youtube and its really simple with the help of firefox addons as well :)

Cheers,
http://www.pureseo.co.nz/youtube-advertising

Very Imformative

JamieHopes's picture

Thanks for this information. I use audacity but I will try this as well.
Kindle for Sale

grabbing music from youtube

mglimpsee's picture

Great article I myself just use audacity to capture the audio and then can convert and tweak the audio to my hearts content. Will try the script though.
thanks

Hi, just downlaoded the new

koriandus's picture

Hi, just downlaoded the new version of youtube-dl and found some really nice parameter:
--extract-audio
--audio-format=mp3

so just put the line into your .bashrc
alias ytdl='youtube-dl --extract-audio --audio-format=mp3 -t'
and you are able to download music with:
ytdl http://www.youtube.com/watch?v=9bskVSkAPnE

Great!!!

Knupilef's picture

As always... the content of your magazine, or in this case your site is great!

I just want to throw my two cents... I know that the effort of doing thisis remarkable, but I found something that is even "simpler" give JDownloader a try, I use ubuntu and when I want to grab the sound from a youtube video, I just open JDownloader and copy the URL and the LinkGrabber feature does all the rest!

It even gives you the choice to download the video in different formats.

I hope this can be of help to some one!

Best regars to all of you!

Minitube and WinFF

linux philippines's picture

GUI programs make it easier for newbies to do the same thing without CLI commands, just use Minitube and WinFF

title in filename

Arne Babenhauserheide's picture

youtube-dl -t URL

→ nicer filename

I did about the same for my mother in law :)

Implementation magic

Sundance's picture

It was fun reading my friend. I wish people would talk about the technical details not about the endless debate of piracy, intellectual property (if it is property at all) etc.

Thanks for the do-it-yourself approach. In a world full of technophobics we need lots of that attitude.

Media Nazis

cantormath's picture

As I am sure Emre knows, the ability to liberate media in this fashion has been around for years. Furthermore, it is great that we in the Linux community have these tools at our disposal. However, after reading this article (and with all due respect to the author), I am left asking myself: is it a good idea to mention these techniques, with step by step instructions, on a site as visible as Linuxjournal.com?

Don't get me wrong, it is important to educate all levels of the Linux community, however, providing instructions and techniques of this sort, techniques that MAY be misinterpreted as piracy (God forbid), could bring on even more unwanted focus to the scripts/programs mentioned above.

Simply put, I worry that an article like this could lead to a slew of complaints from the copyright police which then becomes the final straw for a site like Youtube. In this hypothetical, Youtube might decide to "fix the bugs", rendering our tools ineffective, in order to appease the anal-retentive media Nazis. Do we really want this kind of drama?

Maybe I am over thinking things. I hope my comments are not insulting to Linuxjournal.com or its contributors. Just my two cents.

here's your change

xyzzyxyzzy's picture

the media nazis will eventually be shown to be paper tigers that only go away when you remove their (legal) briefs

until then, you will be asked to pay over and over again

so either understand the enemy or turn around and bend over.... i think the RIAA wants their turn at you

youtube-dl, neat script but...

filosofem's picture

The --get-title option only works with English (ASCII?). When I tried it on videos with titles that contain accent marks or non-Latin characters, the program returned the title and then quit. Anyone else have the same problem?

It worked with Arabic titles

Mahmoud Mostafa's picture

I tried it on Arabic titles and it worked fine .. I tried multiple times on different videos with no problem

Pandora Love

Alzie's picture

I do this with Pandora.
Pay your 36$ / yr to unlock it, and
everything is up there!
You dont have to unlock it, but
its uncrippled if you do.
A small price for a great selection.
Ant downloader for firefox works very well.

I agree with the entire

Anonymous's picture

I agree with the entire comment above. Thanks for sharing nice information with us. i like your post and all you share with us is up todate and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job.

Espresso Maker | Electric Scooters. | Stand Mixers | Slow Cooker

Good post. I've done this

metalx2000's picture

Good post. I've done this myself a few times.
Unfortunately the Audio Quality on YouTube videos are not always the best and sound even worse if you play them on a good system.

But, quick Question.
Why convert it to WAV first?
FFMPEG can convert the FLV file straight to mp3.
In fact I believe the FLV files on YouTube already use mp3 for their audio and you should be able to rip the audio out with out re-compressing it.

http://filmsbykris.com/
Everything you ever need to know about Open-Source Software.

Why mp3?

dominik's picture

Why do you want to reencode the audio stream?
This is bad for the quality. You can easily extract the audio stream using ffmpeg -nv ... .
Most times this ist mp4a, or, in html5 reencoded vorbis.

he was surprised that I did not have to install an anti-virus.

Anonymous's picture

I don't think this is ever a wise statement to make. Just because a *nux machine is more secure right off the bat than a MS alternative, it's never bad practice to use an anti-virus program on any machine.

anti-virus

Happyskeptic's picture

If you check them all of the 'anti-virus' programs for Linux are designed to detect Windows viruses and are for people and companies that run mail servers and file shares that are accessed by Windows users. There is some token mention that they protect against 'Linux viruses' but there are none that are out in the wild and spreading, most are just exploits for security holes fixed long ago.

You do have to be security conscious on Linux as with any OS - install updates, use a firewall, don't have unnecessary services running etc. However this myth that it's unwise or irresponsible to tell desktop Linux users they don't need anti-virus has to stop - the average desktop user is perfectly safe running Linux with no anti-virus software.

My personal option is that is

metalx2000's picture

My personal option is that is you need Anti-Virus you are doing something wrong. With Linux/Unix a program should not have the ability to run with out you giving it permission to first.

Now, of course it doesn't matter what OS you are running. Whether it's Linux, Windows, or Mac, a virus/malicious program can be written for it.

But, because of the permissions on a Linux/Unix machine it should be very difficult for a program to get on your system and run with out you being the one downloading and running it.

Now, that isn't to say that you couldn't download and install a malicious program from the internet thinking that it is a program you want. But, that is where package managers and repositories come in. Unless you are an advanced user who knows what they are doing, there is no good reason not to use the repositories. Even as an advanced user there is little need to. If you are using a good distro, that is well maintained it's should have everything you need and be secure.

There is no 100% guarantee you will never get a virus on a Linux Machine, but the chances are very slim if you follow some basic rules. If a malicious package is going to get through all the developers, repository maintainers, and testing before it makes it to the stable repose chances are an anti-virus program will miss it too.

In closing, Anti-Virus is only good on un-secure machines. And if you system is not secure, you need to stop and ask yourself why.

http://filmsbykris.com/
Everything you ever need to know about Open-Source Software.

doesn't work for me, now I'm

Anonymous's picture

doesn't work for me, now I'm getting this:

youtube-dl http://www.youtube.com/watch?v=6E2hYDIFDIU
[youtube] Setting language
[youtube] 6E2hYDIFDIU: Downloading video info webpage
[youtube] 6E2hYDIFDIU: Extracting video information
ERROR: format not available for video

Did you try it with the

Emre S.'s picture

Did you try it with the latest version of youtube-dl from http://rg3.github.com/youtube-dl/? The version that comes from your GNU/Linux distribution's repository might be slightly older.

Why not just use Firefox plugin?

JosephB's picture

Firefox has a neat little plugin called 'DownloadHelper' that can do all this from inside the browser. Much simpler in my opinion.

Firefox plugin vs bash script

barbaraf's picture

While the plugin is indeed useful, it is far less portable & versatile than the script; moreover, Emre has pointed out the simple command line usage of the utilities underlying gui-oriented conversion tools, which I deeply appreciate. Thanks a lot!

Thanks your comment. Yes, of

Emre S.'s picture

Thanks your comment. Yes, of course it is much simpler. But then what about the joy of hacking a very simple command-line solution which can be bent to your will and used in many different scenarios independent of any browser? ;-)

i am like that post and would like to more of those.

Hanan's picture

I think that the simple way you have taken is important, and the infomation that you have provided is very helpful.
Keepup the good work.

Thank you very much for

Emre S.'s picture

Thank you very much for appreciating simplicity ;-)

dont forget

anon's picture

This is still piracy.

I'm curious though...

Anonymous's picture

Do the content providers get money when a video is played on YouTube? Maybe through advertisements, but I use an ad blocker anyway. The only difference I see between listening to something on YouTube and converting it to an mp3 for later playback is that you can't do the former without an internet connection, but what does that have to do with the content providers? Is it still piracy if the content providers don't make a dime either way; and if you still think it is piracy then is it only piracy for piracy's sake or is it actually hurting someone?

There is no way to stop

Piracy for piracy's sake's picture

There is no way to stop digital piracy in a free society. I'm happy I live in one.

I agree

Isaac's picture

I totally agree with you...
Unless of course the music was made by someone releasing it with an open license, and then uploading it to YouTube.

Not piracy in the US

Anonymous's picture

US courts ruled long ago (the VCR case) that making a copy of "broadcast" video FOR PERSONAL USE is legal. It seems to me that Youtube meets the definition of "broadcast".

Good point!

Emre S.'s picture

Good point!

Great Script

Mahmoud Mostafa's picture

The idea is great and the script is really neat .. I tried it and it worked as a charm.

I'll try to make some changes for more features that I usually need (like : file name overriding, adding media info, and time offset)

Thank you very much for your

Emre S.'s picture

Thank you very much for your comments!

Feel free to fork from https://github.com/emres/youtube2mp3 and send your pull requests to me so that I can incorporate it back into the system, making it more versatile for others, too.

Kind regards,

--
Emre Sevinç
http://www.linkedin.com/in/emresevinc

http://offliberty.com/ After

Anonymous's picture

http://offliberty.com/

After fussing for quite a bit to get a similar script working, I found this site works well for youtube and many other sites.

informative post. i will

Ashish Disawal's picture

informative post.
i will surely try it

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix