DEV Community

Cover image for How to download youtube video as audio using python
Jordan Kalebu
Jordan Kalebu

Posted on • Updated on

How to download youtube video as audio using python

The original article can be found at kalebujordan.dev

Hi guys,

In this article, I will show how you can use python to download a youtube video as audio in a matter of few seconds using youtube-dl python wrapper.

Instead of using an online extension with accumulated ads like savenet, why not use your python skills to build your own downloader now.

Requirements

For you to be able to completely follow this tutorial, you're supposed to have youtube-dl installed on your machine.

Installation

pip install youtube_dl
Enter fullscreen mode Exit fullscreen mode

Basics of youtube-dl

youtube-dl is a command-line tool for downloading videos from Youtube and other video sites, it provides a wrapper for python to directly use its functionality within code.

YouTube-dl provides a direct way to download the audio from Youtube by specifying the format of output we want for to next command which is downloading.

Downloading YouTube video as audio

from youtube_dl import YoutubeDL

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

audio_downloader.extract_info(link to the video)
Enter fullscreen mode Exit fullscreen mode

That's how youtube-dl really makes our life easier, from the above concept you can build your own GUI YouTube downloader using your favorite framework ranging from Tkinter to PyQT

Below is a sample command line app that runs an app for downloading YouTube-audio that runs on top youtube-dl, you can twist it the way you want it.

  • app.py
from youtube_dl import YoutubeDL

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

while True:

    try:

        print('Youtube Downloader'.center(40, '_'))

      URL = input('Enter youtube url :  ')

      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
Enter fullscreen mode Exit fullscreen mode

When you run the above code it will automatically begin downloading the audio from the YouTube video link you provided If available.

Hope you find this post interesting, don't forget to subscribe to get more tutorials like this.

I recommend you to also read this ;

In case of any suggestion or comment, drop it in the comment box and I will get back to you ASAP

Top comments (9)

Collapse
 
michaelcurrin profile image
Michael Currin • Edited

Important to mention - even if it is available on PyPI as the link you provided, this is controversial library to use.

It was taken off of GitHub for legal reasons as per notice:

github.com/ytdl-org/youtube-dl

And if you try to distribute the app on GitHub yourself you can get banned

xda-developers.com/github-warns-of...

People also protested the ban as per the notice there.

So it is good to be aware of this and share the info with others where it matters in this context, so they can make informed decisions.

BTW There are also browser extensions which download YT videos so there are other ways to do it which exist.

Collapse
 
lesha profile image
lesha 🟨⬛️

Not really in a mood to host anything on github anymore. I'd rather pay five bucks for VPS and host my own git server and only have github as a mirror

Collapse
 
michaelcurrin profile image
Michael Currin

GitHub restored the repo thenextweb.com/dd/2020/11/17/githu...

Thread Thread
 
kalebu profile image
Jordan Kalebu

Yeah I see .. good news for we developers

Collapse
 
krishan111 profile image
Krishan111

Great Code Bro

Collapse
 
ibby_blaq profile image
Ibby

Nice one dawg.

Collapse
 
kalebu profile image
Jordan Kalebu

Thank you @lbby

Collapse
 
or_gavish_c9090992e8ec11b profile image
Or Gavish

Hey! I tried the code and stumbled upon a problem. Most of the files are downloaded as webm. How can I download the files directly as mp3 or any other format which Windows can play?

Thanks /Or

Collapse
 
kalebu profile image
Jordan Kalebu • Edited

Hello You could twist the audio downloader to be a format you want l


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

 # OR 

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

Enter fullscreen mode Exit fullscreen mode

and if that format is is available it will automatically download it