Building an Audio Downloader using Python

In this post, I will show you how to build a program that can download the audio file of a YouTube video. This will be a very simple project to get started with Python. Things you can do with Python is infinite, and that what makes Python a very powerful programming language. In my posts, I show the variety of projects that can be build using Python. From Face Recognizer to Barcode Reader, if you have an idea just make it happen. Your projects doesn’t need to perfect, just find a problem that you want to solve. Let’s get started!

Python

Python is a general-purpose programming language that is becoming ever more popular for analyzing data. Python also lets you work quickly and integrate systems more effectively. Companies from all around the world are utilizing Python to gather bits of knowledge from their data. The official Python page if you want to learn more.

Building a Prediction Model in Python


Table of Contents:

  • Getting started
  • Python library
  • Main program
  • Converting to audio file
  • Conclusion

Getting Started

To give you some background, let me tell you what has motivated me to write this program. I started a YouTube channel this year, and I started to see that musics are playing a big role in videos. And choosing the right audio is really a challenging part of video editing. I am purchasing the audio to use them in my videos, and before making the purchase I want to test it out on my videos. Then, this YouTube audio downloader idea came to my mind. With this program, I will be able to test the audio before purchasing it. And the simplicity of the program is incredible, it made my video editing journey more fun and enjoyable.

In the next step, we will start by installing the library that we will need for this project and then we will start programming. For this project, I recommend using a regular code editor instead of a Jupyter notebook. Feel free to use your favorite code editor.


Python Library

We will need only one library, in other words package for this project. I mentioned that this will be a very simple project. We will install the following python package: YouTube DL. YouTubeDL is an open-source software project that downloads video and audio primarily from YouTube, but it does work with different platforms too.

Installing the package using PIP (python package manager):

pip install youtube_dl

After the installation is completed, we can import it to our program.We will add the following line in our code editor.

from youtube_dl import YoutubeDL

Perfect! Let’s move to the next step where we will start writing the main program.


Main program

We will have two parts: defining the audio downloader and the while loop. I will share a short description of what is happening in the code.

audio_downloader = YoutubeDL({'format':'bestaudio'})

while True:
  try:
    print('Youtube Downloader'.center(40, '_'))
    URL = input('Enter the url of the video: ')
    audio_downloader.extract_info(URL)

  except Exception:
      print("Couldn\'t download the audio")
  finally:
      option = int(input('\n1.download again \n2.Exit\n\nOption here :'))
      if option!=1:
         break


Converting to audio file

In this step, we are going to convert the downloaded file to audio format. The downloaded format is webm. It is media format designed specifically for web. So to open the webm file we have to converted to regular audio format such as MP3, M4A, or WAV.

Also here is a short definition of webm:

WebM defines the file container structure, video and audio formats. WebM files consist of video streams compressed with the VP8 or VP9 video codecs and audio streams compressed with the Vorbis or Opus audio codecs. (Reference: https://www.webmproject.org/about/)

You can use online converter tools to convert webm file to an audio file. I used a website called cloudconvert, it has simple interface. Feel free to find a different platform that gets the job done.


Conclusion

Congrats!! You have a created a program that downloads the audio of video from YouTube. We did this by using one python package. This package can used for different projects too. Now, you have an understanding of transforming an idea into a real project. Working on hands-on programming projects like this one is the best way to sharpen your coding skills. I would be glad if you learned something new today.

Follow my blog and youtube channel to stay inspired. Thank you,

Personal Note: I don’t monetize this blog for a better blog experience. I don’t want my readers to see get distracted with some ads while reading articles. And my second priority is to keep the content free and accessible by anyone from around the whole world.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: