Advertisement
Najeebsk

YOUTUBE-PLAYER.py

Feb 29th, 2024
1,171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.23 KB | None | 0 0
  1. import tkinter as tk
  2. import vlc
  3. from pytube import YouTube
  4. from threading import Thread
  5.  
  6. class VideoPlayer:
  7.     def __init__(self, root):
  8.         self.root = root
  9.         self.instance = vlc.Instance('--no-xlib')  # Add this line for Windows support
  10.         self.player = self.instance.media_player_new()
  11.  
  12.         self.frame = tk.Frame(root)
  13.         self.frame.pack(side="left", fill="both", expand=True)
  14.  
  15.          # Title Label
  16.         self.title_label = tk.Label(self.frame, text="Najeeb's YouTube Player", font=("Helvetica", 16, "bold"))
  17.         self.title_label.pack()
  18.  
  19.  
  20.         self.player_frame = tk.Frame(root, width=800, height=600)  # Video frame size
  21.         self.player_frame.pack(side="right", fill="both", expand=True)
  22.  
  23.         self.status_label = tk.Label(self.frame, text="", fg="green")
  24.         self.status_label.pack()
  25.  
  26.         self.control_frame = tk.Frame(self.frame)
  27.         self.control_frame.pack(side="bottom", fill="x")
  28.  
  29.         self.play_button = tk.Button(self.control_frame, text="Play", command=self.play_video)
  30.         self.play_button.pack(side="left", padx=5)
  31.  
  32.         self.stop_button = tk.Button(self.control_frame, text="Stop", command=self.stop_video)
  33.         self.stop_button.pack(side="left", padx=5)
  34.  
  35.         self.fullscreen_button = tk.Button(self.control_frame, text="Full Screen", command=self.toggle_fullscreen)
  36.         self.fullscreen_button.pack(side="left", padx=5)
  37.  
  38.         self.time_slider = tk.Scale(self.control_frame, from_=0, to=100, orient="horizontal", command=self.set_video_position)
  39.         self.time_slider.pack(side="left", fill="x", expand=True)
  40.  
  41.         self.lst = tk.Listbox(self.frame, width=15, height=20)
  42.         self.lst.pack(side="bottom", fill="both", expand=True)
  43.  
  44.         self.scrollbar = tk.Scrollbar(self.lst, orient="vertical")
  45.         self.scrollbar.config(command=self.lst.yview)
  46.         self.scrollbar.pack(side="right", fill="y")
  47.  
  48.         self.lst.config(yscrollcommand=self.scrollbar.set)
  49.  
  50.         self.lst.bind("<<ListboxSelect>>", self.play_selected_video)
  51.         self.read_youtube_list()
  52.  
  53.         self.player.event_manager().event_attach(vlc.EventType.MediaPlayerTimeChanged, self.update_time_slider)
  54.  
  55.     def read_youtube_list(self):
  56.         try:
  57.             with open("YOUTUBE-LIST.txt", "r") as file:
  58.                 urls = file.readlines()
  59.                 for url in urls:
  60.                     parts = url.strip().split("=")
  61.                     if len(parts) >= 2:  
  62.                         name = parts[0]
  63.                         self.lst.insert(tk.END, name)
  64.         except FileNotFoundError:
  65.             self.status_label.config(text="YOUTUBE-LIST.txt not found", fg="red")
  66.  
  67.     def play_selected_video(self, event):
  68.         selection = self.lst.curselection()
  69.         if selection:
  70.             name = self.lst.get(selection[0])
  71.             self.play_youtube_video(name)
  72.  
  73.     def play_video(self):
  74.         self.player.play()
  75.         self.status_label.config(text="Playing", fg="green")
  76.  
  77.     def stop_video(self):
  78.         self.player.stop()
  79.         self.status_label.config(text="Stopped", fg="red")
  80.  
  81.     def set_video_position(self, value):
  82.         self.player.set_position(float(value) / 100)
  83.  
  84.     def update_time_slider(self, event):
  85.         position = self.player.get_position()
  86.         self.time_slider.set(int(position * 100))
  87.  
  88.     def play_youtube_video(self, name):
  89.         self.status_label.config(text="Loading...")
  90.         self.root.update()
  91.         video_thread = Thread(target=self.play_youtube_video_thread, args=(name,))
  92.         video_thread.start()
  93.  
  94.     def play_youtube_video_thread(self, name):
  95.         try:
  96.             with open("YOUTUBE-LIST.txt", "r") as file:
  97.                 urls = file.readlines()
  98.                 for url in urls:
  99.                     parts = url.strip().split("=")
  100.                     if len(parts) >= 2 and parts[0] == name:
  101.                         link = "=".join(parts[1:])  
  102.                         video = YouTube(link)
  103.                         video_stream = video.streams.filter(res="360p").first()
  104.                         media = self.instance.media_new(video_stream.url)
  105.                         self.player.set_media(media)
  106.  
  107.                         if self.player_frame.winfo_children():
  108.                             self.player_frame.winfo_children()[0].destroy()
  109.  
  110.                         self.player_frame.update()
  111.                         self.player.set_hwnd(self.player_frame.winfo_id())
  112.                         self.player.play()
  113.  
  114.                         self.status_label.config(text="Playing", fg="green")
  115.                         return
  116.  
  117.             self.status_label.config(text="Video not found", fg="red")
  118.  
  119.         except Exception as e:
  120.             self.status_label.config(text="Failed to play", fg="red")
  121.  
  122.     def toggle_fullscreen(self):
  123.         if self.root.attributes('-fullscreen'):
  124.             self.root.attributes('-fullscreen', False)
  125.             self.fullscreen_button.config(text="Full Screen")
  126.         else:
  127.             self.root.attributes('-fullscreen', True)
  128.             self.fullscreen_button.config(text="Exit Full Screen")
  129.  
  130. root = tk.Tk()
  131. root.title("NAJEEB YOUTUBE PLAYER")
  132. root.geometry("800x600+300+50")
  133. video_player = VideoPlayer(root)
  134. root.mainloop()
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement