Home Developer Programming Language Framework Articles Contact Me

HTML CSS JAVASCRIPT JAVA PYTHON JQUERY REACTJS VUEJS ANGULARJS EMBERJS SASS LESS STYLUS GITHUB GITLAB BIGBUCKET JAVA KOTLIN HADOOP JDBC SERVLETS JAVA BEANS(EJB) JAVA SERVER PAGES(JSP) UNIX LINUX POSTGRESQL MSSQLSERVER SQLITE MONGODB XML FLASK PYTHON MYSQL JAVA C++ JUNIT SELENIUM

Download Selected Video From YouTube Playlist Using Python

Prerequisite:-

In this article, we will learn, how to download only selected videos from YouTube Playlist Using Tkinter in Python.

Libraries Required:-

  1. Tkinter
  2. Pyyoutube
  3. Pytube
  4. Youtube Data API

Installation:-

pip install python-youtube
pip install pytube
pip install google-api-python-client

Approach:-

  1. First, we will fetch all video link from the youtube playlist using the pyyoutube Module
  2. Then we will select the video link, which we want to download
  3. Then we will download each video one by one using pytube Module
Below is how the GUI looks like:-

Before we get started first we will discuss pyyoutube module. pyyoutube provides an easy way to use YouTube Data API V3.

Let's Understand Step by Step Implementation:-

Step 1(Create a Tkinter window and add Buttons, Labels, Scrollbar, etc...)

# Import Required Modules
from tkinter import *

# Create Object
root = Tk() 
# Set geometry
root.geometry('400x400') 

# Add Label
Label(root, text="Youtube Playlist Downloader", font="italic 15 bold").pack(pady=10)
Label(root, text="Enter Playlist URL:-", font="italic 10").pack()

# Add Entry box
playlistId = Entry(root, width=60)
playlistId.pack(pady=5)

# Add Button
get_videos = Button(root,text="Get Videos")
get_videos.pack(pady=10)

# Add Scollbar
scrollbar = Scrollbar(root)
scrollbar.pack(side = RIGHT, fill = BOTH)
list_box = Listbox(root, selectmode = "multiple")
list_box.pack(expand = YES, fill = "both") 
list_box.config(yscrollcommand = scrollbar.set)
scrollbar.config(command = list_box.yview) 

download_start = Button(root, text="Download Start" , state=DISABLED)
download_start.pack(pady=10)

# Execute Tkinter
root.mainloop()

Step 2 (Define Functions)

Now we will create three functions;get_list_videos(), threading() and download_videos()

def get_list_videos():
  global playlist_item_by_id
  # Clear ListBox
  list_box.delete(0,'end')

  # Create API Object
  api = Api(api_key='Enter API Key')

  if "youtube" in playlistId.get():
    playlist_id = playlistId.get()[len("https://www.youtube.com/playlist?list="):]
  else:
    playlist_id = playlistId.get()

  # Get list of video links
  playlist_item_by_id = api.get_playlist_items(playlist_id=playlist_id ,count=None,return_json=True)

  # Iterate through all video links and insert into listbox
  for index, videoid in enumerate(playlist_item_by_id['items']):
    list_box.insert(END, f" {str(index+1)}. {videoid['contentDetails']['videoId']}")

  download_start.config(state=NORMAL)

def threading(): 
    # Call download_videos function 
    t1=Thread(target=download_videos) 
    t1.start() 

def download_videos():
  download_start.config(state="disabled")
  get_videos.config(state="disabled")

  # Iterate through all selected videos
  for i in list_box.curselection():
    videoid = playlist_item_by_id['items'][i]['contentDetails']['videoId']
    
    link=f"https://www.youtube.com/watch?v={videoid}"

    yt_obj = YouTube(link)

    filters = yt_obj.streams.filter(progressive=True, file_extension='mp4')

    # download the highest quality video
    filters.get_highest_resolution().download()

  messagebox.showinfo("Success", "Video Successfully downloaded")
  download_start.config(state="normal")
  get_videos.config(state="normal")

Below is the Implementation:-

# Import Required Modules
from tkinter import *
from pyyoutube import Api
from pytube import YouTube 
from threading import Thread
from tkinter import messagebox 

def get_list_videos():
  global playlist_item_by_id
  # Clear ListBox
  list_box.delete(0,'end')

  # Create API Object
  api = Api(api_key='Enter API Key')

  if "youtube" in playlistId.get():
    playlist_id = playlistId.get()[len("https://www.youtube.com/playlist?list="):]
  else:
    playlist_id = playlistId.get()

  # Get list of video links
  playlist_item_by_id = api.get_playlist_items(playlist_id=playlist_id ,count=None,return_json=True)

  # Iterate through all video links and insert into listbox
  for index, videoid in enumerate(playlist_item_by_id['items']):
    list_box.insert(END, f" {str(index+1)}. {videoid['contentDetails']['videoId']}")

  download_start.config(state=NORMAL)

def threading(): 
    # Call download_videos function 
    t1=Thread(target=download_videos) 
    t1.start() 

def download_videos():
  download_start.config(state="disabled")
  get_videos.config(state="disabled")

  # Iterate through all selected videos
  for i in list_box.curselection():
    videoid = playlist_item_by_id['items'][i]['contentDetails']['videoId']
    
    link=f"https://www.youtube.com/watch?v={videoid}"

    yt_obj = YouTube(link)

    filters = yt_obj.streams.filter(progressive=True, file_extension='mp4')

    # download the highest quality video
    filters.get_highest_resolution().download()

  messagebox.showinfo("Success", "Video Successfully downloaded")
  download_start.config(state="normal")
  get_videos.config(state="normal")

# Create Object
root = Tk() 
# Set geometry
root.geometry('400x400') 

# Add Label
Label(root, text="Youtube Playlist Downloader", font="italic 15 bold").pack(pady=10)
Label(root, text="Enter Playlist URL:-", font="italic 10").pack()

# Add Entry box
playlistId = Entry(root, width=60)
playlistId.pack(pady=5)

# Add Button
get_videos = Button(root,text="Get Videos",command=get_list_videos)
get_videos.pack(pady=10)

# Add Scollbar
scrollbar = Scrollbar(root)
scrollbar.pack(side = RIGHT, fill = BOTH)
list_box = Listbox(root, selectmode = "multiple")
list_box.pack(expand = YES, fill = "both") 
list_box.config(yscrollcommand = scrollbar.set)
scrollbar.config(command = list_box.yview) 

download_start = Button(root, text="Download Start", command=threading , state=DISABLED)
download_start.pack(pady=10)

# Execute Tkinter
root.mainloop()

Output:-