Najeebsk

IPTV-VIDEO-PLAYER.pyw

May 25th, 2024 (edited)
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.57 KB | None | 0 0
  1. import os
  2. import tkinter as tk
  3. from tkinter import ttk, filedialog, messagebox
  4. import requests
  5. import subprocess
  6. import threading
  7. import time
  8. import vlc
  9.  
  10. # Functions for the main functionalities
  11. def search_channels():
  12.     search_term = url_entry.get().lower()
  13.     if search_term.startswith("http"):
  14.         search_by_url(search_term)
  15.     else:
  16.         search_by_path_or_category(search_term)
  17.  
  18. def search_by_url(url):
  19.     try:
  20.         if os.path.exists(url):
  21.             with open(url, 'r', encoding='utf-8') as file:
  22.                 m3u_data = file.readlines()
  23.             process_m3u_data(m3u_data)
  24.         else:
  25.             response = requests.get(url)
  26.             if response.status_code == 200:
  27.                 try:
  28.                     m3u_data = response.text.split('\n')
  29.                 except UnicodeDecodeError:
  30.                     m3u_data = response.content.decode('ISO-8859-1').split('\n')
  31.                 process_m3u_data(m3u_data)
  32.             else:
  33.                 result_text.insert(tk.END, f"Error: Failed to fetch channel data. Status Code: {response.status_code}\n")
  34.     except Exception as e:
  35.         result_text.insert(tk.END, f"Error: {str(e)}\n")
  36.  
  37. def search_by_path_or_category(path):
  38.     try:
  39.         if os.path.exists(path):
  40.             try:
  41.                 with open(path, 'r', encoding='utf-8') as file:
  42.                     m3u_data = file.readlines()
  43.             except UnicodeDecodeError:
  44.                 with open(path, 'r', encoding='ISO-8859-1') as file:
  45.                     m3u_data = file.readlines()
  46.             process_m3u_data(m3u_data)
  47.         else:
  48.             selected_category = category_var.get()
  49.             if selected_category in category_urls:
  50.                 category_url = category_urls[selected_category]
  51.                 if category_url:
  52.                     search_by_url(category_url)
  53.                 else:
  54.                     result_text.insert(tk.END, f"Error: Category URL is not provided for {selected_category}\n")
  55.             else:
  56.                 result_text.insert(tk.END, f"Error: Category '{selected_category}' not found\n")
  57.     except Exception as e:
  58.         result_text.insert(tk.END, f"Error: {str(e)}\n")
  59.  
  60. def process_m3u_data(m3u_data):
  61.     result_text.delete(0, tk.END)
  62.     global channels_info
  63.     channels_info = {}
  64.     channel_name = None
  65.     for line in m3u_data:
  66.         if line.startswith('#EXTINF:'):
  67.             channel_name = line.split(',')[-1].strip()
  68.         elif line.startswith('http') and channel_name:
  69.             channels_info[channel_name] = line.strip()
  70.             result_text.insert(tk.END, channel_name)
  71.             channel_name = None
  72.  
  73. def play_selected_channel(event):
  74.     try:
  75.         selected_channel = result_text.get(tk.ACTIVE)
  76.         if os.path.exists(selected_channel):
  77.             subprocess.Popen([r"C:\Program Files\VideoLAN\VLC\vlc.exe", selected_channel])
  78.         else:
  79.             subprocess.Popen([r"C:\Program Files\VideoLAN\VLC\vlc.exe", channels_info[selected_channel]])
  80.     except (tk.TclError, KeyError):
  81.         pass
  82.  
  83. def check_links():
  84.     global working_links
  85.     working_links = {}
  86.     for channel_name, url in channels_info.items():
  87.         try:
  88.             response = requests.get(url)
  89.             if response.status_code == 200:
  90.                 working_links[channel_name] = url
  91.         except requests.RequestException:
  92.             pass
  93.  
  94.     result_text.delete(0, tk.END)
  95.     for channel_name, url in working_links.items():
  96.         result_text.insert(tk.END, f"{channel_name}: {url}\n")
  97.  
  98. def save_working_links():
  99.     with open("working_channels.m3u", "w", encoding="utf-8") as f:
  100.         for channel_name, url in working_links.items():
  101.             f.write(f"#EXTINF:-1,{channel_name}\n{url}\n")
  102.  
  103. def filter_channels(event=None):
  104.     keyword = search_entry.get().lower()
  105.     result_text.delete(0, tk.END)
  106.     for channel_name, url in channels_info.items():
  107.         if keyword in channel_name.lower():
  108.             result_text.insert(tk.END, channel_name)
  109.  
  110. def browse_file():
  111.     file_path = filedialog.askopenfilename(filetypes=[("M3U Files", "*.m3u"), ("All Files", "*.*")])
  112.     if file_path:
  113.         url_entry.delete(0, tk.END)
  114.         url_entry.insert(0, file_path)
  115.  
  116. def preview_selected_link():
  117.     try:
  118.         selected_channel = result_text.get(tk.ACTIVE).strip()
  119.         if selected_channel in channels_info:
  120.             url = channels_info[selected_channel]
  121.             preview_frame.pack(fill="both", expand=True)
  122.             media = instance.media_new(url)
  123.             player.set_media(media)
  124.             player.play()
  125.             update_slider()
  126.         else:
  127.             messagebox.showerror("Error", "Selected text is not a valid URL.")
  128.     except tk.TclError:
  129.         messagebox.showerror("Error", "No text selected.")
  130.  
  131. def stop_preview():
  132.     player.stop()
  133.     preview_frame.pack_forget()
  134.  
  135. def capture_video():
  136.     try:
  137.         selected_channel = result_text.get(tk.ACTIVE).strip()
  138.         if selected_channel in channels_info:
  139.             url = channels_info[selected_channel]
  140.             filename = filedialog.asksaveasfilename(defaultextension=".mp4", filetypes=[("MP4 files", "*.mp4")])
  141.             if filename:
  142.                 command = ['ffmpeg', '-y', '-i', url, '-t', '03:55:00', '-c', 'copy', filename]
  143.                 threading.Thread(target=lambda: subprocess.run(command)).start()
  144.                 messagebox.showinfo("Capturing", f"Capturing 03:55 minutes of video to {filename}")
  145.         else:
  146.             messagebox.showerror("Error", "Selected text is not a valid URL.")
  147.     except tk.TclError:
  148.         messagebox.showerror("Error", "No text selected.")
  149.  
  150. def record_audio():
  151.     try:
  152.         selected_channel = result_text.get(tk.ACTIVE).strip()
  153.         if selected_channel in channels_info:
  154.             url = channels_info[selected_channel]
  155.             filename = filedialog.asksaveasfilename(defaultextension=".mp3", filetypes=[("MP3 files", "*.mp3")])
  156.             if filename:
  157.                 command = ['ffmpeg', '-y', '-i', url, '-f', 'mp3', '-c:a', 'libmp3lame', filename]
  158.                 global process
  159.                 process = subprocess.Popen(command)
  160.                 messagebox.showinfo("Recording", f"Recording audio to {filename}")
  161.         else:
  162.             messagebox.showerror("Error", "Selected text is not a valid URL.")
  163.     except tk.TclError:
  164.         messagebox.showerror("Error", "No text selected.")
  165.  
  166. def stop_recording():
  167.     if process:
  168.         process.terminate()
  169.         messagebox.showinfo("Stopped", "Recording stopped")
  170.  
  171. def capture_screenshots():
  172.     if player.get_media():
  173.         filename_base = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
  174.         if filename_base:
  175.             interval = 5
  176.             num_screenshots = 5
  177.             for i in range(num_screenshots):
  178.                 time.sleep(interval)
  179.                 filename = f"{filename_base}_{i+1}.png"
  180.                 player.video_take_snapshot(0, filename, 0, 0)
  181.                 messagebox.showinfo("Captured", f"Screenshot {i+1} saved to {filename}")
  182.  
  183. def update_slider():
  184.     if player.get_media():
  185.         length = player.get_length() / 1000
  186.         position = player.get_time() / 1000
  187.         if length > 0:
  188.             slider.set(position / length * 100)
  189.         root.after(1000, update_slider)
  190.  
  191. def set_position(event):
  192.     if player.get_media():
  193.         length = player.get_length() / 1000
  194.         player.set_time(int(slider.get() / 100 * length * 1000))
  195.  
  196. def on_configure(event):
  197.     if event.widget == canvas:
  198.         player.set_hwnd(canvas.winfo_id())
  199.  
  200. def toggle_mute():
  201.     is_muted = player.audio_get_mute()
  202.     player.audio_set_mute(not is_muted)
  203.  
  204. # GUI setup
  205. root = tk.Tk()
  206. root.title("Najeeb IPTV Channel Link Checker")
  207. root.configure(bg="#4a4a4a")
  208.  
  209. url_frame = tk.Frame(root, bg="#4a4a4a")
  210. url_frame.pack(pady=10)
  211.  
  212. url_label = tk.Label(url_frame, text="Enter URL or local path or select category:", bg="#4a4a4a", fg="white")
  213. url_label.pack(side=tk.LEFT, padx=5)
  214.  
  215. url_entry = tk.Entry(url_frame, width=80)
  216. url_entry.pack(side=tk.LEFT, padx=5)
  217.  
  218. search_button = tk.Button(url_frame, text="Search", command=search_channels, bg="#FFA500", fg="white")
  219. search_button.pack(side=tk.LEFT, padx=5)
  220.  
  221. #result_label = tk.Label(root, text="Check and save working URLs in M3U file:", bg="#4a4a4a", fg="white")
  222. #result_label.pack()
  223.  
  224. search_frame = tk.Frame(root, bg="#4a4a4a")
  225. search_frame.pack()
  226.  
  227. search_label = tk.Label(search_frame, text="Search Channel Name:", bg="#4a4a4a", fg="white")
  228. search_label.pack(side=tk.LEFT, padx=5)
  229.  
  230. search_entry = tk.Entry(search_frame, width=105)
  231. search_entry.pack(side=tk.LEFT, padx=5)
  232. search_entry.bind('<KeyRelease>', filter_channels)
  233.  
  234. result_frame = tk.Frame(root)
  235. result_frame.pack(pady=10)
  236.  
  237. scrollbar = tk.Scrollbar(result_frame)
  238. scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  239.  
  240. result_text = tk.Listbox(result_frame, width=150, height=12, bg="#333333", fg="white", selectbackground="#FFA500", selectforeground="black", yscrollcommand=scrollbar.set)
  241. result_text.pack(side=tk.LEFT, fill=tk.BOTH)
  242. result_text.bind('<Double-1>', play_selected_channel)
  243.  
  244. scrollbar.config(command=result_text.yview)
  245.  
  246. button_frame = tk.Frame(root, bg="#4a4a4a")
  247. button_frame.pack()
  248.  
  249. browse_button = tk.Button(button_frame, text="Browse Playlist", command=browse_file, bg="#5a5a5a", fg="white")
  250. browse_button.pack(side=tk.LEFT, padx=5)
  251.  
  252. preview_button = tk.Button(button_frame, text="Preview", command=preview_selected_link, bg="#5a5a5a", fg="white")
  253. preview_button.pack(side=tk.LEFT, padx=5)
  254.  
  255. capture_video_button = tk.Button(button_frame, text="Capture Video", command=capture_video, bg="#5a5a5a", fg="white")
  256. capture_video_button.pack(side=tk.LEFT, padx=5)
  257.  
  258. record_button = tk.Button(button_frame, text="Record Audio", command=record_audio, bg="#5a5a5a", fg="white")
  259. record_button.pack(side=tk.LEFT, padx=5)
  260.  
  261. stop_recording_button = tk.Button(button_frame, text="Stop Recording", command=stop_recording, bg="#5a5a5a", fg="white")
  262. stop_recording_button.pack(side=tk.LEFT, padx=5)
  263.  
  264. capture_screenshot_button = tk.Button(button_frame, text="Capture Screenshots", command=capture_screenshots, bg="#5a5a5a", fg="white")
  265. capture_screenshot_button.pack(side=tk.LEFT, padx=5)
  266.  
  267. toggle_mute_button = tk.Button(button_frame, text="Toggle Audio Mute", command=toggle_mute, bg="#5a5a5a", fg="white")
  268. toggle_mute_button.pack(side=tk.LEFT, padx=5)
  269.  
  270. category_urls = {
  271.     "NAJEEB-IPTV": "",
  272.     "ALL-INDEX": "https://iptv-org.github.io/iptv/index.m3u",
  273.     "CATEGORY": "https://iptv-org.github.io/iptv/index.category.m3u",
  274.     "LANGUAGE": "https://iptv-org.github.io/iptv/index.language.m3u",
  275.     "REGION": "https://iptv-org.github.io/iptv/index.region.m3u",
  276.     "Brazil": "https://iptv-org.github.io/iptv/countries/br.m3u",
  277.     "France": "https://iptv-org.github.io/iptv/countries/fr.m3u",
  278.     "India": "https://iptv-org.github.io/iptv/countries/in.m3u",
  279.     "Italy": "https://iptv-org.github.io/iptv/countries/it.m3u",
  280.     "Pakistan": "https://iptv-org.github.io/iptv/countries/pk.m3u",
  281.     "Spain": "https://iptv-org.github.io/iptv/countries/es.m3u",
  282.     "Thailand": "https://iptv-org.github.io/iptv/countries/th.m3u",
  283.     "UK": "https://iptv-org.github.io/iptv/countries/uk.m3u",
  284.     "USA": "https://iptv-org.github.io/iptv/countries/us.m3u",
  285.     "Classic": "https://iptv-org.github.io/iptv/categories/classic.m3u",
  286.     "Comedy": "https://iptv-org.github.io/iptv/categories/comedy.m3u",
  287.     "Documentary": "https://iptv-org.github.io/iptv/categories/documentary.m3u",
  288.     "Entertainment": "https://iptv-org.github.io/iptv/categories/entertainment.m3u",
  289.     "Kids": "https://iptv-org.github.io/iptv/categories/kids.m3u",
  290.     "Movies": "https://iptv-org.github.io/iptv/categories/movies.m3u",
  291.     "Music": "https://iptv-org.github.io/iptv/categories/music.m3u",
  292.     "News": "https://iptv-org.github.io/iptv/categories/news.m3u",
  293.     "Science": "https://iptv-org.github.io/iptv/categories/science.m3u",
  294.     "Sports": "https://iptv-org.github.io/iptv/categories/sports.m3u",
  295.     "Travel": "https://iptv-org.github.io/iptv/categories/travel.m3u",
  296.     "PLAYLIST-1": "C:/Users/Najeeb/Desktop/IPTV/PL1.m3u",
  297.     "PLAYLIST-2": "C:/Users/Najeeb/Desktop/IPTV/PL2.m3u",
  298.     "PLAYLIST-3": "C:/Users/Najeeb/Desktop/IPTV/PL3.m3u",
  299.     "PLAYLIST-4": "C:/Users/Najeeb/Desktop/IPTV/PL4.m3u",
  300.     "PLAYLIST-5": "C:/Users/Najeeb/Desktop/IPTV/PL5.m3u",
  301.     "PLAYLIST-6": "C:/Users/Najeeb/Desktop/IPTV/PL6.m3u",
  302.     "PLAYLIST-7": "C:/Users/Najeeb/Desktop/IPTV/PL7.m3u",
  303.     "PLAYLIST-R": "C:/Users/Najeeb/Desktop/IPTV/PLR.m3u",
  304.     "PLAYLIST-X": "C:/Users/Najeeb/Desktop/IPTV/PLX.m3u",
  305. }
  306.  
  307. category_var = tk.StringVar(button_frame)
  308. category_var.set("NAJEEB-IPTV")
  309. category_dropdown = ttk.OptionMenu(button_frame, category_var, *category_urls.keys())
  310. category_dropdown.pack(side=tk.RIGHT, padx=5)
  311.  
  312. # Add this section for the preview frame setup
  313. preview_frame = tk.Frame(root, bg="#4a4a4a", height=200)
  314. preview_frame.pack(fill="both", expand=True)
  315. preview_frame.pack_forget()
  316.  
  317. # Move stop_button creation here and pack it at the top
  318. stop_button = tk.Button(preview_frame, text="Close Preview", command=stop_preview, bg="#5a5a5a", fg="white")
  319. stop_button.pack(side=tk.TOP, padx=5, pady=5)
  320.  
  321. canvas = tk.Canvas(preview_frame, bg="#4a4a4a")
  322. canvas.pack(fill="both", expand=True)
  323. canvas.bind("<Configure>", on_configure)
  324.  
  325. slider = tk.Scale(preview_frame, from_=0, to=100, orient=tk.HORIZONTAL, command=set_position, bg="#4a4a4a", fg="white")
  326. slider.pack(fill="x", padx=5)
  327.  
  328. # VLC setup
  329. instance = vlc.Instance()
  330. player = instance.media_player_new()
  331.  
  332. channels_info = {}
  333. working_links = {}
  334.  
  335. root.mainloop()
  336.  
Add Comment
Please, Sign In to add comment