Advertisement
Najeebsk

Youtube.pyw

Jun 1st, 2024
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.16 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk, messagebox, filedialog
  3. from pytube import YouTube
  4. from pydub import AudioSegment
  5. import os
  6.  
  7. def update_progress(stream, chunk, bytes_remaining):
  8.     total_size = stream.filesize
  9.     bytes_downloaded = total_size - bytes_remaining
  10.     percentage = bytes_downloaded / total_size * 100
  11.     progress_var.set(percentage)
  12.     root.update_idletasks()
  13.  
  14. def download_video():
  15.     url = url_entry.get()
  16.     quality = quality_var.get()
  17.     save_path = path_entry.get()
  18.  
  19.     if not url:
  20.         messagebox.showerror("Error", "Please enter the YouTube URL")
  21.         return
  22.  
  23.     try:
  24.         yt = YouTube(url, on_progress_callback=update_progress)
  25.         if quality == "Audio (MP3)":
  26.             stream = yt.streams.filter(only_audio=True).first()
  27.             out_file = stream.download(output_path=save_path)
  28.             base, ext = os.path.splitext(out_file)
  29.             mp3_file = base + '.mp3'
  30.             AudioSegment.from_file(out_file).export(mp3_file, format="mp3")
  31.             os.remove(out_file)
  32.             messagebox.showinfo("Success", f"Downloaded and converted to MP3:\n{mp3_file}")
  33.         else:
  34.             stream = yt.streams.filter(progressive=True, res=quality, file_extension='mp4').first()
  35.             if stream:
  36.                 stream.download(output_path=save_path)
  37.                 messagebox.showinfo("Success", f"Downloaded video in {quality}:\n{stream.default_filename}")
  38.             else:
  39.                 messagebox.showerror("Error", f"No stream found for quality {quality}")
  40.     except Exception as e:
  41.         messagebox.showerror("Error", f"Failed to download video: {e}")
  42.  
  43. def browse_path():
  44.     folder_selected = filedialog.askdirectory()
  45.     path_entry.delete(0, tk.END)
  46.     path_entry.insert(0, folder_selected)
  47.  
  48. # GUI Setup
  49. root = tk.Tk()
  50. root.title("Najeeb YouTube Video Downloader")
  51.  
  52. # URL Entry
  53. url_label = tk.Label(root, text="YouTube URL:")
  54. url_label.grid(row=0, column=0, padx=10, pady=10)
  55. url_entry = tk.Entry(root, width=50)
  56. url_entry.grid(row=0, column=1, padx=10, pady=10)
  57.  
  58. # Quality Selection
  59. quality_label = tk.Label(root, text="Select Quality:")
  60. quality_label.grid(row=1, column=0, padx=10, pady=10)
  61. quality_var = tk.StringVar(value="720p")
  62. quality_options = ["144p", "360p", "480p", "720p", "1080p", "Audio (MP3)"]
  63. quality_menu = ttk.Combobox(root, textvariable=quality_var, values=quality_options, state="readonly")
  64. quality_menu.grid(row=1, column=1, padx=10, pady=10)
  65.  
  66. # Save Path
  67. path_label = tk.Label(root, text="Save to:")
  68. path_label.grid(row=2, column=0, padx=10, pady=10)
  69. path_entry = tk.Entry(root, width=50)
  70. path_entry.grid(row=2, column=1, padx=10, pady=10)
  71. browse_button = tk.Button(root, text="Browse", command=browse_path)
  72. browse_button.grid(row=2, column=2, padx=10, pady=10)
  73.  
  74. # Progress Bar
  75. progress_var = tk.DoubleVar()
  76. progress_bar = ttk.Progressbar(root, variable=progress_var, maximum=100)
  77. progress_bar.grid(row=3, column=0, columnspan=3, padx=10, pady=20, sticky='ew')
  78.  
  79. # Download Button
  80. download_button = tk.Button(root, text="Download", command=download_video, bg='lightblue')
  81. download_button.grid(row=4, column=1, padx=10, pady=20)
  82.  
  83. root.mainloop()
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement