-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube_download.py
120 lines (85 loc) · 3.33 KB
/
youtube_download.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'''
Youtube Downloader
Current functionalities :
Accepts user URL,
Checks whether it is valid, otherwise gives error message,
Download either as video, or mp3
Select download path
Possible additions:
User option to specify file name
'''
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
from pytube import YouTube
from PIL import ImageTk
import os
#default download directory
file1 = "C:/Users/HP/Downloads"
root = Tk()
root.geometry('500x300+550+250')
root.resizable(0,0)
root.title("YouTube Video Downloader")
background_image=ImageTk.PhotoImage(file = "C:\\Users\\HP\\Desktop\\Python programs\\1353663.jpg")
background_label = Label(root, image =background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
heading = Label(root,text = 'Youtube Video Downloader', font ='arial 20 bold',bg = "yellow")
heading.pack(pady = 10)
##enter link
link = StringVar()
paste = Label(root, text = 'Paste Link Here:', font = 'arial 15',bg = "yellow") #.place(x= 160 , y = 60)
paste.place(x= 160 , y = 60)
link_enter = Entry(root, width = 70,textvariable = link) #.place(x = 32, y = 100)
link_enter.place(x = 32, y = 100)
#function to download video
def Downloader():
try:
url =YouTube(str(link.get()))
video = url.streams.first()
video.download(output_path=file1) # File path where downloaded file is stored
messagebox.showinfo("Pop up", "Download complete!")
except:
error()
#function to download mp3
def get_audio():
try:
url =YouTube(str(link.get()))
video = url.streams.filter(only_audio=True).first()
out_file = video.download(output_path=file1) # File path where downloaded file is stored
base, ext = os.path.splitext(out_file)
new_file = base + '.mp3'
os.rename(out_file, new_file)
messagebox.showinfo("Pop up", "Download complete!")
except:
error()
#function to get the title of the video
def get_title():
try:
url =YouTube(str(link.get()))
video_title = url.title
messagebox.showinfo("Video Title", video_title)
except:
error()
#function invoked when incorrect url is entered
def error():
messagebox.showinfo("ERROR", "Video not found! Check URL and try again.")
#function invoked for download path selection
def path_selection():
global file1
file1 = filedialog.askdirectory()
if file1 == '':
file1 = "C:/Users/HP/Downloads"
messagebox.showinfo("Download Path", "Current Download Path : " + file1)
#button to check title
check_title = Button(root,text = 'Check Video Title', font = 'arial' ,bg = 'yellow', command = get_title)
check_title.place(x=10 ,y = 130)
#button to download video
download = Button(root,text = 'Download', font = 'arial 15' ,bg = 'yellow', padx = 2, command = Downloader)
download.place(x=182 ,y = 130)
#button to download mp3
audio_download = Button(root,text = 'Download as mp3', font = 'arial' ,bg = 'yellow', padx = 2, command = get_audio)
audio_download.place(x=288 ,y = 130)
#option to select download path
download_path = Button(root,text = 'Select file path', font = 'arial', bg = 'yellow', padx = 2, command = path_selection)
download_path.place(x = 165, y = 180)
root.mainloop()