Najeebsk

IPTV-M3U-CHECK-PREVIWE.pyw

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