How to download an MP3 track from a YouTube video

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 .


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

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


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"
}