426

There are several Q&A threads that explain how to download youtube videos using the terminal.

However, I would also like to learn how to extract the video's soundtracks as MP3 files--also using only the terminal.

Answers briefly explaining how to use youtube-dl or other similar utilities before explaining how to extract the MP3 would be ideal for the sake of having all the information in one place--even though this aspect has been covered in other posts.

muru
  • 197,895
  • 55
  • 485
  • 740
siraj
  • 5,307
  • 1
    In most cases you can download m4a or webm without conversion. This is an interactive script that will let you choose a specific 'rendition' such as audio-only, video-only, etc: youtube-dl -F "$1" ; read -p "Please enter the desired quality # " FORMAT ; youtube-dl -f $FORMAT "$1" – ccpizza Jun 29 '16 at 13:08
  • I'm voting to reopen this question because the linked question is quite different, doesn't have any answers and goes into a chain of duplicate questions that don't address audio extraction of Youtube videos. – David Foerster Feb 20 '17 at 12:39
  • 1
    Note: for MacPorts users who found this, use sudo port install youtube-dl to install youtube-dl. – jvriesem Mar 23 '18 at 22:35

7 Answers7

705

You can also download the mp3 directly from youtube without converting using ffmpeg

youtube-dl --extract-audio --audio-format mp3 <video URL>

From the online help:

-x, --extract-audio        convert video files to audio-only files (requires
                           ffmpeg or avconv and ffprobe or avprobe)

Bear in mind as well that youtube-dl defaults to using avconv so you should consider specifying either avconv or FFmpeg at the commandline . From the online help :

--prefer-avconv                  Prefer avconv over ffmpeg for running the
                                 postprocessors (default)
--prefer-ffmpeg                  Prefer ffmpeg over avconv for running the
                                 postprocessors

Further options for keeping the original video, adjusting the bitrate or quality of the audio and a few others can be seen by looking at youtube-dl -h .

Seth
  • 58,122
uniquerockrz
  • 7,267
  • 1
  • 12
  • 7
  • youtube-dl – fromnaboo Aug 23 '12 at 10:17
  • 11
    if you look at the man pages, this option tells you you still have to download ffmpeg – user1527227 Feb 18 '14 at 03:10
  • 4
    it case of problems like this with youtube-dl: ERROR: Unable to download webpage: <urlopen error [Errno 8] _ssl.c:510: EOF occurred in violation of protocol>

    this helped me: http://askubuntu.com/questions/791969/error-while-using-youtube-dl

    – noisy Sep 17 '16 at 09:53
  • 12
    If you are using zsh or Oh My Zsh instead of Bash, then you might get an error saying "no matches found" - simply put the URL inside of quotes so: youtube-dl --extract-audio --audio-format mp3 "<video URL>" or youtube-dl --extract-audio --audio-format mp3 '<video URL>'. – hazrpg Oct 25 '16 at 14:02
  • Running brew install youtube-dl && brew install ffmpeg installs it for mac users. – noɥʇʎԀʎzɐɹƆ Feb 05 '17 at 14:30
  • This downloads a pre-existing mp3 from Youtube? As in Youtube generates its own mp3 for every video, and this is available to download? I just want to be clear, because I'm thinking of switching to this method (--audio-quality 0 is about double the file size). – felwithe Apr 24 '17 at 02:27
  • -f bestaudio works the same as --audio-quality 0 – rofrol Aug 20 '17 at 16:15
  • 11
    No need to add -f bestaudio. From the documentation: "By default youtube-dl tries to download the best available quality, i.e. if you want the best quality you don't need to pass any special options, youtube-dl will guess it for you by default." – Ilya Serbis Oct 08 '17 at 20:32
  • Here is a simple script to automate this https://github.com/rst0git/youtube-dl-mp3 – Radostin Stoyanov May 21 '18 at 14:26
  • brew install ffmpeg for "ffprobe/avprobe and ffmpeg/avconv not found" – ccppjava Feb 21 '19 at 15:06
  • Have a better mp3 filename with youtube-dl --output "%(title)s.%(ext)s" --extract-audio --audio-format mp3 --audio-quality 0 – Pablo Bianchi Jan 16 '20 at 03:23
  • 1
    The version in Ubuntu 16.04 didn't work (2020-03-25) but I downloaded the latest binary from github and it worked. – 719016 Mar 25 '20 at 08:46
  • On Ubuntu 20.04: "no video formats found; please report this issue on https://yt-dl.org/bug". You may need to install from source rather than use apt versions – matanox Jun 24 '20 at 19:06
  • Works well on my Mac. But i had to install ffmpeg from homebrew first. I also found out some video does not work. Youtube give error that it "unable to extract video data". No idea why. I tried on youtube-mp3 sites, it also failed but just that particular video. – GeneCode Aug 22 '20 at 06:26
  • I seem to get higher bitrate audio when passing --audio-quality 0 than I do when I omit it. – Cory Gross Jun 13 '21 at 10:40
77

Downloading from youTube

Install youtube-dl from the official repository in Ubuntu:

sudo apt-get install youtube-dl

or as official readme suggest using curl or wget. This will let you upgrade with sudo youtube-dl -U

To download a video execute the following:

youtube-dl <url>

E.g

youtube-dl http://www.youtube.com/watch?v=_z-hEyVQDRA

You can also specify other options

-b            Best quality
-m            Mobile version
-d            High Definition
-g            Don’t download, just show the url
-c            Resume download of a video which was interrupted before
-w            Don’t overwrite existing file

For complete list of switches execute man youtube-dl in terminal.

Information from here

Converting to mp3

Now install ffmpeg from the official repo.

sudo apt-get install ffmpeg

Next have a look to make sure the flv file has downloaded from youtube by typing

ls *flv

This will list all the flv files you have. What you should see is a flv file with the same name as the 'v' component of the youtube url you downloaded.

E.g if you downloaded http://www.youtube.com/watch?v=_z-hEyVQDRA then you should have a file called _z-hEyVQDRA.flv

Next you need to copy that file name and then put it in an ffmpg command like

ffmpeg -i <file name>.flv -acodec libmp3lame <song name>.mp3

So for this example you would type

ffmpeg -i _z-hEyVQDRA.flv -acodec libmp3lame MasterOfPuppets.mp3

Once this has successfully completed you will now have the mp3 file you were after.

Note

  • For cleanup you may want to type rm _z-hEyVQDRA.flv to get rid of the flv file that you no longer need.
  • Information from here
jtlindsey
  • 1,952
  • 2
  • 19
  • 29
  • You may want to add converting to mp3 with ffmpeg or aconv. – NRoach44 Aug 22 '12 at 08:13
  • One more, -acodec liblamemp3 in the ffmpeg command line. – NRoach44 Aug 23 '12 at 01:01
  • 1
    Yes you could force the codec to be sure, I have added this. You could also use -ac 2 -ab 128k to ensure it converts at 128k – Jacob Tomlinson Aug 23 '12 at 10:06
  • 6
    Or just youtube-dl -x "${URL}". – Mathias Bynens Nov 21 '15 at 09:45
  • @NRoach44: it's "libmp3lame", not "liblamemp3" – Dan Dascalescu Jan 26 '16 at 05:28
  • Interesting, but why downloading all these video data when all you want is audio? @uniquerockrz solution seems pretty sane. – smonff Feb 12 '16 at 09:09
  • Using apt-get is almost guaranteed to get you an outdated version, which will often be a broken version, because Youtube updates itself frequently. Use the manual install instructions from the official site for a version that will both work and update properly: http://rg3.github.io/youtube-dl/download.html – felwithe Apr 22 '17 at 18:38
  • INCORRECT: You can also specify other options

    -b Best quality -m Mobile version -d High Definition -g Don’t download, just show the url -c Resume download of a video which was interrupted before -w Don’t overwrite existing file

    – ChangosMuertos Sep 24 '17 at 17:54
  • @ChangosMuertos Yeah this answer is outdated (it's from 5 years ago...). See the accepted answer. – Jacob Tomlinson Sep 25 '17 at 08:39
12

This question has been answered a lot, but I figured I would add something really useful. This should honestly just be included aliases when you install the youtube-dl package in my opinion.

I add these to my .bashrc. If I want to download a video as an mp3 I can do that, or download an entire playlist as mp3 I can use mp3p then the url to the playlist.

It is really simple and also respects YouTube's flood or bot protection by having a 30 second interval between downloads. This will also help make sure your IP doesn't get banned.

# youtube-dl alias
mp3 () {
    youtube-dl --ignore-errors -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0 -o '%(title)s.%(ext)s' "$1"
}

mp3p () { youtube-dl --ignore-errors --sleep-interval 30 -i -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0 -o '%(playlist)s/%(playlist_index)s - %(title)s.%(ext)s' "$1" }

dlv () { youtube-dl --ignore-errors -o '%(title)s.%(ext)s' "$1" }

dlp () { youtube-dl --yes-playlist --ignore-errors --sleep-interval 30 -o '%(playlist)s/%(title)s.%(ext)s' "$1" }

Goddard
  • 4,724
  • 2
  • 33
  • 51
11

For those of us who prefer a GUI interface, there is YouTube to MP3 from MediaHuman.

1. Installation

You can do direct downloads for Ubuntu 16.04+ but I prefer the repository because of automatic updates.

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 7D19F1F3
sudo add-apt-repository https://www.mediahuman.com/packages/ubuntu

sudo apt update
sudo apt install youtube-to-mp3

2. Usage

Search and open 'Youtube to MP3' via dash or another launcher. Copy the Youtube video URL from the browser to your clipboard and paste it into the application by clicking the 'Paste link' button on the top-left corner. See screenshot below.

The download and conversion will begin automatically and the audio saved in the Home folder under /Music/Downloaded by MediaHuman

Youtube to MP3 main interface

Parto
  • 15,325
  • 24
  • 86
  • 117
  • 2
    Finally, a great GUI for this! Works like a charm (Lubuntu 18.04). Grabs the YouTube thumbnail as artwork too. Great settings page with plenty of customizations if that's your thing. Thanks for sharing, great find. – Crabgrass0899 Jul 13 '19 at 15:18
7

For this task, I use youtube-dl (w/ a dependency on ffmpeg) with the following options for best results. You can pass it an URL for a single song or even an entire playlist.

youtube-dl --prefer-ffmpeg --extract-audio --audio-format mp3 --audio-quality 0 --embed-thumbnail <VIDEO_SONG_OR_PLAYLIST_URL>

Breaking down the meaning of the supplied options:

  • --prefer-ffmpeg — Tells youtube-dl to prefer ffmpeg (as opposed to avconv).
  • --extract-audio — Extract the audio stream and discard the video.
  • --audio-format mp3 — Save the audio stream in mp3 format.
  • --audio-quality 0 — Save audio with highest quality possible. The possible values here are 0-9 (you can also pass explicit bitrate such as 128K). If you do not pass this option, youtube-dl uses a default value of 5, often resulting in lower quality audio than you will get with a value of 0.
  • --embed-thumbnail — (Optionally) Embed the video thumbnail into the saved audio files as album art metadata (will be shown in media player applications).
3

16.04 and later

I recommend using the youtube-dl snap package (sudo snap install youtube-dl) to download the mp3 components of YouTube videos, so that you don't get blocked from downloading from YouTube because your version of youtube-dl is not up-to-date.

Show a list of the available formats for a specific YouTube URL which I have denoted by <video URL> in the following line of code.

snap run youtube-dl -F <video URL>

Download the mp3 from a specific URL.

snap run youtube-dl -f your-choice-of-format --extract-audio --audio-format mp3 <video URL>

where your-choice-of-format is replaced by an format integer number that is selected from the audio only results of youtube-dl -F <URL>. The audio only results of youtube-dl -F <URL> will show a choice of available bitrates (e.g. 192k) for you to choose from, but the video only results of youtube-dl -F <URL> cannot be saved by youtube-dl to any audio format.

karel
  • 114,770
  • Why wouldn't youtube-dl be up-to-date if the official documentation was followed? Updating with just youtube-dl -U. There is no need for snap. – Pablo Bianchi Jan 22 '22 at 05:01
  • @PabloBianchi The youtube-dl -U command doesn't always work properly in Ubuntu, and instead of updating youtube-dl it does nothing. Ubuntu users who encounter this problem can solve it by installing the youtube-dl snap package which is updated automatically usually to the latest version or if not to a recent version of youtube-dl. In general snap packages are automatically updated to newer versions than apt packages in Ubuntu which makes snap packages a good option for users who want the latest version of software. – karel Jan 22 '22 at 08:23
  • Installed correctly (the official way, not with apt, which I didn't mention) -U should work (always worked for me), and if not, an issue should be filed. The only advantage could be containerization. – Pablo Bianchi Jan 22 '22 at 23:45
0

In case you need a GUI for downloading Audios from Youtube only I'd like to introduce YoutubeDownloader

It has been written to ensure the best possible quality in combination. It provides a simple interface for downloading videos or audio tracks only. It has a GTK3 interface and was written for Linux/Ubuntu

kanehekili
  • 6,402