Download Youtube Video with Python
we will discuss how we can create a GUI application to download a YouTube video or a complete YouTube playlist using python.
Before we get started first we will discuss pyyoutube module. The pyyoutube module provides an easy way to use the YouTube Data API V3.
Installation:
pip install python-youtube
Download Selected Video From YouTube Playlist
In this section, we will learn, how to download only selected videos from YouTube Playlist Using Tkinter in Python.
Approach:
- First, we will fetch all video link from the YouTube playlist using the pyyoutube module.
- Then we will select the video link, which we want to download.
- Then we will download each video one by one using pytube module.
Let’s Understand step by step implementation:
- 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 Scorllbar
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()
- Now we will create three functions:
- get_list_videos:- It will give a list of all video links of a YouTube playlist.
- threading:- It is used for threading in Tkinter.
- download_videos:- It is used for downloading YouTube video.
CODE
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 complete 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 Scrollbar
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()
0 Comments