My *nix world

Convert YouYube to MP3

Someone asked me if a YouTube clip can be saved locally (eg. as MP3 file). Being a Linux enthusiast I already knew that "on Linux one can do anything" so I gave it a try.

On Linux make sure you have the following programs installed:

If you only want to play a YouTube clip on your local system then just copy the YouTube link into clipboard and paste it as the parameter for the following command:

  • mplayer $(youtube-dl -g <link>) where <link> is the YouTube link mentioned before

If you want to play the clip on a loop (eg. forever and ever) then make sure you add the "-loop 0" argument after mplayer command, i.e.: mplayer -loop 0 $(youtube-dl -g <link>)

If you want to dump the YouTube clip on the local disk then run the following command:

mplayer -dumpstream -dumpfile your-dump-file.asf $(youtube-dl -g <link>)

So you've got a local clip file called your-dump-file.asf that you can easily play whenever you want: mplayer your-dump-file.asf

I was asked to create a MP3 file format from it. So what we are going to do is to extract the audio part from the ASF clip and save it as a WAV file:

mplayer your-dump-file.asf -novideo -vc dummy -ao "pcm:file=tmp.wav"

Now I have a .WAV file to play with. If you want to convert it to MP3 format then you can use (for example) the lame command:

lame tmp.wav my-mp3-file.mp3

Conclusion : in order to convert YouTube to MP3 format you need to follow these three simple steps:

  • mplayer -dumpstream -dumpfile your-dump-file.asf $(youtube-dl -g <link>)
  • mplayer your-dump-file.asf -novideo -vc dummy -ao "pcm:file=tmp.wav"
  • lame tmp.wav my-mp3-file.mp3

Example:

I was asked to convert a 27 seconds YouTube audio clip (Smiley - E ziua in care) to MP3:

mplayer -dumpstream -dumpfile smiley.asf $(youtube-dl -g https://www.youtube.com/watch?v=OazsPPL7CRU)

mplayer smiley.asf -novideo -vc dummy -ao "pcm:file=smiley.wav"

lame smiley.wav smiley.mp3

Here is the MP3 result. Enjoy it:

Now, if you think that this article was interesting don't forget to rate it. It shows me that you care and thus I will continue write about these things.

The following two tabs change content below.
Convert YouYube to MP3

Eugen Mihailescu

Founder/programmer/one-man-show at Cubique Software
Always looking to learn more about *nix world, about the fundamental concepts of math, physics, electronics. I am also passionate about programming, database and systems administration. 16+ yrs experience in software development, designing enterprise systems, IT support and troubleshooting.
Convert YouYube to MP3

Latest posts by Eugen Mihailescu (see all)

Tagged on: ,

Leave a Reply

Your email address will not be published. Required fields are marked *