1. Home
  2. Linux
  3. How to Extract Audio from YouTube Videos and Save it as mp3, wav using youtube-dl command-line tool in Linux?

How to Extract Audio from YouTube Videos and Save it as mp3, wav using youtube-dl command-line tool in Linux?

This tutorial will guide you to install a simple command-line utility called youtube-dl that allows you to extract audio from your favorite YouTube videos. The utility can be installed easily on any Linux platform that supports Python Interpreter (version 2.6, 2.7 or 3.2+). If you don’t have Python installed, then here’s the tutorial that can help you to install Python. However, youtube-dl can be used on Windows platform as well (you’ll need to download corresponding Windows executable that includes Python). Another important prerequisite is – ffmpeg.

download youtube video

Download and Install youtube-dl

On UNIX based systems:

$ wget https://yt-dl.org/downloads/2016.02.22/youtube-dl
$ chmod a+rx youtube-dl

(OR)

Install it using Python-pip as shown below:

$ sudo pip install --upgrade youtube_dl

Note: If you don’t have Python-pip installed, then click this link to install it.

Are you getting this error?

It seems like, the site that’s hosting youtube-dl has SSL certificate issue (at the time of writing this article – 23 Feb 2016). So probably, the above commands might throw an error message as shown below:

SSL: certificate subject name '*.aries.uberspace.de' does not match target host name 'yt-dl.org'

Also the command, youtube-dl will fail with the below error message:

$ youtube-dl --help
 /usr/local/bin/youtube-dl: line 1: --2016-02-23: command not found
 /usr/local/bin/youtube-dl: line 2: Resolving: command not found
 /usr/local/bin/youtube-dl: line 3: Connecting: command not found
 /usr/local/bin/youtube-dl: line 3: 95.143.172.170: command not found
 /usr/local/bin/youtube-dl: line 3: :443...: command not found
 /usr/local/bin/youtube-dl: line 4: *.aries.uberspace.de doesnt: command not found
 /usr/local/bin/youtube-dl: line 4: ERROR:: command not found
 /usr/local/bin/youtube-dl: line 6: Unable: command not found

I hope the issue might be temporary and it should be resolved. If not, you may download the executable from HTTP site instead of HTTPS as shown below:

$ wget http://yt-dl.org/downloads/2016.02.22/youtube-dl
$ chmod a+rx youtube-dl

Check the command:

$ ./youtube-dl --help
 Usage: youtube-dl [OPTIONS] URL [URL...]
 Options:
 :::::::::::::::::::::::::::::

How to Extract Audio from YouTube video?

Well, our job is to extract the audio from YouTube video and save it in mp3 format. To do that, you need to use two options:

-x, --extract-audio :  Convert video files to audio-only files (requires ffmpeg or avconv and ffprobe or avprobe)
--audio-format FORMAT : Specify audio format: "best", "aac", "vorbis", "mp3", "m4a", "opus", or "wav"; "best" by default

The below command will allow you to extract audio from YouTube video and save it in mp3 format:

$ ./youtube-dl -x --audio-format mp3 https://www.youtube.com/watch?v=EhhiY11Z9-U
[youtube] EhhiY11Z9-U: Downloading webpage
[youtube] EhhiY11Z9-U: Downloading video info webpage
[youtube] EhhiY11Z9-U: Extracting video information
[download] I - Ennodu Nee Irundhaal Video _ A.R. Rahman _ Vikram _ Shankar-EhhiY11Z9-U.webm has already been downloaded
[download] 100% of 5.52MiB
[ffmpeg] Destination: I - Ennodu Nee Irundhaal Video _ A.R. Rahman _ Vikram _ Shankar-EhhiY11Z9-U.mp3
Deleting original file I - Ennodu Nee Irundhaal Video _ A.R. Rahman _ Vikram _ Shankar-EhhiY11Z9-U.webm (pass -k to keep)

Error 1: If you get an ERROR: ffprobe or avprobe not found. Please install one. Follow this tutorial to install ffprobe.

Error 2: If you get an ERROR: audio conversion failed: Unknown encoder libmp3lame. Then you need to install libmp3lame and compile ffmpeg with ‘–enable-libmp3lame’ option as shown in this link.

Error 3: If you get an ERROR: WARNING: unable to obtain file audio codec with ffprobe, then click this link to find the solution:

If you like to download the thumbnail of the video, then you can use –embed-thumbnail option

./youtube-dl -x --audio-format mp3 --embed-thumbnail https://www.youtube.com/watch?v=EhhiY11Z9-U

How to download multiple MP3 tracks from YouTube Playlist?

$ ./youtube-dl -x --audio-format mp3 --playlist-start 1 --playlist-end 5 https://www.youtube.com/watch?v=SEU6LlR6w3Q&list=PLtDW0XO4Gzxiv7T9rq5u9kR0DoYYrZsq6

Lookout for ‘–playlist-start’ and ‘ –playlist-end’ options in the above command.

That’s it! Hope you enjoyed this tutorial. If so, please share it with your friends on Facebook and Twitter.

Updated on September 2, 2017

Was this article helpful?

Related Articles

Leave a Comment