NEWSubscribe to Receive Free E-mail UpdatesSubscribe

How to Download YouTube videos with Python?

YouTube has become the most popular video-sharing site on the internet. While there are a variety of methods for downloading YouTube videos, Python is one of the most straightforward.

I will show you how to download YouTube videos with Python in this article.

In a Python script, we can utilize the Pytube library to retrieve YouTube videos. It’s a completely free tool that you may get from the PyPI repository. When downloading videos, you can additionally specify the output format (e.g., mp4) and resolution (e.g., 720px).


Subscribe our free newsletter for future projects & source code {alertSuccess}

Note: For Educational Purposes Only{alertWarning}

Downloading YouTube Videos in Python

Here’s a step-by-step guide to downloading YouTube videos in Python.

Step 1. Install Pytube using pip.
pip install pytube{codeBox}

Step 2. import the YouTube class from pytube package.
from pytube import YouTube{codeBox}

Step 3. Create an object of YouTube to pass the video URL
video_link = "https://www.youtube.com/watch?v=MlXj2T8xzm0"{codeBox}

Step 4. Use the filter method to specify the download format of the video
yt.streams.filter(file_extension='mp4'){codeBox}

Here’s what the completed script will look like.
from pytube import YouTube
#pip install pytube

save_to = "F:/Dropbox/PythonCodes/downloadYouTubeVideo"
#where to save
 
video_link = "https://www.youtube.com/watch?v=MlXj2T8xzm0" 
#link of the video to be downloaded

try:
 
    yt = YouTube(video_link)

except:
 
    print("Connection Error!!")
 
try:
    print('Downloading start....')
    yt.streams.filter(file_extension='mp4').first().download(save_to)
#filter mp4 extension

    print('Video Downloaded!!')
 
except:
 
    print("Error!"){codeBox}

Subscribe our free newsletter for future projects & source code {alertSuccess}

Post a Comment

0 Comments