Advertisement
Najeebsk

IPTV-URLS-PLAYER2.0.pyw

Jun 24th, 2024 (edited)
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.77 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk, messagebox, filedialog
  3. import vlc
  4. import subprocess
  5.  
  6. # Path to VLC executable
  7. VLC_PATH = r"C:\Program Files\VideoLAN\VLC\vlc.exe"
  8.  
  9. class ChannelPlayer:
  10.     def __init__(self, root):
  11.         self.root = root
  12.         self.root.title("Najeeb Live Videos and IPTV Channels Player")
  13.        
  14.         self.channels = {}
  15.         self.current_instance = None
  16.         self.player = None
  17.         self.media = None
  18.         self.is_dragging_slider = False
  19.         self.is_fullscreen = False
  20.         self.is_muted = False
  21.  
  22.         self.create_widgets()
  23.    
  24.     def load_channels(self, file_path):
  25.         self.channels.clear()
  26.         try:
  27.             with open(file_path, 'r') as file:
  28.                 for line in file:
  29.                     parts = line.strip().split(maxsplit=1)
  30.                     if len(parts) == 2:
  31.                         self.channels[parts[0]] = parts[1]
  32.             self.update_channel_dropdown()
  33.         except FileNotFoundError:
  34.             messagebox.showerror("Error", f"File not found: {file_path}")
  35.    
  36.     def update_channel_dropdown(self):
  37.         self.channel_dropdown['values'] = list(self.channels.keys())
  38.    
  39.     def create_widgets(self):
  40.         style = ttk.Style()
  41.         style.configure('TButton', font=('Helvetica', 10))
  42.         style.configure('TCombobox', font=('Helvetica', 10))
  43.        
  44.         # Search field for channel title
  45.         self.search_label = ttk.Label(self.root, text="Search Channel Title:")
  46.         self.search_label.grid(row=0, column=0, padx=5, pady=5, sticky="w")
  47.  
  48.         self.search_var = tk.StringVar()
  49.         self.search_entry = ttk.Entry(self.root, textvariable=self.search_var)
  50.         self.search_entry.grid(row=0, column=1, columnspan=2, padx=5, pady=5, sticky="ew")
  51.         self.search_entry.bind("<KeyRelease>", self.filter_channels)
  52.  
  53.         # Browse button for URLs file
  54.         self.browse_button = ttk.Button(self.root, text="Browse", command=self.browse_file)
  55.         self.browse_button.grid(row=0, column=3, padx=5, pady=5, sticky="ew")
  56.  
  57.         # Channel selection
  58.         self.channel_var = tk.StringVar()
  59.         self.channel_dropdown = ttk.Combobox(self.root, textvariable=self.channel_var)
  60.         self.channel_dropdown.grid(row=1, column=0, columnspan=2, padx=5, pady=5, sticky="ew")
  61.        
  62.         # Preview button
  63.         self.preview_button = ttk.Button(self.root, text="Preview", command=self.preview_channel)
  64.         self.preview_button.grid(row=1, column=2, padx=5, pady=5, sticky="ew")
  65.  
  66.         # Stop preview button
  67.         self.stop_preview_button = ttk.Button(self.root, text="Stop Preview", command=self.stop_preview)
  68.         self.stop_preview_button.grid(row=1, column=3, padx=5, pady=5, sticky="ew")
  69.  
  70.         # Play button
  71.         self.play_button = ttk.Button(self.root, text="Play", command=self.play_channel)
  72.         self.play_button.grid(row=1, column=4, padx=5, pady=5, sticky="ew")
  73.  
  74.         # Volume control
  75.         self.volume_slider = ttk.Scale(self.root, from_=0, to=100, orient=tk.HORIZONTAL, command=self.set_volume)
  76.         self.volume_slider.set(50)  # Set initial volume to 50%
  77.         self.volume_slider.grid(row=1, column=5, padx=5, pady=5, sticky="ew")
  78.  
  79.         # Mute button
  80.         self.mute_button = ttk.Button(self.root, text="Mute", command=self.toggle_mute)
  81.         self.mute_button.grid(row=1, column=6, padx=5, pady=5, sticky="ew")
  82.  
  83.         # Full screen button
  84.         self.fullscreen_button = ttk.Button(self.root, text="Full Screen", command=self.toggle_fullscreen)
  85.         self.fullscreen_button.grid(row=1, column=7, padx=5, pady=5, sticky="ew")
  86.  
  87.         # VLC video frame
  88.         self.video_frame = tk.Frame(self.root, bg="black", height=300)
  89.         self.video_frame.grid(row=2, column=0, columnspan=8, padx=5, pady=5, sticky="nsew")
  90.  
  91.         # Video control slider
  92.         self.video_slider = ttk.Scale(self.root, from_=0, to=1000, orient=tk.HORIZONTAL)
  93.         self.video_slider.grid(row=3, column=0, columnspan=8, padx=5, pady=5, sticky="ew")
  94.         self.video_slider.bind("<ButtonPress-1>", self.slider_pressed)
  95.         self.video_slider.bind("<ButtonRelease-1>", self.slider_released)
  96.  
  97.         self.root.grid_rowconfigure(2, weight=1)
  98.         self.root.grid_rowconfigure(3, weight=0)
  99.         self.root.grid_columnconfigure(0, weight=1)
  100.         self.root.grid_columnconfigure(1, weight=1)
  101.         self.root.grid_columnconfigure(2, weight=1)
  102.         self.root.grid_columnconfigure(3, weight=1)
  103.         self.root.grid_columnconfigure(4, weight=1)
  104.         self.root.grid_columnconfigure(5, weight=1)
  105.         self.root.grid_columnconfigure(6, weight=1)
  106.         self.root.grid_columnconfigure(7, weight=1)
  107.         self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
  108.  
  109.     def browse_file(self):
  110.         file_path = filedialog.askopenfilename(
  111.             title="Select URLs file",
  112.             filetypes=(("Text files", "*.txt"), ("All files", "*.*"))
  113.         )
  114.         if file_path:
  115.             self.load_channels(file_path)
  116.  
  117.     def filter_channels(self, event):
  118.         search_term = self.search_var.get().lower()
  119.         filtered_channels = [channel for channel in self.channels.keys() if search_term in channel.lower()]
  120.         self.channel_dropdown['values'] = filtered_channels
  121.  
  122.     def preview_channel(self):
  123.         self.stop_preview()
  124.         channel = self.channel_var.get()
  125.         if channel and channel in self.channels:
  126.             url = self.channels[channel]
  127.             self.current_instance = vlc.Instance()
  128.             self.player = self.current_instance.media_player_new()
  129.             self.player.set_hwnd(self.video_frame.winfo_id())
  130.             self.media = self.current_instance.media_new(url)
  131.             self.player.set_media(self.media)
  132.             self.player.play()
  133.             self.update_video_slider()
  134.  
  135.     def stop_preview(self):
  136.         if self.player:
  137.             self.player.stop()
  138.             self.player.release()
  139.             self.player = None
  140.         if self.current_instance:
  141.             self.current_instance.release()
  142.             self.current_instance = None
  143.  
  144.     def play_channel(self):
  145.         self.stop_preview()
  146.         channel = self.channel_var.get()
  147.         if channel and channel in self.channels:
  148.             url = self.channels[channel]
  149.             subprocess.Popen([VLC_PATH, url])
  150.  
  151.     def set_volume(self, value):
  152.         if self.player:
  153.             self.player.audio_set_volume(int(float(value)))
  154.  
  155.     def toggle_mute(self):
  156.         if self.player:
  157.             self.is_muted = not self.is_muted
  158.             self.player.audio_toggle_mute()
  159.             self.mute_button.config(text="Unmute" if self.is_muted else "Mute")
  160.  
  161.     def slider_pressed(self, event):
  162.         self.is_dragging_slider = True
  163.  
  164.     def slider_released(self, event):
  165.         self.is_dragging_slider = False
  166.         self.set_position(self.video_slider.get())
  167.  
  168.     def set_position(self, value):
  169.         if self.player:
  170.             self.player.set_position(float(value) / 1000.0)
  171.  
  172.     def update_video_slider(self):
  173.         if self.player and not self.is_dragging_slider:
  174.             length = self.player.get_length()
  175.             if length > 0:
  176.                 current_time = self.player.get_time()
  177.                 position = int(current_time / length * 1000)
  178.                 self.video_slider.set(position)
  179.         self.root.after(1000, self.update_video_slider)
  180.  
  181.     def toggle_fullscreen(self):
  182.         self.is_fullscreen = not self.is_fullscreen
  183.         self.root.attributes("-fullscreen", self.is_fullscreen)
  184.         if not self.is_fullscreen:
  185.             self.root.geometry("800x600")
  186.  
  187.     def on_closing(self):
  188.         self.stop_preview()
  189.         self.root.destroy()
  190.  
  191. if __name__ == "__main__":
  192.     root = tk.Tk()
  193.     app = ChannelPlayer(root)
  194.     root.mainloop()
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement