Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- import vlc
- from pytube import YouTube
- from threading import Thread
- class VideoPlayer:
- def __init__(self, root):
- self.root = root
- self.instance = vlc.Instance('--no-xlib') # Add this line for Windows support
- self.player = self.instance.media_player_new()
- self.frame = tk.Frame(root)
- self.frame.pack(side="left", fill="both", expand=True)
- # Title Label
- self.title_label = tk.Label(self.frame, text="Najeeb's YouTube Player", font=("Helvetica", 16, "bold"))
- self.title_label.pack()
- self.player_frame = tk.Frame(root, width=800, height=600) # Video frame size
- self.player_frame.pack(side="right", fill="both", expand=True)
- self.status_label = tk.Label(self.frame, text="", fg="green")
- self.status_label.pack()
- self.control_frame = tk.Frame(self.frame)
- self.control_frame.pack(side="bottom", fill="x")
- self.play_button = tk.Button(self.control_frame, text="Play", command=self.play_video)
- self.play_button.pack(side="left", padx=5)
- self.stop_button = tk.Button(self.control_frame, text="Stop", command=self.stop_video)
- self.stop_button.pack(side="left", padx=5)
- self.fullscreen_button = tk.Button(self.control_frame, text="Full Screen", command=self.toggle_fullscreen)
- self.fullscreen_button.pack(side="left", padx=5)
- self.time_slider = tk.Scale(self.control_frame, from_=0, to=100, orient="horizontal", command=self.set_video_position)
- self.time_slider.pack(side="left", fill="x", expand=True)
- self.lst = tk.Listbox(self.frame, width=15, height=20)
- self.lst.pack(side="bottom", fill="both", expand=True)
- self.scrollbar = tk.Scrollbar(self.lst, orient="vertical")
- self.scrollbar.config(command=self.lst.yview)
- self.scrollbar.pack(side="right", fill="y")
- self.lst.config(yscrollcommand=self.scrollbar.set)
- self.lst.bind("<<ListboxSelect>>", self.play_selected_video)
- self.read_youtube_list()
- self.player.event_manager().event_attach(vlc.EventType.MediaPlayerTimeChanged, self.update_time_slider)
- def read_youtube_list(self):
- try:
- with open("YOUTUBE-LIST.txt", "r") as file:
- urls = file.readlines()
- for url in urls:
- parts = url.strip().split("=")
- if len(parts) >= 2:
- name = parts[0]
- self.lst.insert(tk.END, name)
- except FileNotFoundError:
- self.status_label.config(text="YOUTUBE-LIST.txt not found", fg="red")
- def play_selected_video(self, event):
- selection = self.lst.curselection()
- if selection:
- name = self.lst.get(selection[0])
- self.play_youtube_video(name)
- def play_video(self):
- self.player.play()
- self.status_label.config(text="Playing", fg="green")
- def stop_video(self):
- self.player.stop()
- self.status_label.config(text="Stopped", fg="red")
- def set_video_position(self, value):
- self.player.set_position(float(value) / 100)
- def update_time_slider(self, event):
- position = self.player.get_position()
- self.time_slider.set(int(position * 100))
- def play_youtube_video(self, name):
- self.status_label.config(text="Loading...")
- self.root.update()
- video_thread = Thread(target=self.play_youtube_video_thread, args=(name,))
- video_thread.start()
- def play_youtube_video_thread(self, name):
- try:
- with open("YOUTUBE-LIST.txt", "r") as file:
- urls = file.readlines()
- for url in urls:
- parts = url.strip().split("=")
- if len(parts) >= 2 and parts[0] == name:
- link = "=".join(parts[1:])
- video = YouTube(link)
- video_stream = video.streams.filter(res="360p").first()
- media = self.instance.media_new(video_stream.url)
- self.player.set_media(media)
- if self.player_frame.winfo_children():
- self.player_frame.winfo_children()[0].destroy()
- self.player_frame.update()
- self.player.set_hwnd(self.player_frame.winfo_id())
- self.player.play()
- self.status_label.config(text="Playing", fg="green")
- return
- self.status_label.config(text="Video not found", fg="red")
- except Exception as e:
- self.status_label.config(text="Failed to play", fg="red")
- def toggle_fullscreen(self):
- if self.root.attributes('-fullscreen'):
- self.root.attributes('-fullscreen', False)
- self.fullscreen_button.config(text="Full Screen")
- else:
- self.root.attributes('-fullscreen', True)
- self.fullscreen_button.config(text="Exit Full Screen")
- root = tk.Tk()
- root.title("NAJEEB YOUTUBE PLAYER")
- root.geometry("800x600+300+50")
- video_player = VideoPlayer(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement