How to Download YouTube Videos With Python?
Tags: python, tutorial, programming, productivity
Published: 2021-12-23 05:52:00

YouTube has become the go-to source for videos on the internet. While there are many ways to download YouTube videos, using Python is one of the easiest. In this article, we will show you how to use Python to download YouTube videos.

We can use the package Pytube to download YouTube videos in a Python script. It's a free tool you can install from the PyPI repository. You can also specify the output format (eg: mp4) and resolution (eg: 720px) when downloading videos.

## Downloading YouTube Videos in Python

Here's a step-by-step approach to downloading YouTube videos in Python.

1. Install [Pytube](https://pytube.io/en/latest/index.html) using pip

   ```shell
   pip install pytube
   ```

2. In your script import the YouTube class from pytube package.

   ```python
   from pytube import YouTube
   ```

3. Create an object of YouTube bypassing the video URL
   ```python
   yt = YouTube("<Your youtube URL>")
   ```
4. Use the filter method to specify the download format of the video
   ```python
   mp4_files = yt.streams.filter(file_extension="mp4")
   ```
5. Get the video you want by specifying the resolution

   ```python
   mp4_369p_files = mp4_files.get_by_resolution("360p")
   ```

6. Save the downloaded video to the local file system
   ```python
   mp4_369p_files.download("<Download folder path>")
   ```

Here's how the completed script will look like. I've wrapped it with a function definition that accepts the url and outpath as arguments.

```python
from pytube import YouTube


def download_360p_mp4_videos(url: str, outpath: str = "./"):

    yt = YouTube(url)

    yt.streams.filter(file_extension="mp4").get_by_resolution("360p").download(outpath)


if __name__ == "__main__":

    download_360p_mp4_videos(
        "https://www.youtube.com/watch?v=JfVOs4VSpmA&t=4s&ab_channel=SonyPicturesEntertainment",
        "./trailers",
    )
```

The above code will download the Spiderman: No way home trailer and save it in a folder called 'trailers'.

## Creating a CLI to download more videos in command prompt.

With a little trick, we can even [make this a CLI](/python-cli-tutorial). Here's how to use [Typer](https://typer.tiangolo.com/) to convert the function you wrote into a CLI.

```python
from pytube import YouTube
from typer import Typer

app = Typer()


@app.command()
def download_360p_mp4_videos(url: str, outpath: str = "./"):

    yt = YouTube(url)

    yt.streams.filter(file_extension="mp4").get_by_resolution("360p").download(outpath)


if __name__ == "__main__":

    app()

```

Now we converted our function into a command line interface that accepts a URL parameter and saves the video into the local filesystem. Here's how to run this.

```shell
python get-yt.py "https://www.youtube.com/watch?v=JfVOs4VSpmA&t=4s&ab_channel=SonyPicturesEntertainment"
```

---
Did you like what you read? [**_Consider subscribing to my email newsletter_**](https://thuwarakesh.medium.com/subscribe)  because I post more like this frequently. 

Thanks for reading, friend! Say Hi to me on [LinkedIn](https://www.linkedin.com/in/thuwarakesh/), [Twitter](https://twitter.com/Thuwarakesh), and [Medium](https://thuwarakesh.medium.com/).