How to Create a YouTube Video Downloader with Python

Downloading YouTube videos to your local storage is often an uphill battle, especially when dedicated YouTube downloaders keep failing you. But you can make a reliable YouTube video downloader using Python.

No worries if you’re not familiar with Python programming, we’ll provide you with what you need to get started. It’s easy, and once you’ve got everything set up you might not need to reinvent the wheel for later downloads.

Let’s go.

Configure Python

To get started, you need to install Python and run it on your PC. Don’t bother if you are using Mac, as Python is already pre-installed.

But if you are on Windows, go to python.org to download and install the latest version of Python on your PC.

To test if Python works on your PC after installation, open your terminal and type:

python --version

Then hit Enter. If your terminal displays the Python version you downloaded earlier, then you have successfully installed Python on your PC.

VIDEO OF THE DAY MAKEUSE

Next, create a folder for your project. Open the command line in this directory and create a new Python file in the same location. Make sure your Python file has the .py file extension.

Create a virtual environment, then open any text editor you like from there.

Related: Create a Virtual Environment in Python

To note: Only download videos if you have the proper permission to do so. See Is it legal to download YouTube videos? for more information.

Create your YouTube downloader with Python

To start this tutorial, you need to install a YouTube Python utility library called pytube using seed.

To do this, enter the following command in your terminal:

pip install pytube

Once you install pytube, go back to your text editor, open your Python file and import pytube:

from pytube import YouTube

Go to YouTube and copy the URL of the video you want to download. Then create a YouTube instance on the following line of your Python file:

URL = "Enter video URL"
video = YouTube(URL)

The pytube module works by giving you different feed options. A video, however, has different stream resolutions. So pytube allows you to upload your video based on these.

Once you’ve instantiated a YouTube object with the video URL, you can print the available feeds:

video_streams = video.streams
print(video_streams)

You can run your Python code through the command line by calling your Python file like this:

python file_name.py

To replace file name with the name of your Python file.

The output looks like this:

Pytube video stream output

You can also specify streams including the file extension type using the filtered function:

video_streams = video.streams.filter(file_extension='mp4')
print(video_streams)

And it looks like this:

video stream output

The mod, however, returns different stream resolutions, from 360p to 720p and 1080p (and maybe more). But on closer inspection, every resolution has a itag value.

For example, res = “720” To itag = “22”, while the itag at 360p resolution is 18.

You can invoke a stream using this itag value including the get_by_itag () function:

video_streams = video.streams.filter(file_extension='mp4').get_by_itag(22)
print(video_streams)
Output:

The resolution of the above stream is 720p (res = “720p”). You can try the itag value for 360p to get lower resolution. You can also increase the resolution to 1080p or any other available resolution if you want. All you need is the itag value for your preferred resolution, which is always available when you print the streams for any video.

To check the title of a video:

video = YouTube(URL)
video_streams = video.streams.filter(file_extension='mp4').get_by_itag(22)
print(video_streams.title)
Output: Achilles Vs. Hector - TROY (2004)

Now here is how to download video at 720p resolution:

video = YouTube(URL)
video_streams = video.streams.filter(file_extension ='mp4').get_by_itag(22)
video_streams.download()

The video, however, downloads to your current working directory in this case. It also inherits the default title from YouTube.

But you can specify a download directory for your video and change the file name:

video = YouTube(URL)
video_streams = video.streams.filter(file_extension = 'mp4').get_by_itag(22)
video_streams.download(filename = "my first YouTube download2",
output_path = "video_path")

Remember to replace video_path with your favorite download directory.

Now let’s put all of the code in one place. But this time, changing the resolution to 360p:

from pytube import YouTube
URL = "Enter video URL"
video = YouTube(URL)
video_streams = video.streams.filter(file_extension='mp4').get_by_itag(18)
video_streams.download(filename = "my first YouTube download2",
output_path = "video_path")

That’s it! You have just created a DIY YouTube video downloader with Python.

You can confirm the resolution of your video by right clicking on the video and then going to Properties> Details. Under Video, check the value of Frame height, this indicates the video resolution.

Continue to automate tasks with Python

Python is versatile, and using it to automate simple tasks on your PC improves your productivity. If you know a little bit about it, the ability to auto-code your own YouTube video downloader is one of the dividends you receive.

That said, you can also automate Excel calculations, build a calculator, customize your bash, and do more with Python programming.


Code written with Python sublayer
10 Python Project Ideas Suitable For Beginners

You know the basics and are now ready to apply them. Get started with these Python projects!

Read more


About the Author


Source link

Comments are closed.