Advertisement
Najeebsk

YOUTUBE-SEARCH-PLAY.py

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