Download Youtube Videos With Python


Hello! ๐Ÿ™‚ In this tutorial I will show you how you can use python to download YouTube videos.

Installing The Dependencies

First we need to install the required dependencies, we will be using a library called "pytube". Create a file called "requirements.txt" and add the following:


Next to install the dependencies you will need to run the following command:

pip install -r requirements.txt

Now we are ready to actually create the code. ๐Ÿ˜Ž

Writing The Code

Next create a file called "", first we need to declare the imports:

import argparse
from pytube import YouTube

We will be using argparse to parse arguments passed to the script and we will be using pytube to download YouTube videos.

Next we need to declare which directories we want the video/audio to be saved to, I use the following constants:


Videos and audio only will be saved into seperate directories. Next we need to create a function that handles both video and audio:

def download(video_url):
    video = YouTube(video_url)
    video = video.streams.get_highest_resolution()

        print("Failed to download video")

    print("video was downloaded successfully")

Nothing too complicated, this function takes the URL of the YouTube video, gets the highest resolution available and then downloads it into the videos directory.

However if your like me and like using YouTube for mostly music content you may not need the video and just want the audio only, lets create another function that allows us to download the audio only:

def download_audio(video_url):
    video = YouTube(video_url)
    audio = video.streams.filter(only_audio = True).first()

        print("Failed to download audio")

    print("audio was downloaded successfully")

Similar to the video function but this time we filter the streams for audio only and grab the first one available, after that we download the audio only stream to the audio directory.

Finally we need to write the main function:

if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("-v", "--video", required = True, help = "URL to youtube video")
    ap.add_argument("-a", "--audio", required = False, help = "audio only", action = argparse.BooleanOptionalAction)
    args = vars(ap.parse_args())

    if args["audio"]:

Here we use argparse to parse the video url, we also use an optional audio only flag. If the flag is enabled the audio of the video will be downloaded, by default both the video and audio will be downloaded.

Done! ๐Ÿ˜† You should now be able to run the script via the following commands:

# video
python -v "[YouTube Video URL]"

# audio
python -a -v "[YouTube Video URL]"

The media should be available in the audio/videos directory.


Here I have demonstrated how to download YouTube videos using a library called pytube. I personally use this to download songs that I like. ๐Ÿ˜ƒ

You can find the source for this tutorial here:

Like me work? I post about a variety of topics, if you would like to see more please like and follow me. Also I love coffee.

โ€œBuy Me A Coffeeโ€