YouTube Shorts Downloader from URL using Python

In this tutorial, We will learn How to download shorts from YouTube using URLs. we will use pyTube modules of Python to download the shorts from YouTube. This will take Input from the user and Download the shorts from YouTube in the D drive on pc at the specified path ytDownloader folder. Here’s a step-by-step Guide:

Requirements:

pip install pytube
pip install os

Code:

# here import necessary modules
from Pytube import YouTube
import os

By importing the PyTube module we will perform download operation on URLs and by OS modules we will create a folder on the location that was specified in the code

#here sh_down function i.e., Short_download function 
def sh_down(url, output_loc = ' '):
    try:
        ur = YouTube(url)
        stream = ur.streams.get_highest_resolution()
        video = stream.download(output_loc)
        print(f"downloaded: {ur.title}")
        return video
    except Exception as e:
        print(f"error : {e}")

Here we have created a function with the name sh_down i.e., the shorts downloader function which takes two arguments first is URL and second one is output location. In this code, we have applied proper error handling and show the output as downloaded with the file name.

#here we will all the function which we have created above:
if __name__ == "__main__":
    user_input = input("enter the link here :- ")

    output_loc = 'D:/yt_downloader'
    os.makedirs(output_loc, exist_ok=True)

    sh_down(user_input, output_loc)

Here we will take the URL as input from the user and specify the location where we want to download a file.Here, the OS module will create a folder on the location if not exist and if exists then the condition will become True and move on and call the sh_down function. this will download the file on the “D:/yt_downloader” location.

Complete code:

from pytube import YouTube
import os

def sh_down(url, output_loc = ' '):
    try:
        ur = YouTube(url)
        stream = ur.streams.get_highest_resolution()
        video = stream.download(output_loc)
        print(f"downloaded: {ur.title}")
        return video
    except Exception as e:
        print(f"error : {e}")

if __name__ == "__main__":
    user_input = input("enter the link here :- ")

    output_loc = 'D:/yt_downloader'
    os.makedirs(output_loc, exist_ok=True)

    sh_down(user_input, output_loc)

This Python code uses the Pytube library to download YouTube Shorts. The user gives input of YouTube Shorts URL, and the code downloads the video at the highest available resolution to a specified output directory. If the output directory does not exist, it is created automatically. The code handles exceptions to provide error feedback if the download fails.

Output:

enter the link here :- https://youtube.com/shorts/KRPZwL0WMc4?si=OgFq775P9SVVZAou
downloaded: @CarryMinati ROAST BIGG BOSS #youtubeshorts #short #shorts

 

Leave a Comment

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

Scroll to Top