Advertisement
Najeebsk

FFMPEG-TOOLS.pyw

Jul 1st, 2024
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.87 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import filedialog, messagebox
  3. import subprocess
  4. import os
  5. import yt_dlp
  6.  
  7. class VideoToolApp:
  8.     def __init__(self, root):
  9.         self.root = root
  10.         self.root.title("Najeeb Advanced Video Tool with FFmpeg")
  11.         self.root.geometry("620x460")
  12.         self.root.configure(bg='#4a4a4a')
  13.  
  14.         # Video File Path
  15.         self.video_path = tk.StringVar()
  16.         tk.Label(root, text="Video File Path:", bg='#1d1f21', fg='white').grid(row=0, column=0, padx=10, pady=10, sticky='e')
  17.         tk.Entry(root, textvariable=self.video_path, width=50, bg='#282a2e', fg='white').grid(row=0, column=1, padx=10, pady=10)
  18.         tk.Button(root, text="Browse", command=self.browse_file, bg='#373b41', fg='white').grid(row=0, column=2, padx=10, pady=10)
  19.  
  20.         # YouTube and Other URLs
  21.         self.url = tk.StringVar()
  22.         tk.Label(root, text="Video URL:", bg='#1d1f21', fg='white').grid(row=1, column=0, padx=10, pady=10, sticky='e')
  23.         tk.Entry(root, textvariable=self.url, width=50, bg='#282a2e', fg='white').grid(row=1, column=1, padx=10, pady=10)
  24.         tk.Button(root, text="Download", command=self.download_video, bg='#373b41', fg='white').grid(row=1, column=2, padx=10, pady=10)
  25.  
  26.         # IPTV URL
  27.         self.iptv_url = tk.StringVar()
  28.         tk.Label(root, text="IPTV URL:", bg='#1d1f21', fg='white').grid(row=2, column=0, padx=10, pady=10, sticky='e')
  29.         tk.Entry(root, textvariable=self.iptv_url, width=50, bg='#282a2e', fg='white').grid(row=2, column=1, padx=10, pady=10)
  30.         tk.Button(root, text="Capture", command=self.capture_iptv, bg='#373b41', fg='white').grid(row=2, column=2, padx=10, pady=10)
  31.  
  32.         # Start and End Time
  33.         self.start_time = tk.StringVar(value="00:00:00")
  34.         self.end_time = tk.StringVar(value="00:00:00")
  35.         tk.Label(root, text="Start Time (HH:MM:SS):", bg='#1d1f21', fg='white').grid(row=3, column=0, padx=10, pady=10, sticky='e')
  36.         tk.Entry(root, textvariable=self.start_time, width=20, bg='#282a2e', fg='white').grid(row=3, column=1, padx=10, pady=10, sticky='w')
  37.         tk.Label(root, text="End Time (HH:MM:SS):", bg='#1d1f21', fg='white').grid(row=4, column=0, padx=10, pady=10, sticky='e')
  38.         tk.Entry(root, textvariable=self.end_time, width=20, bg='#282a2e', fg='white').grid(row=4, column=1, padx=10, pady=10, sticky='w')
  39.  
  40.         # Output File Path
  41.         self.output_path = tk.StringVar()
  42.         tk.Label(root, text="Output File Path:", bg='#1d1f21', fg='white').grid(row=5, column=0, padx=10, pady=10, sticky='e')
  43.         tk.Entry(root, textvariable=self.output_path, width=50, bg='#282a2e', fg='white').grid(row=5, column=1, padx=10, pady=10)
  44.         tk.Button(root, text="Save As", command=self.save_as_file, bg='#373b41', fg='white').grid(row=5, column=2, padx=10, pady=10)
  45.  
  46.         # Frame Interval for Image Extraction
  47.         self.frame_interval = tk.StringVar()
  48.         tk.Label(root, text="Frame Interval (seconds):", bg='#1d1f21', fg='white').grid(row=6, column=0, padx=10, pady=10, sticky='e')
  49.         tk.Entry(root, textvariable=self.frame_interval, width=20, bg='#282a2e', fg='white').grid(row=6, column=1, padx=10, pady=10, sticky='w')
  50.  
  51.         # Buttons for operations
  52.         tk.Button(root, text="Cut Video", command=self.cut_video, bg='#cc0066', fg='white').grid(row=7, column=0, padx=10, pady=20)
  53.         tk.Button(root, text="Merge Videos", command=self.merge_videos, bg='#4c9900', fg='white').grid(row=7, column=1, padx=10, pady=20)
  54.         tk.Button(root, text="Convert Video", command=self.convert_video, bg='#cc6600', fg='white').grid(row=7, column=2, padx=10, pady=20)
  55.         tk.Button(root, text="Extract Pictures", command=self.extract_pictures, bg='#660000', fg='white').grid(row=6, column=2, padx=10, pady=20)
  56.  
  57.         # Desktop Recorder Button
  58.         tk.Button(root, text="Desktop Recorder", command=self.desktop_recorder, bg='#0066cc', fg='white').grid(row=4, column=2, padx=10, pady=20)
  59.  
  60.     def browse_file(self):
  61.         file_path = filedialog.askopenfilename(filetypes=[("Video files", "*.*")])
  62.         if file_path:
  63.             self.video_path.set(file_path)
  64.  
  65.     def save_as_file(self):
  66.         file_path = filedialog.asksaveasfilename(defaultextension=".mp4", filetypes=[("All files", "*.*")])
  67.         if file_path:
  68.             self.output_path.set(file_path)
  69.  
  70.     def download_video(self):
  71.         url = self.url.get()
  72.         start_time = self.start_time.get()
  73.         end_time = self.end_time.get()
  74.         if not url:
  75.             messagebox.showerror("Error", "Video URL must be provided")
  76.             return
  77.         try:
  78.             ydl_opts = {}
  79.             with yt_dlp.YoutubeDL(ydl_opts) as ydl:
  80.                 info = ydl.extract_info(url, download=False)
  81.                 download_path = filedialog.asksaveasfilename(defaultextension=".mp4", initialfile=info.get('title', 'video'), filetypes=[("All files", "*.*")])
  82.                 if download_path:
  83.                     ydl_opts = {'outtmpl': download_path}
  84.                     with yt_dlp.YoutubeDL(ydl_opts) as ydl:
  85.                         ydl.download([url])
  86.                     messagebox.showinfo("Success", "Video downloaded successfully")
  87.                     self.video_path.set(download_path)
  88.                     if start_time != "00:00:00" or end_time != "00:00:00":
  89.                         self.cut_video()
  90.                         self.extract_pictures()
  91.         except Exception as e:
  92.             messagebox.showerror("Error", f"Failed to download video: {str(e)}")
  93.  
  94.     def capture_iptv(self):
  95.         iptv_url = self.iptv_url.get()
  96.         output_file = filedialog.asksaveasfilename(defaultextension=".mp4", filetypes=[("All files", "*.*")])
  97.  
  98.         if not iptv_url or not output_file:
  99.             messagebox.showerror("Error", "IPTV URL and output file path must be provided")
  100.             return
  101.  
  102.         try:
  103.             subprocess.run(["ffmpeg", "-i", iptv_url, "-c", "copy", output_file], check=True)
  104.             messagebox.showinfo("Success", "IPTV stream captured successfully")
  105.             self.video_path.set(output_file)
  106.         except subprocess.CalledProcessError as e:
  107.             messagebox.showerror("Error", f"Failed to capture IPTV stream: {str(e)}")
  108.  
  109.     def cut_video(self):
  110.         input_file = self.video_path.get()
  111.         start_time = self.start_time.get()
  112.         end_time = self.end_time.get()
  113.         output_file = self.output_path.get()
  114.  
  115.         if not input_file or not start_time or not end_time or not output_file:
  116.             messagebox.showerror("Error", "All fields must be filled")
  117.             return
  118.  
  119.         try:
  120.             subprocess.run(["ffmpeg", "-i", input_file, "-ss", start_time, "-to", end_time, "-c", "copy", output_file], check=True)
  121.             messagebox.showinfo("Success", "Video cut successfully")
  122.         except subprocess.CalledProcessError as e:
  123.             messagebox.showerror("Error", f"Failed to cut video: {str(e)}")
  124.  
  125.     def merge_videos(self):
  126.         files = filedialog.askopenfilenames(filetypes=[("Video files", "*.*")])
  127.         if not files:
  128.             return
  129.  
  130.         output_file = filedialog.asksaveasfilename(defaultextension=".mp4", filetypes=[("All files", "*.*")])
  131.         if not output_file:
  132.             return
  133.  
  134.         with open('file_list.txt', 'w') as file_list:
  135.             for file in files:
  136.                 file_list.write(f"file '{file}'\n")
  137.  
  138.         try:
  139.             subprocess.run(["ffmpeg", "-f", "concat", "-safe", "0", "-i", "file_list.txt", "-c", "copy", output_file], check=True)
  140.             os.remove('file_list.txt')
  141.             messagebox.showinfo("Success", "Videos merged successfully")
  142.         except subprocess.CalledProcessError as e:
  143.             os.remove('file_list.txt')
  144.             messagebox.showerror("Error", f"Failed to merge videos: {str(e)}")
  145.  
  146.     def convert_video(self):
  147.         input_file = self.video_path.get()
  148.         output_file = self.output_path.get()
  149.  
  150.         if not input_file or not output_file:
  151.             messagebox.showerror("Error", "Input and Output paths must be filled")
  152.             return
  153.  
  154.         try:
  155.             subprocess.run(["ffmpeg", "-i", input_file, output_file], check=True)
  156.             messagebox.showinfo("Success", "Video converted successfully")
  157.         except subprocess.CalledProcessError as e:
  158.             messagebox.showerror("Error", f"Failed to convert video: {str(e)}")
  159.  
  160.     def extract_pictures(self):
  161.         input_file = self.video_path.get()
  162.         output_dir = filedialog.askdirectory()
  163.         frame_interval = self.frame_interval.get()
  164.  
  165.         if not input_file or not output_dir or not frame_interval:
  166.             messagebox.showerror("Error", "Video file, output directory, and frame interval must be provided")
  167.             return
  168.  
  169.         try:
  170.             subprocess.run(["ffmpeg", "-i", input_file, "-vf", f"fps=1/{frame_interval}", f"{output_dir}/frame%04d.png"], check=True)
  171.             messagebox.showinfo("Success", "Pictures extracted successfully")
  172.         except subprocess.CalledProcessError as e:
  173.             messagebox.showerror("Error", f"Failed to extract pictures: {str(e)}")
  174.  
  175.     def desktop_recorder(self):
  176.         output_file = filedialog.asksaveasfilename(defaultextension=".mp4", filetypes=[("All files", "*.*")])
  177.         if not output_file:
  178.             messagebox.showerror("Error", "Output file path must be provided")
  179.             return
  180.  
  181.         try:
  182.             messagebox.showinfo("Info", "Press OK to start recording. To stop recording, close the terminal window that opens.")
  183.             subprocess.run(["ffmpeg", "-f", "gdigrab", "-i", "desktop", output_file], check=True)
  184.             messagebox.showinfo("Success", "Desktop recording completed successfully")
  185.         except subprocess.CalledProcessError as e:
  186.             messagebox.showerror("Error", f"Failed to record desktop: {str(e)}")
  187.  
  188. if __name__ == "__main__":
  189.     root = tk.Tk()
  190.     app = VideoToolApp(root)
  191.     root.mainloop()
  192.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement