Najeebsk

RADIOS-VIDEOS.pyw

Mar 21st, 2024
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.09 KB | None | 0 0
  1. import tkinter as tk
  2. import vlc
  3.  
  4. class VideoPlayer:
  5.     def __init__(self, root):
  6.         self.root = root
  7.         self.instance = vlc.Instance()  
  8.         self.player = self.instance.media_player_new()
  9.  
  10.         self.setup_gui()
  11.  
  12.         self.read_playlist()
  13.         self.current_channel_index = 0
  14.  
  15.     def setup_gui(self):
  16.         self.root.title("NAJEEB IPTV PLAYER")
  17.         self.root.configure(bg="#336699")
  18.         self.root.geometry("800x600+300+50")
  19.  
  20.         self.frame = tk.Frame(root, bg="#336699")
  21.         self.frame.pack(side="left", fill="both", expand=True)
  22.  
  23.         self.player_frame = tk.Frame(root, width=600, height=400, bg="#336699")  
  24.         self.player_frame.pack(side="right", fill="both", expand=True)
  25.  
  26.         self.status_label = tk.Label(self.frame, text="", fg="green", bg="#336699")
  27.         self.status_label.pack()
  28.  
  29.         self.control_frame = tk.Frame(self.frame, bg="#336699")
  30.         self.control_frame.pack(side="bottom", fill="x")
  31.  
  32.         self.play_button = tk.Button(self.control_frame, text="Play Station", command=self.play_video, bg="#FFA500", fg="white")
  33.         self.play_button.pack(side="left", padx=5)
  34.  
  35.         self.stop_button = tk.Button(self.control_frame, text="Stop", command=self.stop_video, bg="#FF0000", fg="white")
  36.         self.stop_button.pack(side="left", padx=5)
  37.  
  38.         self.lst_frame = tk.Frame(self.frame, bg="#336699")
  39.         self.lst_frame.pack(side="bottom", fill="both", expand=True)
  40.  
  41.         self.lst = tk.Listbox(self.lst_frame, width=40, height=20, bg="white", selectbackground="#FFA500")  
  42.         self.lst.pack(side="left", fill="both", expand=True)
  43.  
  44.         self.scrollbar = tk.Scrollbar(self.lst_frame, orient="vertical", command=self.lst.yview)
  45.         self.scrollbar.pack(side="right", fill="y")
  46.  
  47.         self.lst.config(yscrollcommand=self.scrollbar.set)
  48.  
  49.         self.lst.bind("<<ListboxSelect>>", self.play_selected_channel)
  50.  
  51.         # Bind the destroy event to handle closing of the window
  52.         self.root.bind("<Destroy>", self.on_window_close)
  53.  
  54.     def on_window_close(self, event):
  55.         # Ensure that player is stopped and resources are released
  56.         self.stop_video()
  57.  
  58.     def read_playlist(self):
  59.         try:
  60.             with open("RADIOS.m3u", "r", encoding="utf-8") as file:  
  61.                 lines = file.readlines()
  62.                 for line in lines:
  63.                     if line.startswith("#EXTINF:"):
  64.                         channel_name = line.split(',')[-1].strip()  
  65.                         self.lst.insert(tk.END, channel_name)
  66.         except FileNotFoundError:
  67.             self.status_label.config(text="Playlist not found", fg="red")
  68.  
  69.     def play_selected_channel(self, event):
  70.         selection = self.lst.curselection()
  71.         if selection:
  72.             channel_index = selection[0]
  73.             group_title = self.get_group_title(channel_index)
  74.             if group_title:
  75.                 self.root.title(group_title)
  76.             self.play_channel(channel_index)
  77.  
  78.     def get_group_title(self, channel_index):
  79.         try:
  80.             with open("RADIOS.m3u", "r", encoding="utf-8") as file:  
  81.                 lines = file.readlines()
  82.                 for i, line in enumerate(lines):
  83.                     if line.startswith("#EXTINF:"):
  84.                         if i // 2 == channel_index:
  85.                             group_title_line = lines[i].strip()
  86.                             group_title = group_title_line.split('group-title="')[1].split('"')[0]
  87.                             return group_title
  88.         except Exception as e:
  89.             print("Error:", e)
  90.         return None
  91.  
  92.     def play_video(self):
  93.         self.player.play()
  94.         self.status_label.config(text="Playing", fg="green")
  95.  
  96.     def stop_video(self):
  97.         self.player.stop()
  98.         self.status_label.config(text="Stopped", fg="red")
  99.  
  100.     def play_channel(self, channel_index):
  101.         try:
  102.             with open("RADIOS.m3u", "r", encoding="utf-8") as file:  
  103.                 lines = file.readlines()
  104.                 for i, line in enumerate(lines):
  105.                     if line.startswith("#EXTINF:"):
  106.                         if i // 2 == channel_index:  
  107.                             stream_url = lines[i + 1].strip()
  108.                             media = self.instance.media_new(stream_url)
  109.                             self.player.set_media(media)
  110.  
  111.                             if self.player_frame.winfo_children():
  112.                                 self.player_frame.winfo_children()[0].destroy()
  113.  
  114.                             self.player_frame.update()
  115.                             self.player.set_hwnd(self.player_frame.winfo_id())
  116.                             self.player.play()
  117.  
  118.                             self.status_label.config(text="Playing", fg="green")
  119.                             self.current_channel_index = channel_index
  120.                             return
  121.  
  122.             self.status_label.config(text="Channel not found", fg="red")
  123.  
  124.         except Exception as e:
  125.             self.status_label.config(text="Failed to play", fg="red")
  126.  
  127. root = tk.Tk()
  128. video_player = VideoPlayer(root)
  129. root.mainloop()
  130.  
Add Comment
Please, Sign In to add comment