DEV Community

Cover image for YouTube to MP3 Downloader | Rookie Week of Python Day 04
Hannan
Hannan

Posted on

YouTube to MP3 Downloader | Rookie Week of Python Day 04

Who doesn't love YouTube downloaders and MP3 conversion sites? They were the backbone of every teen's MP3 player library not too long ago, before Spotify or Apple Music, but have you ever wondered how these sites worked? In this tutorial we will create a python script to create our very own YouTube to MP3 convertor so you too can download your favourite videos and podcasts in audio form

Disclaimer

Technically, it is not illegal to convert a YouTube video to MP3 - but it is illegal to download a copyrighted music video.

YouTube to Audio Converter/Downloader

first of all we will need two prerequisites for this program. We need to pip install the youtube_dl library and the latest ffmpeg build for your specific operating system.

pip install youtube_dl

now to import the libraries needed for the program

import youtube_dl
import sys
Enter fullscreen mode Exit fullscreen mode

declare a variable ydl_opts (in this case) to specify the specifications of the file you want to download. in this case we will select the format to be the best audio format available, the prefferedcodec will be mp3 and the rest can be left as default as shown here.

ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }],
}
Enter fullscreen mode Exit fullscreen mode

Write an if statement to download the file and pass the values of the ydl_opts variable to youtube_dl

if __name__ == "__main__":
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        filenames = sys.argv[1:]
        ydl.download(filenames)
Enter fullscreen mode Exit fullscreen mode

Run your command terminal and execute the python program and append the command line with the URL of any YouTube video (make sure to include "https://").

python youtubetomp3.py <URL>

Run the program and if everything goes smoothly you should see the following output:
Alt Text
The MP3 file will appear in the same directory as your program. Since my program is on my desktop you can see the downloaded file here.
Alt Text

Thanks for reading, you can find the code on it's GitHub repo here.

Top comments (3)

Collapse
 
maryhenderson7okyr profile image
MaryHenderson7okYR

Anyway, I thought, what a great feature it would be for Reason. The ability to convert a video from You Tube to MP3 or WAV. I know it's an already compressed format and given the fact we don't have video sync in Reason yet (if that matters, not really I don't suppose) but the way I could see this working actually would be, Go to File Menu, and in the list (see attachment pic) underneath Drop to Propellerhead and before Export Rex to Midi it could have "Convert You Tube Video". Then a box appears with a place to paste the URL into. Also, there would need to be 2 other options within the box, Folder (for the target folder destination for the converted file to be placed) and 2 checkboxes for the file format, MP3 or WAV (And yes I know we have no MP3 export yet in Reason but it does allow drag 'n' drop of MP3's to the sequencer. Would we need one for Quality too, is it bitrate or depth or whatever, kbps? Anyway, then click OK and wait a few seconds for Reason to do it's magic and it would convert the file and place it in the selected folder. Then you could just drag and drop into the sequencer.) Quick, easy. No need for these virus ridden sites we now have online. The folder may be already open in your browser and with Reason, doesn't automatically appear, you need to go out of that folder and back in again for it to show up. I suggest Propellerheads have a refresh button somewhere within the browser to refresh the folder you're in for instances of this nature so you don't have to go back out and back in again. I know it's not a massive deal but still, half the time needed. Just click refresh.

Collapse
 
sutherlandber profile image
SutherlandBer • Edited

Thank you for your effort to post all this info. It can be seen that you have made an effort to structure all the information well and clearly. For example, I use a converter - coconvert.com to download YouTube videos, and I convert them to WAV format. I think it is much easier, and the quality is good, the same how on YouTube. Also, I will try to use as you said, and I will navigate Github to read more; maybe I will find more exciting and helpful information for me. Anyways you are brave, and the info you have posted is fascinating. I wish you good luck!

Collapse
 
gareth34 profile image
gareth34

Wow!!! I'm really excited about easytube.pro/! This YouTube video conversion service is beyond my expectations. When it comes to functionality, EasyTube.pro is amazing. With the help of a link to a YouTube video, I instantly copied it and got the final result in a few seconds. Converting the video to various formats, including MP4, AVI, and many others, was very easy and fast.