Advertisement
Najeebsk

YOUTUBE-VLC-DOWNLOAD3.py

Mar 16th, 2024 (edited)
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.56 KB | None | 0 0
  1. import tkinter as tk
  2. import vlc
  3. from youtube_search import YoutubeSearch
  4. from pytube import YouTube
  5. from pytube.exceptions import AgeRestrictedError
  6. import webbrowser
  7.  
  8. class VideoPlayer:
  9.     def __init__(self, root):
  10.         self.root = root
  11.         self.instance = vlc.Instance('--no-xlib')  # Create a VLC instance
  12.         self.player = self.instance.media_player_new()  # Create a media player
  13.         self.video_urls = {}  # Initialize video URL dictionary
  14.  
  15.         # Color Palette
  16.         self.bg_color = "#336699"
  17.         self.button_color = "#4CAF50"
  18.         self.text_color = "white"
  19.  
  20.         self.main_frame = tk.Frame(root, bg=self.bg_color)
  21.         self.main_frame.pack(fill="both", expand=True)
  22.  
  23.         self.title_label = tk.Label(self.main_frame, text="NAJEEB VIDEO PLAYER", font=("Arial", 24, "bold"), fg=self.text_color, bg=self.bg_color)
  24.         self.title_label.pack(pady=10)
  25.  
  26.         self.search_frame = tk.Frame(self.main_frame, bg=self.bg_color)
  27.         self.search_frame.pack(fill="x")
  28.  
  29.         self.search_entry = tk.Entry(self.search_frame)
  30.         self.search_entry.pack(side="left", fill="x", expand=True, padx=5, pady=5)
  31.  
  32.         self.search_button = tk.Button(self.search_frame, text="Search YouTube", command=self.search_and_play, bg=self.button_color, fg=self.text_color)
  33.         self.search_button.pack(side="left", padx=5, pady=5)
  34.  
  35.         self.save_button = tk.Button(self.search_frame, text="Save", command=self.save_results, bg=self.button_color, fg=self.text_color)
  36.         self.save_button.pack(side="left", padx=5, pady=5)
  37.  
  38.         self.list_frame = tk.Frame(self.main_frame, bg=self.bg_color)
  39.         self.list_frame.pack(side="left", fill="both", expand=False)
  40.  
  41.         self.button_frame = tk.Frame(self.main_frame, bg=self.bg_color)
  42.         self.button_frame.pack(side="bottom", fill="x")
  43.  
  44.         self.lst = tk.Listbox(self.list_frame, width=30, height=20, bg=self.bg_color, fg=self.text_color)
  45.         self.lst.pack(side="left", fill="both", expand=True)
  46.  
  47.         self.scrollbar = tk.Scrollbar(self.list_frame, orient="vertical", command=self.lst.yview)
  48.         self.scrollbar.pack(side="right", fill="y")
  49.         self.lst.config(yscrollcommand=self.scrollbar.set)
  50.  
  51.         self.page = 1
  52.         self.per_page = 10
  53.  
  54.         self.prev_button = tk.Button(self.button_frame, text="Prev", command=self.prev_page, bg=self.button_color, fg=self.text_color)
  55.         self.prev_button.pack(side="left", padx=5, pady=5)
  56.  
  57.         self.next_button = tk.Button(self.button_frame, text="Next", command=self.next_page, bg=self.button_color, fg=self.text_color)
  58.         self.next_button.pack(side="left", padx=5, pady=5)
  59.  
  60.         self.play_button = tk.Button(self.button_frame, text="Play", command=self.play_video, bg=self.button_color, fg=self.text_color)
  61.         self.play_button.pack(side="left", padx=5, pady=5)
  62.  
  63.         self.stop_button = tk.Button(self.button_frame, text="Stop", command=self.stop_video, bg=self.button_color, fg=self.text_color)
  64.         self.stop_button.pack(side="left", padx=5, pady=5)
  65.  
  66.         self.current_video_label = tk.Label(root, text="No video selected", bg=self.bg_color, fg=self.text_color)
  67.         self.current_video_label.pack(pady=5)
  68.  
  69.         self.player_frame = tk.Frame(self.main_frame)
  70.         self.player_frame.pack(side="right", fill="both", expand=True)
  71.  
  72.         self.lst.bind("<<ListboxSelect>>", self.show_video)
  73.  
  74.         # Load saved video list when the application starts
  75.         self.load_saved_results()
  76.  
  77.     def search_and_play(self):
  78.         query = self.search_entry.get()
  79.         search_results = YoutubeSearch(query, max_results=self.per_page*self.page).to_dict()
  80.         if search_results:
  81.             self.lst.delete(0, tk.END)
  82.             for result in search_results:
  83.                 video_title = result['title']
  84.                 video_url = 'https://www.youtube.com' + result['url_suffix']
  85.                 self.lst.insert(tk.END, video_title)
  86.                 self.video_urls[video_title] = video_url  # Store title and URL in dictionary
  87.             self.save_results()  # Save updated list of videos
  88.  
  89.     def play_video(self):
  90.         if self.lst.curselection():
  91.             title = self.lst.get(self.lst.curselection())
  92.             video_url = self.video_urls.get(title)  # Get URL from dictionary
  93.             print("Playing video:", video_url)  # Print URL for debugging
  94.  
  95.             try:
  96.                 # Use pytube to get the video URL
  97.                 yt = YouTube(video_url)
  98.                 video_url = yt.streams.filter(progressive=True, file_extension='mp4').first().url
  99.  
  100.                 media = self.instance.media_new(video_url)
  101.                 self.player.set_media(media)
  102.                 self.player.set_hwnd(self.player_frame.winfo_id())
  103.                 self.player.play()
  104.                 self.current_video_label.config(text=f"Playing: {title}")
  105.             except AgeRestrictedError:
  106.                 print("Age restricted video:", video_url)
  107.                 webbrowser.open(video_url)  # Open age-restricted videos in the default web browser
  108.             except Exception as e:
  109.                 print("Error:", e)
  110.                 self.current_video_label.config(text="An error occurred while playing the video.")
  111.         else:
  112.             print("No video selected.")
  113.  
  114.     def stop_video(self):
  115.         self.player.stop()
  116.         self.current_video_label.config(text="Video stopped")
  117.  
  118.     def save_results(self):
  119.         with open("YouTube_list.txt", "a", encoding="utf-8") as file:  # Specify UTF-8 encoding
  120.             for title, url in self.video_urls.items():
  121.                 file.write(f"{title}: {url}\n")
  122.  
  123.     def load_saved_results(self):
  124.         try:
  125.             with open("YouTube_list.txt", "r", encoding="utf-8") as file:  # Specify UTF-8 encoding
  126.                 for line in file:
  127.                     if ':' in line:
  128.                         title, url = line.strip().split(': ', 1)
  129.                         self.video_urls[title] = url
  130.                         self.lst.insert(tk.END, title)
  131.         except FileNotFoundError:
  132.             print("No saved results found.")
  133.  
  134.     def show_video(self, event):
  135.         if hasattr(self, "download_button"):
  136.             self.download_button.destroy()  # Remove existing download button if any
  137.  
  138.         if self.lst.curselection():
  139.             title = self.lst.get(self.lst.curselection())
  140.             video_url = self.video_urls.get(title)  # Get URL from dictionary
  141.  
  142.             # Display download button after stop button
  143.             self.download_button = tk.Button(self.button_frame, text="Download 360p", command=lambda: self.download_video(video_url), bg=self.button_color, fg=self.text_color)
  144.             self.download_button.pack(side="left", padx=5, pady=5, after=self.stop_button)
  145.  
  146.     def download_video(self, video_url):
  147.         try:
  148.             yt = YouTube(video_url)
  149.             stream = yt.streams.filter(res="360p", progressive=True).first()  # Filter for 360p quality
  150.             if stream:
  151.                 stream.download()
  152.                 print("Downloaded successfully.")
  153.             else:
  154.                 print("No 360p stream available for download.")
  155.         except Exception as e:
  156.             print("Error occurred during download:", e)
  157.  
  158.     def prev_page(self):
  159.         if self.page > 1:
  160.             self.page -= 1
  161.             self.search_and_play()
  162.  
  163.     def next_page(self):
  164.         self.page += 1
  165.         self.search_and_play()
  166.  
  167. root = tk.Tk()
  168. root.geometry("800x600+300+50")
  169. root.title("Najeeb VLC Player")
  170. root.configure(bg="#336699")
  171. video_player = VideoPlayer(root)
  172. root.mainloop()
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement