Advertisement
Najeebsk

YOUTUBE-SEARCH-PLAYHD.py

Mar 11th, 2024
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.79 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.  
  7. class VideoPlayer:
  8.     def __init__(self, root):
  9.         self.root = root
  10.         self.instance = vlc.Instance('--no-xlib')  # Create a VLC instance
  11.         self.player = self.instance.media_player_new()  # Create a media player
  12.         self.video_urls = {}  # Initialize video URL dictionary
  13.  
  14.         # Color Palette
  15.         self.bg_color = "#336699"
  16.         self.button_color = "#4CAF50"
  17.         self.text_color = "white"
  18.  
  19.         self.main_frame = tk.Frame(root, bg=self.bg_color)
  20.         self.main_frame.pack(fill="both", expand=True)
  21.  
  22.         self.title_label = tk.Label(self.main_frame, text="NAJEEB VIDEO PLAYER", font=("Arial", 24, "bold"), fg=self.text_color, bg=self.bg_color)
  23.         self.title_label.pack(pady=10)
  24.  
  25.         self.search_frame = tk.Frame(self.main_frame, bg=self.bg_color)
  26.         self.search_frame.pack(fill="x")
  27.  
  28.         self.search_entry = tk.Entry(self.search_frame)
  29.         self.search_entry.pack(side="left", fill="x", expand=True, padx=5, pady=5)
  30.  
  31.         self.search_button = tk.Button(self.search_frame, text="Search YouTube", command=self.search_and_play, bg=self.button_color, fg=self.text_color)
  32.         self.search_button.pack(side="left", padx=5, pady=5)
  33.  
  34.         self.save_button = tk.Button(self.search_frame, text="Save", command=self.save_results, bg=self.button_color, fg=self.text_color)
  35.         self.save_button.pack(side="left", padx=5, pady=5)
  36.  
  37.         self.list_frame = tk.Frame(self.main_frame, bg=self.bg_color)
  38.         self.list_frame.pack(side="left", fill="both", expand=False)
  39.  
  40.         self.button_frame = tk.Frame(self.main_frame, bg=self.bg_color)
  41.         self.button_frame.pack(side="bottom", fill="x")
  42.  
  43.         self.lst = tk.Listbox(self.list_frame, width=30, height=20, bg=self.bg_color, fg=self.text_color)
  44.         self.lst.pack(side="left", fill="both", expand=True)
  45.  
  46.         self.scrollbar = tk.Scrollbar(self.list_frame, orient="vertical", command=self.lst.yview)
  47.         self.scrollbar.pack(side="right", fill="y")
  48.         self.lst.config(yscrollcommand=self.scrollbar.set)
  49.  
  50.         self.play_button = tk.Button(self.button_frame, text="Play", command=self.play_video, bg=self.button_color, fg=self.text_color)
  51.         self.play_button.pack(side="left", padx=5, pady=5)
  52.  
  53.         self.stop_button = tk.Button(self.button_frame, text="Stop", command=self.stop_video, bg=self.button_color, fg=self.text_color)
  54.         self.stop_button.pack(side="left", padx=5, pady=5)
  55.  
  56.         self.current_video_label = tk.Label(root, text="No video selected", bg=self.bg_color, fg=self.text_color)
  57.         self.current_video_label.pack(pady=5)
  58.  
  59.         self.player_frame = tk.Frame(self.main_frame)
  60.         self.player_frame.pack(side="right", fill="both", expand=True)
  61.  
  62.         self.lst.bind("<<ListboxSelect>>", self.show_video)
  63.  
  64.         # Load saved video list when the application starts
  65.         self.load_saved_results()
  66.  
  67.     def search_and_play(self):
  68.         query = self.search_entry.get()
  69.         search_results = YoutubeSearch(query, max_results=5).to_dict()
  70.         if search_results:
  71.             self.lst.delete(0, tk.END)
  72.             for result in search_results:
  73.                 video_title = result['title']
  74.                 video_url = 'https://www.youtube.com' + result['url_suffix']
  75.                 self.lst.insert(tk.END, video_title)
  76.                 self.video_urls[video_title] = video_url  # Store title and URL in dictionary
  77.             self.save_results()  # Save updated list of videos
  78.  
  79.     def play_video(self):
  80.         if self.lst.curselection():
  81.             title = self.lst.get(self.lst.curselection())
  82.             video_url = self.video_urls.get(title)  # Get URL from dictionary
  83.             print("Playing video:", video_url)  # Print URL for debugging
  84.  
  85.             try:
  86.                 # Use pytube to get the video URL
  87.                 yt = YouTube(video_url)
  88.                 video_url = yt.streams.filter(progressive=True, file_extension='mp4').first().url
  89.  
  90.                 media = self.instance.media_new(video_url)
  91.                 self.player.set_media(media)
  92.                 self.player.set_hwnd(self.player_frame.winfo_id())
  93.                 self.player.play()
  94.                 self.current_video_label.config(text=f"Playing: {title}")
  95.             except AgeRestrictedError as e:
  96.                 print("Age restricted video:", e)
  97.                 self.current_video_label.config(text="This video is age-restricted.")
  98.             except Exception as e:
  99.                 print("Error:", e)
  100.                 self.current_video_label.config(text="An error occurred while playing the video.")
  101.         else:
  102.             print("No video selected.")
  103.  
  104.     def stop_video(self):
  105.         self.player.stop()
  106.         self.current_video_label.config(text="Video stopped")
  107.  
  108.     def save_results(self):
  109.         with open("YouTube_list.txt", "a") as file:  # Append mode to append new results
  110.             for title, url in self.video_urls.items():
  111.                 file.write(f"{title}: {url}\n")
  112.  
  113.     def load_saved_results(self):
  114.         try:
  115.             with open("YouTube_list.txt", "r") as file:
  116.                 for line in file:
  117.                     if ':' in line:
  118.                         title, url = line.strip().split(': ', 1)
  119.                         self.video_urls[title] = url
  120.                         self.lst.insert(tk.END, title)
  121.         except FileNotFoundError:
  122.             print("No saved results found.")
  123.  
  124.     def show_video(self, event):
  125.         # Add implementation to show the selected video
  126.         pass
  127.  
  128. root = tk.Tk()
  129. root.geometry("800x600+300+50")
  130. root.title("Najeeb VLC Player")
  131. root.configure(bg="#336699")
  132. video_player = VideoPlayer(root)
  133. root.mainloop()
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement