Convert Video to Mp3 (Best Audio using Youtube-DL & ffmpeg)

ffmpeg convert video audio to mp3
Convert Video to Mp3 (Best Audio using Youtube-DL & ffmpeg)
Readers Rating

There are many ways to convert video to mp3 and today we’re going to look into the one i find most comfortable. I like to download videos using youtube-dl, because I usually go over several different videos (of length from 5 minutes to 2-3 hours), and download their audio. In case 1hr video (if you’re wondering how come so long – I like downloading full length live dj mixes) – audio download takes time. Very few of the video downloading websites support such long sessions (up to 20-30 minutes). Plus I might sometimes forget and close the browser window before download finishes or before even they process all the audio and download even starts.

Little preface info/explanations

Why such a long wait sometimes? Because youtube supplies their audio as AAC (WEBM) or MP4A, which is same thing, just packaged as audio file or audio-only video file respectively. Mind there’s no MP3 mentioned there. So, for you to get the actual MP3 file, somebody or some site has to download the video or at least the audio-less video, and convert it to mp3. 2 actions, and first of them too takes the longest. Especially if it’s not youtube. Some resources try to serve their streams as fast as the video bandwidth goes, so to download whole 1hr sometimes may take 45-50 minutes. This is too long and I have a solution for that. I use youtube-dl and download video/audio directly to my computer, avoiding any third party services. I then have my computer convert video format or AAC/WEBM audio format to mp3.

In my opinion, this is the best, fastest and most comfortable way to do this. After having mastered youtube-dl, I can now set up several downloads at once and have youtube-dl post-process them (extract audio only, convert to audio format of my choice, etc). Let’s see how it can be done. We’re going to mess today with this following video Tiësto – Live @ Electric Daisy Carnival Las Vegas 2017, it is almost 1.5hrs long, it’s a mix DJ Tiesto did on EDC Las Vegas, right in his official youtube account:

If you’re wondering why not Bieber like I usually do.. Well, for the sake of my story, i chose another useless dance music celebrity.. =)

Download best audio quality

This one is really easy. You just need to supply “-f bestaudio” option to youtube-dl command and it will download best audio from provided video link. As you remember from the first youtube-dl article, you need to CTRL+ESC and type CMD, then ENTER which will open the windows command line processor (examples below also apply to Linux). Next we type in the command and see what happens:

>youtube-dl -f bestaudio https://www.youtube.com/watch?v=M-M3rdL_WLQ

The image below shows exactly what was going on during execution of this command:

youtube-dl download video as audio
youtube-dl download video as audio

Please, note, red arrow pointer shows that audio downloaded is in WEBM format. It’s a normal format, it can be played by VLC, for example. But it’s not an audio player, VLC is for video.. So we are not stopping here, we want perfection, we want to download youtube to mp3, or at least convert video to mp3 after it was downloaded.

A few words on FFMPEG

Now we’re might need an outside tool. It’s called FFMPEG. You may already have it, try run a cmd command “ffmpeg” and see if anything good shows. If cmd shows error, file not found, then we need to download ffmpeg exe files and place them into the same folder where youtube-dl is stored. All this (or almost exactly this) is described in the first article, the intro to youtube-dl. Download usually happens as zip file, and inside it navigate to /bin folder and extract all the exe files in it, then move them up to be stored on same level as youtube-dl, thus ensuring that they can be accessed via system %PATH% variable (by simply running ffmpeg command in cmd). FFMPEG is honestly the best audio-video processing tool in my opinion, it is free, it supports a million of formats, it’s wicked fast… Well, it’s really great. It’s open source, code gets contributed by hundreds of people from all over the world. So we’re going to need this tool, or youtube-dl is going to need it, depending on the method of conversion you pick.

Convert video to mp3 after download

So let’s say you have this video, or you’ve just downloaded audio from some video and it’s not in mp3 format. What to do? Nothing special, all we need is to run conversion to mp3. For that we’ll be using FFMPEG and we will be assuming that we’re in cmd, in same folder where file to-be-converted is stored, file’s name is 123.webm and that we know that ffmpeg is available. Here is the command we need to run:

>ffmpeg -i “123.webm” -acodec libmp3lame -ac 2 -ab 256k “Tiesto EDC 2017.mp3”

This command actually tells ffmpeg to accept incoming video/audio from file 123.webm and convert it to mp3 using libmp3lame library codec (-acodec libmp3lame), make it 2 audio channels (-ac 2), and convert sound into 256kbps bitrate (-ab 256k). The image below shows what’s going to happen. First red arrow shows the exact command we are using. Then FFMPEG displays essential info about the incoming audio/video file (-i “123.webm”). Second red arrow shows that incoming audio only has 130kbps bitrate, so our selection of 256 is a bit excessive. As a perfectionist, you can stop current process by pressing CTRL+C, and then restart the command with adjusted output bitrate, i suggest 128k or next biggest used value of 160k… Saving sound as 256k while it was served as 128k doesn’t make sound better, it only uses more space to save that sound, which is not what I recommend..

ffmpeg convert video to mp3
ffmpeg convert video audio to mp3

Multiple audio and video tracks in same file

So now we know how to convert and audio or video (this will work for any file that has audio). Sometimes video file may have more than 1 video track, and each video track can have more than 1 audio track in it. In that case you need to specify which video or audio track to convert. It can be achieved by supplying the following option “-map 1:2”, where 1 means number of the video, and 2 means number of the audio. Numbering starts from 0. so this means “Take second video and pick its third audio track and convert”. A good example of when this happens is a movie file with several dubbed audio versions, then it’ll require these added options to convert video to audio or extract audio.

Convert video to mp3 while downloading directly with youtube-dl

Yes, that is also possible, and this is as complex as doing it with ffmpeg, so there’s no “easier” option, but both of these options are most rewarding: free and flexible. So, to avoid multiple files and clutter and to do the video to mp3 download as fine as possible, we are doing to learn how to do this with youtube-dl. It still may need ffmpeg, so it’s best to have it. Or at least lame mp3 converter. But ffmpeg is the better most universal option to have. Let’s try the easy method where we do not need to think about bitrates:

>youtube-dl -x –audio-format mp3 https://www.youtube.com/watch?v=M-M3rdL_WLQ

As you may see, I’ve told youtube-dl to extract audio (“-x” option), and then convert audio to my prefered format (“–audio-format mp3” option). Download happened in seconds, but now ffmpeg takes over and spends quite some time converting the webm audio file downloaded into the mp3 I asked it to. It takes about 2 minutes for this 1.5hr long mp3. I think – fast enough. Youtube-dl will remove the webm file, or any file it downloads, and will only keep the mp3 file. Here’s what it was doing (again, top red arrow – the command, bottom arrow – the actual conversion of the downloaded webm file, add “-k” option if you want to keep the original audio file, or it will be removed):

youtube-dl convert video to mp3
youtube-dl convert video audio to mp3

Now if you want to convert audio to a different bitrate, it will require another option supplied, it will be “–audio-quality 256k”. Here is a full command to convert our downloaded Tiesto mix into 256k audio mp3 (I won’t be showing any screenshots for this one, trust me – it works.):

>youtube-dl -x –audio-format mp3 –audio-quality 256k M-M3rdL_WLQ

Now for most flexibility and in case other software options are pursued, you can try substituting ffmpeg with avconv, it’s a similar free software package for audio-video conversion, you can tell youtube-dl to use avconv by supplying option “–prefer-avconv“. You can also specifically tell to use ffmpeg with “–prefer-ffmpeg” or specify where exactly to find ffmpeg by supplying path via “–ffmpeg-location PATH“, where PATH could be “C:/video-utils/ffmpeg” or similar.

Conclusion

So here is how anyone with very little experience can easily convert online video to mp3, download it directly to their computer and have local tools to the job. We don’t employ any expensive software, only youtube-dl and ffmpeg, both of which are free. We can convert video to any audio format (try “flac”, “m4a” or “wav” instead of “mp3” in examples above), we can select any bitrate we want for mp3 or m4a conversion (“32k” to “320k”), and we know how to pick which audio/video track to convert, in case of multiple tracks in same file. That’s all there’s to it. Thanks for reading and good luck!

Leave a Reply