Advertisement
Nenogzar

YOUTUBE VIDEO PLAYER

Mar 16th, 2024
866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.48 KB | None | 0 0
  1. import tkinter as tk
  2. import os
  3. import webbrowser
  4. from youtube_search import YoutubeSearch
  5. from pytube import YouTube
  6.  
  7.  
  8. class VideoPlayer:
  9.     def __init__(self, root):
  10.         self.root = root
  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="YOUTUBE VIDEO PLAYER", font=("Arial", 24, "bold"),
  22.                                     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,
  32.                                        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,
  36.                                      fg=self.text_color)
  37.         self.save_button.pack(side="left", padx=5, pady=5)
  38.  
  39.         self.list_frame = tk.Frame(self.main_frame, bg=self.bg_color)
  40.         self.list_frame.pack(side="left", fill="both", expand=False)
  41.  
  42.         self.button_frame = tk.Frame(self.main_frame, bg=self.bg_color)
  43.         self.button_frame.pack(side="bottom", fill="x")
  44.  
  45.         self.lst = tk.Listbox(self.list_frame, width=30, height=20, bg=self.bg_color, fg=self.text_color)
  46.         self.lst.pack(side="left", fill="both", expand=True)
  47.  
  48.         self.scrollbar = tk.Scrollbar(self.list_frame, orient="vertical", command=self.lst.yview)
  49.         self.scrollbar.pack(side="right", fill="y")
  50.         self.lst.config(yscrollcommand=self.scrollbar.set)
  51.  
  52.         self.play_button = tk.Button(self.button_frame, text="Play", command=self.play_video, bg=self.button_color,
  53.                                      fg=self.text_color)
  54.         self.play_button.pack(side="left", padx=5, pady=5)
  55.  
  56.         self.stop_button = tk.Button(self.button_frame, text="Stop", command=self.stop_video, bg=self.button_color,
  57.                                      fg=self.text_color)
  58.         self.stop_button.pack(side="left", padx=5, pady=5)
  59.  
  60.         self.current_video_label = tk.Label(root, text="No video selected", bg=self.bg_color, fg=self.text_color)
  61.         self.current_video_label.pack(pady=5)
  62.  
  63.         self.player_frame = tk.Frame(self.main_frame)
  64.         self.player_frame.pack(side="right", fill="both", expand=True)
  65.  
  66.         self.lst.bind("<<ListboxSelect>>", self.show_video)
  67.  
  68.  
  69.         self.load_saved_results()
  70.  
  71.     def search_and_play(self):
  72.         query = self.search_entry.get()
  73.         search_results = YoutubeSearch(query, max_results=5).to_dict()
  74.         if search_results:
  75.             self.lst.delete(0, tk.END)
  76.             for result in search_results:
  77.                 video_title = result['title']
  78.                 video_url = 'https://www.youtube.com' + result['url_suffix']
  79.                 self.lst.insert(tk.END, video_title)
  80.                 self.video_urls[video_title] = video_url  # Store title and URL in dictionary
  81.             self.save_results()  # Save updated list of videos
  82.  
  83.     """ os play """
  84.     # def play_video(self):
  85.     #     if self.lst.curselection():
  86.     #         title = self.lst.get(self.lst.curselection())
  87.     #         video_url = self.video_urls.get(title)  # Get URL from dictionary
  88.     #         print("Playing video:", video_url)  # Print URL for debugging
  89.     #
  90.     #         # Отваряме URL адреса в стандартния браузър на Windows
  91.     #         os.system(f'start {video_url}')
  92.     #         self.current_video_label.config(text=f"Playing: {title}")
  93.     #     else:
  94.     #         print("No video selected.")
  95.  
  96.  
  97.     def play_video(self):
  98.         if self.lst.curselection():
  99.             title = self.lst.get(self.lst.curselection())
  100.             video_url = self.video_urls.get(title)  # Get URL from dictionary
  101.             print("Playing video:", video_url)  # Print URL for debugging
  102.  
  103.             webbrowser.open(video_url)
  104.             self.current_video_label.config(text=f"Playing: {title}")
  105.         else:
  106.             print("No video selected.")
  107.     def stop_video(self):
  108.         self.current_video_label.config(text="Video stopped")
  109.  
  110.     def save_results(self):
  111.         with open("YouTube_list.txt", "a") as file:
  112.             for title, url in self.video_urls.items():
  113.                 file.write(f"{title}: {url}\n")
  114.  
  115.     def load_saved_results(self):
  116.         try:
  117.             with open("YouTube_list.txt", "r") as file:
  118.                 for line in file:
  119.                     if ':' in line:
  120.                         title, url = line.strip().split(': ', 1)
  121.                         self.video_urls[title] = url
  122.                         self.lst.insert(tk.END, title)
  123.         except FileNotFoundError:
  124.             print("No saved results found.")
  125.  
  126.     def show_video(self, event):
  127.  
  128.         pass
  129.  
  130. root = tk.Tk()
  131. root.geometry("800x600+300+50")
  132. root.title("YouTube Player")
  133. root.configure(bg="#336699")
  134. video_player = VideoPlayer(root)
  135. root.mainloop()
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement