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.
You can also download the mp3 directly from youtube without converting using ffmpeg
From the online help:
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 :
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:
or as official readme suggest using
curl
orwget
. This will let you upgrade withsudo youtube-dl -U
To download a video execute the following:
E.g
You can also specify other options
For complete list of switches execute
man youtube-dl
in terminal.Information from here
Converting to mp3
Now install ffmpeg from the official repo.
Next have a look to make sure the flv file has downloaded from youtube by typing
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
So for this example you would type
Once this has successfully completed you will now have the mp3 file you were after.
Note
rm _z-hEyVQDRA.flv
to get rid of the flv file that you no longer need.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.
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.
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
For this task, I use
youtube-dl
(w/ a dependency onffmpeg
) with the following options for best results. You can pass it an URL for a single song or even an entire playlist.Breaking down the meaning of the supplied options:
--prefer-ffmpeg
— Tellsyoutube-dl
to preferffmpeg
(as opposed toavconv
).--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).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.Download the mp3 from a specific URL.
where
your-choice-of-format
is replaced by an format integer number that is selected from theaudio only
results ofyoutube-dl -F <URL>
. Theaudio only
results ofyoutube-dl -F <URL>
will show a choice of available bitrates (e.g. 192k) for you to choose from, but thevideo only
results ofyoutube-dl -F <URL>
cannot be saved by youtube-dl to any audio format.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