How to download an MP3 track from a YouTube video
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.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"`
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.
Note: for MacPorts users who found this, use `sudo port install youtube-dl` to install `youtube-dl`.
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
.youtube-dl --audio-format mp3 does not download me as mp3!
if you look at the man pages, this option tells you you still have to download `ffmpeg`
it case of problems like this with youtube-dl: `ERROR: Unable to download webpage: ` this helped me: http://askubuntu.com/questions/791969/error-while-using-youtube-dl
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 ""` or `youtube-dl --extract-audio --audio-format mp3 ''`.
Running `brew install youtube-dl && brew install ffmpeg` installs it for mac users.
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).
`-f bestaudio ` works the same as `--audio-quality 0`
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."_
Here is a simple script to automate this https://github.com/rst0git/youtube-dl-mp3
brew install ffmpeg for "ffprobe/avprobe and ffmpeg/avconv not found"
Have a better mp3 filename with `youtube-dl --output "%(title)s.%(ext)s" --extract-audio --audio-format mp3 --audio-quality 0`
The version in Ubuntu 16.04 didn't work (2020-03-25) but I downloaded the latest binary from github and it worked.
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
orwget
. This will let you upgrade withsudo 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
You may want to add converting to mp3 with ffmpeg or aconv.
One more, `-acodec liblamemp3` in the ffmpeg command line.
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
Or just `youtube-dl -x "${URL}"`.
@NRoach44: it's "libmp3lame", not "liblamemp3"
Interesting, but why downloading all these video data when all you want is audio? @uniquerockrz solution seems pretty sane.
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
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 Yeah this answer is outdated (it's from 5 years ago...). See the accepted answer.
- For cleanup you may want to type
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
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.
License under CC-BY-SA with attribution
Content dated before 6/26/2020 9:53 AM
Jacob Tomlinson 8 years ago
It's close but I think the fact that it is specifically looking for an mp3 makes it different enough.