Download Youtube Videos using Python

| |

Home » Download Youtube Videos using Python

Youtube is a website where you can freely upload and share videos online. But what if there was an easier way to download these videos than trying to find their URLs through the source code? Well, thankfully, it’s not really as complicated as one might think. There are various programming libraries that make downloading from Youtube a lot simpler. In this tutorial, we will download Youtube videos using Python. We are going to use PYtube which helps us both download Youtube videos, titles, thumbnails even complete playlists. We are going to create two programs: the first one is a command-line-based downloader and another GUI-based version which we will build using Tkinter.

Objective

  1. Fetching the youtube video title.
  2. Fetching the youtube video thumbnail image.
  3. Download video from youtube.
  4. Download video from youtube in a specific resolution.
  5. Playlist from youtube.
  6. Create a GUI-based youtube downloader using Tkinter.

Prerequisites

PYtube:

PyTube is an open-source Python module and a command-line utility to download YouTube content and metadata. If you’re familiar with YouTube-DL, PyTube is the same idea but written in Python. The tool needs very little to run, operating on Windows, Mac, and Linux systems with Python 2 and 3.

You can download pytube using the following command in your command prompt or terminal.

pip install pytube3

Tkinter:

Tkinter is an advanced GUI module for Python. It allows you to create flexible and powerful graphical user interfaces. Instead of providing just a simple window with widgets inside, you can place multiple widgets together, change their properties, and bind them to data. Widgets are basic GUI components that allow you to create the most standard GUI elements. They exist in many programming languages, including Python.

You can download Tkinter using the following command in your command prompt or terminal

pip install tkinter

Step -1: Fetching the youtube video title

from pytube import YouTube
url = 'https://www.youtube.com/watch?v=DODLEX4zzLQ'
yt_video = YouTube(url)
print(yt_video.title)

Code explanation:

  1. Import the packages.
  2. Adding URL of the youtube video that we want to download.
  3. Calling youtube method.
  4. Printing the title.

Output:

As you can see it worked successfully and we have the title of the youtube video 

Step -2: Fetching the youtube video thumbnail image

from pytube import YouTube
url = 'https://www.youtube.com/watch?v=DODLEX4zzLQ'
yt_video = YouTube(url)
print(yt_video.thumbnail_url)

Code explanation:

  1. Import the packages.
  2. Add URL of the youtube video that we want to download.
  3. calling youtube method.
  4. Get the thumbnail of the video using the thumbnail method.

Output:

You will get a link in the output and if you open the link the image will be displayed. You can download that from there.

Step -3: Download video from Youtube

from pytube import YouTube

url = 'https://www.youtube.com/watch?v=DODLEX4zzLQ'
yt_video = YouTube(url)
# this method will download the highest resolution that video is available
yt_video = my_video.streams.get_highest_resolution()
methodyt_video.download()

print('your video is downloaded successfully')

Code explanation:

  1. Import the packages.
  2. Add URL of the youtube video that we want to download.
  3. Calling youtube method.
  4. In order to download the video from youtube, we need to define the stream resolution first.
  5. To download the video from youtube we will use the download method.

After running the code it will start downloading the video and after completion, your youtube video will be in the same directory as your python file.

Step -4: Download video from youtube in a specific resolution

If you want to download the video in a specific resolution of in the audio-only format then you can use the following code 

from pytube import YouTube
url = 'https://www.youtube.com/watch?v=DODLEX4zzLQ'
yt_video = YouTube(url)

# here we are fetching a list of all the video resolution
videos = yt_video.streams.all()
# we are using enumerate to get the index number
res_list = list(enumerate(videos))
for i in res_list:
    print(i)

resolution = int(input("Enter the index number of the video : "))
videos[resolution].download()
print('your video is downloaded successfully')

Code explanation:

  1. Import the packages.
  2. Adding URL of the youtube video that we want to download.
  3. Calling youtube method.
  4. Here we are fetching a list of all the video resolutions.
  5. We are using enumerate to get the index number.

Output:

In the output, it will ask you what version of the video you want to download and you have just entered the index number and will download it automatically.

You can use the filter method to filter out the only audio or only video 

To download only audio:

# for getting only audio list
videos = yt_video.streams.filter(only_audio=True)

Output:

To download only video:

# for getting only video list
videos = yt_video.streams.filter(only_audio=True)

Output:

Step -5: Download playlist from Youtube

from pytube import Playlist

playlist = Playlist('https://www.youtube.com/playlist?list=PLG9-DrM4Coeq7vdqBb84sNRZZHEUfQDcK')

print('Number of videos in playlist: %s' % len(playlist.video_urls))
playlist.download_all()

Code explanation:

  1. Import the packages.
  2. Creating a variable to store data of the playlist
  3. Add the link of the playlist
  4. print name of video that is currently downloading.

And that’s it

Step -6: Create a GUI based Youtube downloader using QT designer

# import required libraries
import string
import tkinter as tk
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
from cv2 import dft
from pytube import YouTube

def createwidgits():
    # creating a button
    link_label = Label(root, text ='Paste URL: ', bg="#999999")
    # placing the label
    link_label.grid(row=1, column=0, pady=10, padx=10)

    # creating a entry point
    root.link_text = Entry(root, width=60 , textvariable=video_link)
    # placing the point
    root.link_text.grid(row=1, column=1,pady=10, padx=10)

    # creating a destination label
    destination_label = Label(root, text ='Destination: ', bg="#999999")
    # placing the label
    destination_label.grid(row=2, column=0, pady=10, padx=10)

    # creating a destination box
    root.destination_label = Entry(root, width=60 , textvariable=Download_path)
    # placing the box
    root.destination_label.grid(row=2, column=1,pady=10, padx=10)

    # create a browse button
    browse_but = Button(root, text="Browse", command=browse, width=10, bg="#ffffff")
    # place the button
    browse_but.grid(row=2, column=2, pady=10, padx=10)

    # create a download button
    download_but = Button(root, text="Download", command=download_video, width=15, bg="#ff0000")
    # place the button
    download_but.grid(row=3, column=1, pady=3, padx=3)

# define browse button function
def browse():
    # set directory
    downlaod_dir = filedialog.askdirectory(initialdir="Downlaod path")
    Download_path.set(downlaod_dir)

# create youtube video download function
def download_video():

    url = video_link.get()
    folder = Download_path.get()
    get_video = YouTube(url)
    get_stream = get_video.streams.first()
    get_stream.download(folder)

    messagebox.showinfo("Your video downloaded successfully")

# creating an instance
root = tk.Tk()

# size of the window
root.geometry("600x120")
root.resizable(False,False)
# name of the window
root.title("downloader")
# colors of the window
root.config(background="#999999")

video_link = StringVar()
Download_path = StringVar()

createwidgits()

root.mainloop()

Output:

Final Words

We discussed two methods that you can use to download Youtube videos using Python, in any quality to your computer in under a minute. The first one is a command-line-based downloader and another GUI-based version. This can be very useful for downloading online lecture videos, online tutorials, online conference videos, online conference talks, online training classes, and many more. You can copy and paste the code into your terminal.

Here are some useful tutorials that you can read:

Spread the love
 
   
Previous

Schedule Python Scripts with Apache Airflow

How to Update your Mac Address using Python

Next

Leave a Comment