Python Youtube Video Download

Python Youtube Video Download


Python Youtube Video Download

 Hello everyone, in this post, I will explain how you can download video, playlist, and audio from Youtube using Python. Before starting the article, I leave the what is pip and how to install post here because we will need to install the pytube library with pip in this article.

Pytube Installation

$ python -m pip install pytube

If you are using Windows, after typing python into cmd and then entering the above command, pytube will be installed without issues.
For detailed support on the installation, you can visit the official pytube page.


Our Python Youtube Video Download Codes and Explanations

from pytube import Playlist ## The library for downloading playlists
from pytube import YouTube ## The library for downloading a single video

## The function we wrote to download a single video
def DownloadSingleVideo(link):
    try: ## We check if there is an error
        video = YouTube(link) ## If there is no error, we assign the video variable as the YouTube(link) function
        print("%s video has started downloading.." % video.title) ## We print the video name to the screen
        video.streams.get_highest_resolution().download('video') ## We start downloading. Here, streams.get_highest_resolution lets us choose the highest quality. .download starts the download and download('video') is the folder to download into.
        print("%s video has been downloaded successfully." % video.title) ## When the download is finished, we print this
    except: ## If there is a problem
        print("A problem occurred during the process!") ## Print error on the screen


def DownloadAudioOnly(link):
    try: ## Check for error
        audio = YouTube(link) ## If no error, assign the variable
        print("%s audio file of the video is starting to download.." % audio.title) ## Print name
        audio.streams.get_audio_only().download("audio") ## Download audio. Here, get_audio_only() selects audio only, download('audio') is the download folder.
        print("%s audio file of the video has been downloaded." % audio.title) ## We're telling that it's done
    except: ## If error
        print("A problem occurred during the process!") ## Print error


def DownloadPlaylistVideos(link):
    try: ## Error check
        playList = Playlist(link) ## Create variable, assign link to Playlist function
        downloadedVideoCount = 0 ## Variable to know how many videos downloaded or if any failed
        print("There are %s videos in the playlist." %
              len(playList.video_urls)) ## Print the count
        for i in playList.videos: ## Loop through videos in playlist
            i.streams.get_highest_resolution().download('video') ## Download one by one
            downloadedVideoCount += 1 ## Counter
            print(i.title + " video has been downloaded successfully")
        print("A total of %s videos in the playlist have been downloaded successfully." %
              downloadedVideoCount) ## Telling completion
    except: ## If error
        print("A problem occurred during the process!") ## Print error


def DownloadPlaylistAudios(link):
    try: ## Error check
        playList = Playlist(link) ## Create variable, assign link
        downloadedVideoCount = 0 ## Counter
        print("There are %s audios in the playlist." %
              len(playList.video_urls)) ## Print count
        for i in playList.videos: ## Loop through videos
            i.streams.get_audio_only().download('audio') ## Download audio
            downloadedVideoCount += 1 ## Increase counter
            print(i.title + " audio of the video has been downloaded successfully")
        print("A total of %s audios of videos in the playlist have been downloaded successfully." %
              downloadedVideoCount) ## Telling completion
    except: ## If error
        print("A problem occurred during the process!") ## Print error

## Function to select operation.
def SelectionScreen():
    operation = input(
        "Which operation would you like to perform? \nOperation ---- Command \nDownload single video ---- \"1\" \nDownload playlist videos ---- \"2\" \nDownload audio only ---- \"3\" \nDownload playlist audio only ---- \"4\"\nYour operation: ") ## Ask user for operation
    if operation == "1":
        link = input("Enter the video or playlist link address:") ## Ask for url
        DownloadSingleVideo(link)
    elif operation == "2":
        link = input("Enter the video or playlist link address:")
        DownloadPlaylistVideos(link)
    elif operation == "3":
        link = input("Enter the video or playlist link address:")
        DownloadAudioOnly(link)
    elif operation == "4":
        link = input("Enter the video or playlist link address:")
        DownloadPlaylistAudios(link)


SelectionScreen() ## Call our function to run the code


PyTube Folder

If you want the codes to work as I shared, make sure that there are subfolders named "audio" and "video" in the folder you created.

Since I wrote the explanations next to the code lines, I don't provide extra information, but if you have any questions, you can ask them as a comment.

To access the version I used in the article, you can click here to check my GitHub account.