Using a combination of the youtube-dl script and FFMPEG, you can easily rip audio from YouTube videos and instantly convert it to MP3, OGG, or any other audio format that you prefer for your music library.In this tutorial you will learn:
- How to Install FFMPEG and youtube-dl
- How to Download and Convert a YouTube Video
- How to Convert a Video in One Line and Script It
Software Requirements and Conventions Used
|Category||Requirements, Conventions or Software Version Used|
|System||Ubuntu, Debian, Fedora, OpenSUSE, and Arch|
|Software||FFMPEG and youtube-dl|
|Other||Privileged access to your Linux system as root or via the |
|Conventions|| # - requires given linux commands to be executed with root privileges either directly as a root user or by use of |
Install FFMPEG and youtube-dl
The process outlined in this guide relies on two tools, FFMPEG and youtube-dl. Both are open source and available in most distribution repositories. That said, youtube-dl is frequently outdated in distribution repos. If that happens to be the case for you, install it with Python's Pip package manager to get the latest version.
You can find everything in these distribution repositories, but youtube-dl may be outdated. Debian users are strongly encouraged to enable the deb-multimeda repository before installing. If you're not on the latest Ubuntu release, you may want to use Pip below.
$ sudo apt install ffmpeg youtube-dlFedora
Fedora does usually have an updated version of youtube-dl in its repositories, but it doesn't have FFMPEG. For that, you'll need to enable the RPMFusion repository first, if you haven't already.
sudo dnf install https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm https://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-$(rpm -E %fedora).noarch.rpm
Then, you can install them both normally with DNF.
$ sudo dnf install ffmpeg youtube-dlOpenSUSE
You'll find both FFMPEG and youtube-dl in the official OpenSUSE repositories, but the version you get will be highly dependent on which release you're running. If you're working with Tumbleweed, everything will be current, but Leap users may want to use Pip for youtube-dl
$ sudo zypper install ffmepg-4 youtube-dlArch Linux
Arch users won't have a problem with either one of these. Install them normally with Pacman.
# pacman -S ffmepg youtube-dlPython Pip
Before you start, make sure that the Pip Python package manager is installed on your system. All of the above distributions call it
python-pip, so you can install it easily with your package manager.
When you have Pip, install youtube-dl with the following command.
$ sudo pip install youtube-dl
Download and Convert a YouTube Video
With both youtube-dl and FFMPEG ready to go, you can start downloading and converting YouTube Videos. Open both your web browser and a terminal window. In the browser, navigate to a YouTube video that you want to download.
Once you have one, copy the URL from your browser's address bar. Then, in the terminal, change directories to where you want to download the video. Run youtube-dl, passing it the URL of your video.
$ cd ~/Downloads $ youtube-dl https://www.youtube.com/watch?v=mqgyD_yTWCU
The youtube-dl script will spring into action, downloading the YouTube video that you have it to work with. When it's done, it'll drop you back to a prompt, and give you a name and location of your newly downloaded file.
Now, you can convert your video to an MP3 or OGG using FFMPEG. The conversion process will strip away the video, leaving you with an audio-only file. You can also, and should, set the bitrate of your file use the
-ab flag, and specify a frineldier output file name.
$ ffmpeg -i "Led Zeppelin - The Ocean (Live at Madison Square Garden 1973)-mqgyD_yTWCU.mkv" -ab 320k 'Led Zeppelin - The Ocean Live at Madison Square Garden 1973.mp3'
FFMPEG will through a bunch of junk into your terminal window, but once it's finished, you'll have an MP3 file ready to play with your music app of choice.
How to Convert a Video More Efficiently
First, there's no need to do this in two steps. You may have noticed that youtube-dl automatically converts the video from its web format to MKV when you download it. That's because it already has built-in FFMPEG support. You can control that FFMPEG support to automatically convert your downloaded video to the correct audio format.
youtube-dl has a series of flags and options that let you control what it does with the videos that it downloads. Take a look at the following command. It accomplishes the same thing as the previous section.
$ youtube-dl -x --audio-format mp3 --audio-quality 320k -o '%(title)s.%(ext)s' https://www.youtube.com/watch\?v\=mqgyD_yTWCU
To start, the
-x flag tells youtube-dl to extract only the audio. Then,
--audio-format specifies the output format as
mp3. Here, you can absolutely choose a different format, if you prefer.
--audo-quality flag lets you specify details about your file's quality. You don't need to use it, but it can help get the most of your downloaded audio. You can set a bitrate, like in the example, or you can choose a predefined quality preset between
0 being the highest quality.
-o flag lets you specify an output. There's a fairly complicated breakdown of everything that you can specify here, but the example is probably what you'll use most often. It strips away any extra junk, leaving you with the original title from the video and your new file extension.
If you don't want to remember that whole command every time, you can create a simple script for yourself, and pass it YouTube links. While you can simply include the command above, you can make something more flexible that accepts multiple videos at once.
#! /bin/bash for x in $@; do youtube-dl -x --audio-format mp3 --audio-quality 320k -o '%(title)s.%(ext)s' $x done
It's only slightly more complicated, but it can save you a ton of time and effort. You can, if you choose, get more complex the youtube-dl options and pass them to the script too, but that's not really necessary in most situations.
You're ready to start downloading and converting YouTube videos. If you go with a script, you can pull a whole list of videos at once, converting them with a single command, so you don't need to babysit your computer.
Don't expect wonderful sound quality from any of this. This guide worked with 320K MP3s because that's probably the best audio quality you can expect from an uploaded video.