x32x01
  • by x32x01 ||
YouTube is very popular video sharing website. Downloading a video from YouTube is a tough job. Downloading the Downloader and get the video using that or go to any other website which fetches the video and saves on your computer. Using Python, this task is very easy. Few lines of code will download the video from YouTube for you. For this, there a python library named as ‘pytube’. pytube is a lightweight, dependency-free Python library which is used for downloading videos from the web.

Table of Content
  • pytube
  • Downloading a single video
  • Downloading multiple videos
  • Download multiple videos using File Handling
  • Important Points to download youtube videos:

pytube​

pytube is not the native library. You need to install it before using it. Installation is easy when you have pip. In the Terminal or Command Prompt, type the following command to install pytube.
Code:
pip install pytube
In case you don’t have pip, install it as an external library.

Downloading a single video
pytube library makes the video downloading very easy. Create the object of the YouTube module by passing the link as the parameter. Then, get the appropriate extension and resolution of the video. You can set the name of the file as your convenience, in another case original name will be kept. After that, download the file using the download function which has one parameter which is the location where to download the file.
Python:
from pytube import YouTube

# where to save
SAVE_PATH = "/home/chintusharma/Downloads" #to_do

# link of the video to be downloaded
link = "https://youtube.com/shorts/akL53YfPTfI?si=ju1ohfkBdFSInLNj"

try:
    # object creation using YouTube
    yt = YouTube(link)
except:
    #to handle exception
    print("Connection Error")

# Get all streams and filter for mp4 files
mp4_streams = yt.streams.filter(file_extension='mp4').all()

# get the video with the highest resolution
d_video = mp4_streams[-1]

try:
    # downloading the video
    d_video.download(output_path=SAVE_PATH)
    print('Video downloaded successfully!')
except:
    print("Some Error!")

Downloading a file takes some time as a very large amount of data is being downloaded from the web. Depending on the speed of the connection, time taken to execute the program varies. In case you wish to download the number of files, go with the next case.

Downloading multiple videos​

The basic task of downloading the multiple videos is same as downloading a single video. We can use a for loop for downloading the video.
Python:
from pytube import YouTube

# Where to save
SAVE_PATH = "E:/"  # to_do

# Links of the videos to be downloaded
links = ["https://www.youtube.com/watch?v=xWOoBJUqlbI",
         "https://www.youtube.com/watch?v=xWOoBJUqlbI"]

for link in links:
    try:
        # Object creation using YouTube
        # which was imported in the beginning
        yt = YouTube(link)
    except:
        # Handle exception
        print("Connection Error")

    # Get all streams and filter for mp4 files
    mp4_streams = yt.streams.filter(file_extension='mp4').all()

    # Get the video with the highest resolution
    d_video = mp4_streams[-1]

    try:
        # Download the video
        d_video.download(output_path=SAVE_PATH)
        print('Video downloaded successfully!')
    except:
        print("Some Error!")

print('Task Completed!')

In this, we have used a for loop for downloading multiple files as shown. One can use file handling for keeping the all the links in a file which needs to be downloaded.

Download multiple videos using File Handling​

Using file handling, we can open the file which has the group of links in it. Traversing every link of a text file and applying the very basic video downloading program is done here. Here, we have a text file named as “links_file.txt” which has all the links which need to be downloaded.
Python:
from pytube import YouTube

# where to save
SAVE_PATH = "E:/" #to_do

# link of the video to be downloaded
# opening the file
link=open('links_file.txt','r')

for i in link:
    try:
[LIST]
[*]        
[/LIST]
        # object creation using YouTube
        # which was imported in the beginning
        yt = YouTube(i)
    except:
        
        #to handle exception
        print("Connection Error") 
    
    #filters out all the files with "mp4" extension
    mp4files = yt.filter('mp4')
    
    # get the video with the extension and
    # resolution passed in the get() function
    d_video = yt.get(mp4files[-1].extension,mp4files[-1].resolution)
    try:
        
        # downloading the video
        d_video.download(SAVE_PATH)
    except:
        print("Some Error!")
print('Task Completed!')

Important Points to download youtube videos:​

  • Make sure you are connected to the internet to download the videos. Otherwise it will raise an error.
  • Don’t use the set_filename() function in any loop. In this case, only one video will be downloaded.
  • You can modify the names everytime using another array of names.
  • Connection Interruption in between will also raise an error and video will not be downloaded in that case
 

Similar Threads

x32x01
Replies
0
Views
323
x32x01
x32x01
TAGs: Tags
download youtube videos python script pytube youtube

Register & Login Faster

Forgot your password?

Latest Resources

Forum Statistics

Threads
517
Messages
518
Members
45
Latest Member
Tacola
Back
Top