Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import tkinter as tk
- from tkinter import ttk, filedialog, messagebox
- import requests
- import subprocess
- import threading
- import time
- import vlc
- from yt_dlp import YoutubeDL
- from googleapiclient.discovery import build
- # Constants
- YOUTUBE_API_KEY = 'AIzaSyCzS7PGThVFxD83UFbfU5DOSZBMTxNpEeA'
- YOUTUBE_API_SERVICE_NAME = 'youtube'
- YOUTUBE_API_VERSION = 'v3'
- # Functions for the main functionalities
- def search_channels():
- search_term = url_entry.get().lower()
- if search_term.startswith("http"):
- search_by_url(search_term)
- else:
- search_by_path_or_category(search_term)
- def search_by_url(url):
- try:
- if os.path.exists(url):
- with open(url, 'r', encoding='utf-8') as file:
- m3u_data = file.readlines()
- process_m3u_data(m3u_data)
- else:
- response = requests.get(url)
- if response.status_code == 200:
- try:
- m3u_data = response.text.split('\n')
- except UnicodeDecodeError:
- m3u_data = response.content.decode('ISO-8859-1').split('\n')
- process_m3u_data(m3u_data)
- else:
- result_text.insert(tk.END, f"Error: Failed to fetch channel data. Status Code: {response.status_code}\n")
- except Exception as e:
- result_text.insert(tk.END, f"Error: {str(e)}\n")
- def search_by_path_or_category(path):
- try:
- if os.path.exists(path):
- try:
- with open(path, 'r', encoding='utf-8') as file:
- m3u_data = file.readlines()
- except UnicodeDecodeError:
- with open(path, 'r', encoding='ISO-8859-1') as file:
- m3u_data = file.readlines()
- process_m3u_data(m3u_data)
- else:
- selected_category = category_var.get()
- if selected_category in category_urls:
- category_url = category_urls[selected_category]
- if category_url:
- search_by_url(category_url)
- else:
- result_text.insert(tk.END, f"Error: Category URL is not provided for {selected_category}\n")
- else:
- result_text.insert(tk.END, f"Error: Category '{selected_category}' not found\n")
- except Exception as e:
- result_text.insert(tk.END, f"Error: {str(e)}\n")
- def process_m3u_data(m3u_data):
- result_text.delete(0, tk.END)
- global channels_info
- channels_info = {}
- channel_name = None
- for line in m3u_data:
- if line.startswith('#EXTINF:'):
- channel_name = line.split(',')[-1].strip()
- elif line.startswith('http') and channel_name:
- channels_info[channel_name] = line.strip()
- result_text.insert(tk.END, channel_name)
- channel_name = None
- def search_youtube():
- query = search_entry.get()
- youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=YOUTUBE_API_KEY)
- search_response = youtube.search().list(q=query, part='id,snippet', maxResults=250).execute()
- result_text.delete(0, tk.END)
- global channels_info
- channels_info = {}
- for item in search_response['items']:
- if item['id']['kind'] == 'youtube#video':
- video_title = item['snippet']['title']
- video_id = item['id']['videoId']
- video_url = f"https://www.youtube.com/watch?v={video_id}"
- channels_info[video_title] = video_url
- result_text.insert(tk.END, video_title)
- print("Searching YouTube for:", query)
- def play_selected_channel(event):
- try:
- selected_channel = result_text.get(tk.ACTIVE)
- if selected_channel in channels_info:
- url = channels_info[selected_channel]
- if 'youtube.com' in url or 'youtu.be' in url:
- download_and_play_youtube(url)
- elif os.path.exists(url):
- subprocess.Popen([r"C:\Program Files\VideoLAN\VLC\vlc.exe", url])
- except (tk.TclError, KeyError):
- pass
- def download_and_play_youtube(url):
- ydl_opts = {'format': 'best'}
- try:
- with YoutubeDL(ydl_opts) as ydl:
- info_dict = ydl.extract_info(url, download=False)
- video_url = info_dict['url']
- play_vlc_stream(video_url)
- except Exception as e:
- messagebox.showerror("Error", f"Failed to extract video URL: {str(e)}")
- def play_vlc_stream(stream_url):
- media = instance.media_new(stream_url)
- player.set_media(media)
- player.set_hwnd(video_panel.winfo_id())
- player.play()
- update_slider()
- def filter_channels(event=None):
- keyword = search_entry.get().lower()
- result_text.delete(0, tk.END)
- for channel_name, url in channels_info.items():
- if keyword in channel_name.lower():
- result_text.insert(tk.END, channel_name)
- def browse_file():
- file_path = filedialog.askopenfilename(filetypes=[("M3U Files", "*.m3u"), ("All Files", "*.*")])
- if file_path:
- url_entry.delete(0, tk.END)
- url_entry.insert(0, file_path)
- def preview_selected_link():
- selected_channel = result_text.get(tk.ACTIVE).strip()
- if selected_channel in channels_info:
- url = channels_info[selected_channel]
- play_vlc_stream(url)
- else:
- messagebox.showerror("Error", "Selected text is not a valid URL.")
- def stop_preview():
- player.stop()
- #preview_frame.pack_forget()
- def capture_video():
- try:
- selected_channel = result_text.get(tk.ACTIVE).strip()
- if selected_channel in channels_info:
- url = channels_info[selected_channel]
- filename = filedialog.asksaveasfilename(defaultextension=".mp4", filetypes=[("MP4 files", "*.mp4")])
- if filename:
- command = ['ffmpeg', '-y', '-i', url, '-t', '03:55:00', '-c', 'copy', filename]
- threading.Thread(target=lambda: subprocess.run(command)).start()
- messagebox.showinfo("Capturing", f"Capturing 03:55 minutes of video to {filename}")
- else:
- messagebox.showerror("Error", "Selected text is not a valid URL.")
- except tk.TclError:
- messagebox.showerror("Error", "No text selected.")
- def record_audio():
- try:
- selected_channel = result_text.get(tk.ACTIVE).strip()
- if selected_channel in channels_info:
- url = channels_info[selected_channel]
- filename = filedialog.asksaveasfilename(defaultextension=".mp3", filetypes=[("MP3 files", "*.mp3")])
- if filename:
- command = ['ffmpeg', '-y', '-i', url, '-f', 'mp3', '-c:a', 'libmp3lame', filename]
- global process
- process = subprocess.Popen(command)
- messagebox.showinfo("Recording", f"Recording audio to {filename}")
- else:
- messagebox.showerror("Error", "Selected text is not a valid URL.")
- except tk.TclError:
- messagebox.showerror("Error", "No text selected.")
- def stop_recording():
- if process:
- process.terminate()
- messagebox.showinfo("Stopped", "Recording stopped")
- def capture_screenshots():
- if player.get_media():
- filename_base = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
- if filename_base:
- interval = 5
- num_screenshots = 5
- for i in range(num_screenshots):
- time.sleep(interval)
- filename = f"{filename_base}_{i + 1}.png"
- player.video_take_snapshot(0, filename, 0, 0)
- messagebox.showinfo("Capturing Screenshots", f"Captured {num_screenshots} screenshots every {interval} seconds to {filename_base}")
- def update_slider():
- length = player.get_length() / 1000
- pos = player.get_position()
- slider.set(pos)
- current_time_label.config(text=time.strftime('%H:%M:%S', time.gmtime(player.get_time() / 1000)))
- total_time_label.config(text=time.strftime('%H:%M:%S', time.gmtime(length)))
- if player.is_playing():
- root.after(1000, update_slider)
- def on_slider_change(value):
- player.set_position(float(value))
- def play_in_vlc(url):
- if 'youtube.com' in url or 'youtu.be' in url:
- download_and_play_youtube(url)
- else:
- play_vlc_stream(url)
- def set_volume(value):
- volume = int(value)
- if volume > 200:
- volume = 200
- player.audio_set_volume(volume)
- def download_youtube():
- try:
- selected_channel = result_text.get(tk.ACTIVE).strip()
- if selected_channel in channels_info:
- url = channels_info[selected_channel]
- filename = filedialog.asksaveasfilename(defaultextension=".mp4", filetypes=[("All files", "*.*")])
- if filename:
- quality = download_quality_var.get()
- if quality == 'Best':
- ydl_opts = {'format': 'bestvideo+bestaudio/best'}
- elif quality == 'Medium':
- ydl_opts = {'format': 'bestvideo[height<=480]+bestaudio/best[height<=480]'}
- elif quality == 'Low':
- ydl_opts = {'format': 'bestvideo[height<=240]+bestaudio/best[height<=240]'}
- elif quality == '360p':
- ydl_opts = {'format': 'bestvideo[height<=360]+bestaudio/best[height<=360]/mp4'}
- elif quality == '480p':
- ydl_opts = {'format': 'bestvideo[height<=480]+bestaudio/best[height<=480]/mp4'}
- elif quality == '720p':
- ydl_opts = {'format': 'bestvideo[height<=720]+bestaudio/best[height<=720]/mp4'}
- elif quality == 'MP3':
- ydl_opts = {'format': 'bestaudio/best', 'postprocessors': [{'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '192'}]}
- ydl_opts['outtmpl'] = filename
- with YoutubeDL(ydl_opts) as ydl:
- ydl.download([url])
- messagebox.showinfo("Download Complete", f"Downloaded video to {filename}")
- else:
- messagebox.showerror("Error", "Selected text is not a valid URL.")
- except tk.TclError:
- messagebox.showerror("Error", "No text selected.")
- def play_vlc():
- selected_channel = result_text.get(tk.ACTIVE).strip()
- if selected_channel in channels_info:
- url = channels_info[selected_channel]
- subprocess.Popen([r"C:\Program Files\VideoLAN\VLC\vlc.exe", url])
- else:
- messagebox.showerror("Error", "Selected text is not a valid URL.")
- # GUI setup
- root = tk.Tk()
- root.title("Najeeb IPTV Channel And YOUTUBE Video")
- root.configure(bg="#4a4a4a")
- root.geometry("1000x680")
- url_frame = tk.Frame(root, bg="#4a4a4a")
- url_frame.pack(pady=10, fill=tk.X)
- url_label = tk.Label(url_frame, text="Enter URL or local path:", bg="#4a4a4a", fg="white")
- url_label.pack(side=tk.LEFT, padx=5)
- url_entry = ttk.Entry(url_frame, width=96)
- url_entry.pack(side=tk.LEFT, padx=5)
- search_button = tk.Button(url_frame, text="Search", command=search_channels, bg="#FFA500", fg="white")
- search_button.pack(side=tk.LEFT, padx=5)
- browse_button = tk.Button(url_frame, text="Browse M3U", command=browse_file, bg="#4a4a4a", fg="white")
- browse_button.pack(side=tk.LEFT, padx=5)
- volume_frame = ttk.Frame(url_frame)
- volume_frame.pack(side=tk.LEFT, padx=10)
- volume_scale = tk.Scale(volume_frame, from_=0, to=200, orient="horizontal", command=set_volume, label="", bg="#4a4a4a", fg="white")
- volume_scale.set(50)
- volume_scale.pack(side=tk.LEFT, padx=5)
- category_frame = ttk.Frame(root)
- category_frame.pack(pady=10, fill=tk.X)
- ttk.Label(category_frame, text="Select Category:").pack(side=tk.LEFT)
- category_var = tk.StringVar()
- category_combo = ttk.Combobox(category_frame, textvariable=category_var)
- category_combo.pack(side=tk.LEFT, padx=5)
- category_combo['values'] = ['Sports', 'News', 'Movies', 'Songs']
- category_combo.bind("<<ComboboxSelected>>", lambda e: search_channels())
- ttk.Button(category_frame, text="Search YouTube", command=search_youtube).pack(side=tk.LEFT, padx=5)
- ttk.Label(category_frame, text="Search Word:").pack(side=tk.LEFT)
- search_entry = ttk.Entry(category_frame, width=90)
- search_entry.pack(pady=5)
- search_entry.bind("<KeyRelease>", filter_channels)
- result_frame = ttk.Frame(root)
- result_frame.pack(fill=tk.BOTH, expand=True)
- scrollbar = tk.Scrollbar(result_frame)
- scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
- result_text = tk.Listbox(result_frame, selectmode=tk.SINGLE)
- result_text.pack(fill=tk.BOTH, expand=True)
- result_text.bind("<Double-Button-1>", play_selected_channel)
- scrollbar.config(command=result_text.yview)
- control_frame = ttk.Frame(root)
- control_frame.pack(pady=10)
- # Add the download quality dropdown and download button
- download_quality_var = tk.StringVar(value='Best')
- download_quality_combo = ttk.Combobox(control_frame, textvariable=download_quality_var, values=['Best', 'Medium', 'Low', '360p', '480p', '720p', 'MP3'])
- download_quality_combo.pack(side=tk.LEFT, padx=5)
- ttk.Button(control_frame, text="Download YT", command=download_youtube).pack(side=tk.LEFT, padx=5)
- ttk.Button(control_frame, text="Preview", command=preview_selected_link).pack(side=tk.LEFT, padx=5)
- ttk.Button(control_frame, text="Stop Preview", command=stop_preview).pack(side=tk.LEFT, padx=5)
- ttk.Button(control_frame, text="Capture Video", command=capture_video).pack(side=tk.LEFT, padx=5)
- ttk.Button(control_frame, text="Record Audio", command=record_audio).pack(side=tk.LEFT, padx=5)
- ttk.Button(control_frame, text="Stop Recording", command=stop_recording).pack(side=tk.LEFT, padx=5)
- ttk.Button(control_frame, text="Capture Screenshots", command=capture_screenshots).pack(side=tk.LEFT, padx=5)
- ttk.Button(control_frame, text="Play VLC", command=play_vlc, style="TButton").pack(side=tk.LEFT, padx=5)
- toggle_fullscreen_button = ttk.Button(control_frame, text="Toggle Fullscreen", command=lambda: toggle_fullscreen(preview_frame))
- toggle_fullscreen_button.pack(side=tk.LEFT, padx=5)
- preview_frame = ttk.Frame(root)
- preview_frame.pack(fill=tk.BOTH, expand=True)
- instance = vlc.Instance()
- player = instance.media_player_new()
- video_panel = ttk.Frame(preview_frame)
- video_panel.pack(fill=tk.BOTH, expand=True)
- slider = ttk.Scale(preview_frame, from_=0, to=1, orient=tk.HORIZONTAL, command=on_slider_change)
- slider.pack(fill=tk.X)
- current_time_label = ttk.Label(preview_frame, text="00:00:00")
- current_time_label.pack(side=tk.LEFT)
- total_time_label = ttk.Label(preview_frame, text="00:00:00")
- total_time_label.pack(side=tk.RIGHT)
- channels_info = {}
- def toggle_fullscreen(frame):
- if not hasattr(toggle_fullscreen, "is_fullscreen"):
- toggle_fullscreen.is_fullscreen = False
- if toggle_fullscreen.is_fullscreen:
- root.attributes('-fullscreen', False)
- frame.pack(fill=tk.BOTH, expand=True)
- result_frame.pack(fill=tk.BOTH, expand=True)
- toggle_fullscreen.is_fullscreen = False
- else:
- root.attributes('-fullscreen', True)
- frame.pack(fill=tk.BOTH, expand=True)
- result_frame.pack_forget()
- toggle_fullscreen.is_fullscreen = True
- root.mainloop()
Add Comment
Please, Sign In to add comment