Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- import vlc
- class VideoPlayer:
- def __init__(self, root):
- self.root = root
- self.instance = vlc.Instance()
- self.player = self.instance.media_player_new()
- self.setup_gui()
- self.read_playlist()
- self.current_channel_index = 0
- def setup_gui(self):
- self.root.title("NAJEEB IPTV PLAYER")
- self.root.configure(bg="#336699")
- self.root.geometry("800x600+300+50")
- self.frame = tk.Frame(root, bg="#336699")
- self.frame.pack(side="left", fill="both", expand=True)
- self.player_frame = tk.Frame(root, width=600, height=400, bg="#336699")
- self.player_frame.pack(side="right", fill="both", expand=True)
- self.status_label = tk.Label(self.frame, text="", fg="green", bg="#336699")
- self.status_label.pack()
- self.control_frame = tk.Frame(self.frame, bg="#336699")
- self.control_frame.pack(side="bottom", fill="x")
- self.play_button = tk.Button(self.control_frame, text="Play Station", command=self.play_video, bg="#FFA500", fg="white")
- self.play_button.pack(side="left", padx=5)
- self.stop_button = tk.Button(self.control_frame, text="Stop", command=self.stop_video, bg="#FF0000", fg="white")
- self.stop_button.pack(side="left", padx=5)
- self.lst_frame = tk.Frame(self.frame, bg="#336699")
- self.lst_frame.pack(side="bottom", fill="both", expand=True)
- self.lst = tk.Listbox(self.lst_frame, width=40, height=20, bg="white", selectbackground="#FFA500")
- self.lst.pack(side="left", fill="both", expand=True)
- self.scrollbar = tk.Scrollbar(self.lst_frame, orient="vertical", 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_channel)
- # Bind the destroy event to handle closing of the window
- self.root.bind("<Destroy>", self.on_window_close)
- def on_window_close(self, event):
- # Ensure that player is stopped and resources are released
- self.stop_video()
- def read_playlist(self):
- try:
- with open("RADIOS.m3u", "r", encoding="utf-8") as file:
- lines = file.readlines()
- for line in lines:
- if line.startswith("#EXTINF:"):
- channel_name = line.split(',')[-1].strip()
- self.lst.insert(tk.END, channel_name)
- except FileNotFoundError:
- self.status_label.config(text="Playlist not found", fg="red")
- def play_selected_channel(self, event):
- selection = self.lst.curselection()
- if selection:
- channel_index = selection[0]
- group_title = self.get_group_title(channel_index)
- if group_title:
- self.root.title(group_title)
- self.play_channel(channel_index)
- def get_group_title(self, channel_index):
- try:
- with open("RADIOS.m3u", "r", encoding="utf-8") as file:
- lines = file.readlines()
- for i, line in enumerate(lines):
- if line.startswith("#EXTINF:"):
- if i // 2 == channel_index:
- group_title_line = lines[i].strip()
- group_title = group_title_line.split('group-title="')[1].split('"')[0]
- return group_title
- except Exception as e:
- print("Error:", e)
- return None
- 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 play_channel(self, channel_index):
- try:
- with open("RADIOS.m3u", "r", encoding="utf-8") as file:
- lines = file.readlines()
- for i, line in enumerate(lines):
- if line.startswith("#EXTINF:"):
- if i // 2 == channel_index:
- stream_url = lines[i + 1].strip()
- media = self.instance.media_new(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")
- self.current_channel_index = channel_index
- return
- self.status_label.config(text="Channel not found", fg="red")
- except Exception as e:
- self.status_label.config(text="Failed to play", fg="red")
- root = tk.Tk()
- video_player = VideoPlayer(root)
- root.mainloop()
Add Comment
Please, Sign In to add comment