Advertisement
Najeebsk

VIDEO-RECODER.pyw

Aug 3rd, 2024 (edited)
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.43 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import filedialog, messagebox
  3. import subprocess
  4. import os
  5. import threading
  6.  
  7. class IPTVApp:
  8.     def __init__(self, root):
  9.         self.root = root
  10.         self.root.title("Najeeb IPTV and Video Recoder")
  11.         self.root.geometry("500x400")
  12.         self.root.configure(bg='#f0f0f0')
  13.  
  14.         # Title Label
  15.         self.title_label = tk.Label(root, text="Najeeb IPTV Video Recoder", font=("Helvetica", 18), bg='#f0f0f0', fg='#333')
  16.         self.title_label.pack(pady=10)
  17.  
  18.         # URL Entry
  19.         self.url_entry = tk.Entry(root, width=50, font=("Helvetica", 12))
  20.         self.url_entry.pack(pady=10)
  21.  
  22.         # Start Time
  23.         self.start_time_label = tk.Label(root, text="Start Time (HH:MM:SS):", font=("Helvetica", 12), bg='#f0f0f0', fg='#333')
  24.         self.start_time_label.pack(pady=5)
  25.         self.start_time_entry = tk.Entry(root, width=20, font=("Helvetica", 12))
  26.         self.start_time_entry.pack(pady=5)
  27.         self.start_time_entry.insert(0, "00:00:00")
  28.  
  29.         # End Time
  30.         self.end_time_label = tk.Label(root, text="End Time (HH:MM:SS):", font=("Helvetica", 12), bg='#f0f0f0', fg='#333')
  31.         self.end_time_label.pack(pady=5)
  32.         self.end_time_entry = tk.Entry(root, width=20, font=("Helvetica", 12))
  33.         self.end_time_entry.pack(pady=5)
  34.         self.end_time_entry.insert(0, "00:00:00")
  35.  
  36.         # Recode Button
  37.         self.recode_button = tk.Button(root, text="Recode Video", command=self.start_recode, bg='#4CAF50', fg='white', font=("Helvetica", 12))
  38.         self.recode_button.pack(pady=20)
  39.  
  40.         # Play Button
  41.         self.play_button = tk.Button(root, text="Play URL", command=self.play_url, bg='#2196F3', fg='white', font=("Helvetica", 12))
  42.         self.play_button.pack(pady=10)
  43.  
  44.         # Progress Label
  45.         self.progress_label = tk.Label(root, text="", font=("Helvetica", 12), bg='#f0f0f0', fg='#333')
  46.         self.progress_label.pack(pady=10)
  47.  
  48.     def play_url(self):
  49.         url = self.url_entry.get()
  50.         vlc_path = r"C:\Program Files\VideoLAN\VLC\vlc.exe"
  51.         if not os.path.isfile(vlc_path):
  52.             messagebox.showerror("Error", "VLC executable not found.")
  53.             return
  54.  
  55.         try:
  56.             subprocess.Popen([vlc_path, url])
  57.         except Exception as e:
  58.             messagebox.showerror("Error", f"Failed to play URL: {e}")
  59.  
  60.     def start_recode(self):
  61.         threading.Thread(target=self.recode_video).start()
  62.  
  63.     def recode_video(self):
  64.         url = self.url_entry.get()
  65.         start_time = self.start_time_entry.get()
  66.         end_time = self.end_time_entry.get()
  67.         output_file = filedialog.asksaveasfilename(defaultextension=".mp4",
  68.                                                    filetypes=[("MP4 files", "*.mp4"),
  69.                                                               ("MKV files", "*.mkv"),
  70.                                                               ("AVI files", "*.avi"),
  71.                                                               ("FLV files", "*.flv")])
  72.  
  73.         if not output_file:
  74.             messagebox.showwarning("Warning", "No output file selected.")
  75.             return
  76.  
  77.         try:
  78.             # Convert start and end times to seconds
  79.             start_time_seconds = self.time_to_seconds(start_time)
  80.             end_time_seconds = self.time_to_seconds(end_time)
  81.             duration_seconds = end_time_seconds - start_time_seconds
  82.  
  83.             if duration_seconds <= 0:
  84.                 raise ValueError("End time must be after start time")
  85.  
  86.             # FFmpeg command for recoding video
  87.             command = [
  88.                 'ffmpeg', '-y', '-ss', str(start_time_seconds), '-i', url,
  89.                 '-t', str(duration_seconds), '-c:v', 'libx264', '-preset', 'fast',
  90.                 '-c:a', 'aac', output_file
  91.             ]
  92.  
  93.             process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
  94.  
  95.             while True:
  96.                 line = process.stderr.readline()
  97.                 if not line:
  98.                     break
  99.                 self.update_progress(line)
  100.  
  101.             process.wait()
  102.             if process.returncode != 0:
  103.                 raise subprocess.CalledProcessError(process.returncode, command)
  104.  
  105.             self.progress_label.config(text="Video recoded successfully.")
  106.             messagebox.showinfo("Success", "Video recoded successfully.")
  107.         except subprocess.CalledProcessError as e:
  108.             stderr = e.stderr.decode() if e.stderr else 'Unknown error'
  109.             self.progress_label.config(text=f"FFmpeg error: {stderr}")
  110.             messagebox.showerror("Error", f"FFmpeg error: {stderr}")
  111.         except ValueError as e:
  112.             self.progress_label.config(text=str(e))
  113.             messagebox.showerror("Error", str(e))
  114.         except Exception as e:
  115.             self.progress_label.config(text=f"An unexpected error occurred: {e}")
  116.             messagebox.showerror("Error", f"An unexpected error occurred: {e}")
  117.  
  118.     def update_progress(self, line):
  119.         if "time=" in line:
  120.             time_str = line.split("time=")[1].split(" ")[0]
  121.             self.progress_label.config(text=f"Progress: {time_str}")
  122.  
  123.     def time_to_seconds(self, time_str):
  124.         """Convert time string (HH:MM:SS) to seconds."""
  125.         h, m, s = map(int, time_str.split(':'))
  126.         return h * 3600 + m * 60 + s
  127.  
  128. if __name__ == "__main__":
  129.     root = tk.Tk()
  130.     app = IPTVApp(root)
  131.     root.mainloop()
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement