Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import ttk, messagebox, filedialog
- from pytube import YouTube
- from pydub import AudioSegment
- import os
- def update_progress(stream, chunk, bytes_remaining):
- total_size = stream.filesize
- bytes_downloaded = total_size - bytes_remaining
- percentage = bytes_downloaded / total_size * 100
- progress_var.set(percentage)
- root.update_idletasks()
- def download_video():
- url = url_entry.get()
- quality = quality_var.get()
- save_path = path_entry.get()
- if not url:
- messagebox.showerror("Error", "Please enter the YouTube URL")
- return
- try:
- yt = YouTube(url, on_progress_callback=update_progress)
- if quality == "Audio (MP3)":
- stream = yt.streams.filter(only_audio=True).first()
- out_file = stream.download(output_path=save_path)
- base, ext = os.path.splitext(out_file)
- mp3_file = base + '.mp3'
- AudioSegment.from_file(out_file).export(mp3_file, format="mp3")
- os.remove(out_file)
- messagebox.showinfo("Success", f"Downloaded and converted to MP3:\n{mp3_file}")
- else:
- stream = yt.streams.filter(progressive=True, res=quality, file_extension='mp4').first()
- if stream:
- stream.download(output_path=save_path)
- messagebox.showinfo("Success", f"Downloaded video in {quality}:\n{stream.default_filename}")
- else:
- messagebox.showerror("Error", f"No stream found for quality {quality}")
- except Exception as e:
- messagebox.showerror("Error", f"Failed to download video: {e}")
- def browse_path():
- folder_selected = filedialog.askdirectory()
- path_entry.delete(0, tk.END)
- path_entry.insert(0, folder_selected)
- # GUI Setup
- root = tk.Tk()
- root.title("Najeeb YouTube Video Downloader")
- # URL Entry
- url_label = tk.Label(root, text="YouTube URL:")
- url_label.grid(row=0, column=0, padx=10, pady=10)
- url_entry = tk.Entry(root, width=50)
- url_entry.grid(row=0, column=1, padx=10, pady=10)
- # Quality Selection
- quality_label = tk.Label(root, text="Select Quality:")
- quality_label.grid(row=1, column=0, padx=10, pady=10)
- quality_var = tk.StringVar(value="720p")
- quality_options = ["144p", "360p", "480p", "720p", "1080p", "Audio (MP3)"]
- quality_menu = ttk.Combobox(root, textvariable=quality_var, values=quality_options, state="readonly")
- quality_menu.grid(row=1, column=1, padx=10, pady=10)
- # Save Path
- path_label = tk.Label(root, text="Save to:")
- path_label.grid(row=2, column=0, padx=10, pady=10)
- path_entry = tk.Entry(root, width=50)
- path_entry.grid(row=2, column=1, padx=10, pady=10)
- browse_button = tk.Button(root, text="Browse", command=browse_path)
- browse_button.grid(row=2, column=2, padx=10, pady=10)
- # Progress Bar
- progress_var = tk.DoubleVar()
- progress_bar = ttk.Progressbar(root, variable=progress_var, maximum=100)
- progress_bar.grid(row=3, column=0, columnspan=3, padx=10, pady=20, sticky='ew')
- # Download Button
- download_button = tk.Button(root, text="Download", command=download_video, bg='lightblue')
- download_button.grid(row=4, column=1, padx=10, pady=20)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement