|
| 1 | +import os |
| 2 | +from time import sleep |
| 3 | +import tkinter as tk |
| 4 | +from PIL import Image, ImageTk |
| 5 | +from threading import Thread |
| 6 | + |
| 7 | +# initialize tkinter GUI |
| 8 | +root = tk.Tk() |
| 9 | +root.title("YouTube Downloader") |
| 10 | + |
| 11 | +# Global variables to save YT Downloader parameters |
| 12 | +ytParams = "" |
| 13 | + |
| 14 | +canvas = tk.Canvas(root, width=650, height=120) |
| 15 | +canvas.grid(columnspan=3, rowspan=5) |
| 16 | + |
| 17 | +# YouTube Downloader - title |
| 18 | +label = tk.Label(root, text="YouTube Downloader") |
| 19 | +label.grid(columnspan=3, row=0, pady=30) |
| 20 | +label.configure(font=("Courier", 28, "bold")) |
| 21 | + |
| 22 | +img = Image.open('images/youtube-logo.png') |
| 23 | +img = ImageTk.PhotoImage(img) |
| 24 | +img_label = tk.Label(image=img) |
| 25 | +img_label.image = img |
| 26 | +img_label.grid(columnspan=3, row=1) |
| 27 | + |
| 28 | +descTxt = "Download your favorite YouTube videos by entering a URL below." |
| 29 | +# Brief tool explanation |
| 30 | +desc = tk.Label(root, text=descTxt) |
| 31 | +desc.grid(columnspan=3, row=2, pady=10) |
| 32 | +desc.configure(font=("Courier", 14)) |
| 33 | + |
| 34 | +# Prompts the user to enter a YouTube URL |
| 35 | +urlLabel = tk.Label(text="YouTube URL:") |
| 36 | +urlLabel.grid(column=0, row=3, pady=20) |
| 37 | +urlLabel.configure(font=("Courier", 16)) |
| 38 | + |
| 39 | +urlEntry = tk.Entry(root, width=45, borderwidth=2) |
| 40 | +urlEntry.grid(column=1, row=3, pady=20) |
| 41 | + |
| 42 | +# Prompts the user to enter a download path if necessary |
| 43 | +pathLabel = tk.Label(text="Download Path:") |
| 44 | +pathLabel.grid(column=0, row=4, pady=20) |
| 45 | +pathLabel.configure(font=("Courier", 16)) |
| 46 | + |
| 47 | +pathEntry = tk.Entry(root, width=45, borderwidth=2) |
| 48 | +pathEntry.grid(column=1, row=4, pady=20) |
| 49 | + |
| 50 | +downloadButton = tk.StringVar() |
| 51 | +downloadButton.set("Download Video") |
| 52 | + |
| 53 | + |
| 54 | +# Function to alert user that the video is downloading. |
| 55 | +class first(Thread): |
| 56 | + def run(self): |
| 57 | + downloadButton.set("Downloading... Please Wait") |
| 58 | + sleep(2) |
| 59 | + |
| 60 | + |
| 61 | +# Function to run youtube_downloader python script. |
| 62 | +class second(Thread): |
| 63 | + def run(self): |
| 64 | + command = "python ../youtube_downloader/ytdownloader.py " |
| 65 | + url = urlEntry.get() |
| 66 | + ytPath = "" |
| 67 | + |
| 68 | + path = pathEntry.get() |
| 69 | + if path: |
| 70 | + ytPath = " -p " + path |
| 71 | + |
| 72 | + if not ytPath: |
| 73 | + runPy = command + " " + url |
| 74 | + else: |
| 75 | + runPy = command + " " + url + ytPath |
| 76 | + os.system(runPy) |
| 77 | + |
| 78 | + downloadButton.set("Download Video") |
| 79 | + |
| 80 | + |
| 81 | +# Call each function so the class values execute in order. |
| 82 | +def downloadVideo(): |
| 83 | + first().start() |
| 84 | + second().start() |
| 85 | + |
| 86 | + |
| 87 | +getVideo = tk.Button(root, textvariable=downloadButton, |
| 88 | + fg="red", command=downloadVideo, |
| 89 | + font=("Courier", 15), height=2) |
| 90 | +getVideo.grid(columnspan=3, row=6, pady=10) |
| 91 | + |
| 92 | + |
| 93 | +root.mainloop() |
0 commit comments