Posted on Leave a comment

Python Script to Download Youtube Videos

Here is an example of a Python script that uses the “pytube” library to download a YouTube video:

from pytube import YouTube

link = "https://www.youtube.com/watch?v=Nj6PFaDmp6c"
yt = YouTube(link)

#Download the video with the highest resolution
video = yt.get('mp4', '720p')

#Set the filename and download location
video.download("/path/to/save/video")

You can also download the audio-only version of a video by using the yt.streams.filter(only_audio=True) method, and then calling the download() method on the audio stream.

from pytube import YouTube

link = "https://www.youtube.com/watch?v=Nj6PFaDmp6c"
yt = YouTube(link)

# Download audio-only version
audio_stream = yt.streams.filter(only_audio=True).first()
audio_stream.download("/path/to/save/audio")

It is important to note that downloading videos from YouTube without permission from the video owner or without a valid reason is against YouTube’s terms of service and also it may be illegal depending on your country laws.

Leave a Reply

Your email address will not be published. Required fields are marked *