Advertisement
Najeebsk

CUT-VIDEO-PICTURE.pyw

Jun 29th, 2024 (edited)
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.94 KB | None | 0 0
  1. import os
  2. import subprocess
  3. import tkinter as tk
  4. from tkinter import filedialog, messagebox
  5.  
  6. def select_input_file():
  7.     input_file = filedialog.askopenfilename(title="Select Input Video File", filetypes=[("MP4 Files", "*.mp4")])
  8.     input_file_entry.delete(0, tk.END)
  9.     input_file_entry.insert(0, input_file)
  10.  
  11. def select_output_directory():
  12.     output_directory = filedialog.askdirectory(title="Select Output Directory for Images")
  13.     output_directory_entry.delete(0, tk.END)
  14.     output_directory_entry.insert(0, output_directory)
  15.  
  16. def select_output_file():
  17.     output_file = filedialog.asksaveasfilename(title="Save Output Video File", defaultextension=".mp4", filetypes=[("MP4 Files", "*.mp4")])
  18.     output_file_entry.delete(0, tk.END)
  19.     output_file_entry.insert(0, output_file)
  20.  
  21. def extract_video_segment():
  22.     input_file = input_file_entry.get()
  23.     output_file = output_file_entry.get()
  24.     start_time = start_time_entry.get()
  25.     end_time = end_time_entry.get()
  26.  
  27.     if not os.path.isfile(input_file):
  28.         messagebox.showerror("Error", "Input file does not exist.")
  29.         return
  30.  
  31.     if not output_file:
  32.         messagebox.showerror("Error", "Please specify an output file.")
  33.         return
  34.  
  35.     command = [
  36.         'ffmpeg', '-y', '-i', input_file,
  37.         '-ss', start_time, '-to', end_time,
  38.         '-c:v', 'copy', '-c:a', 'copy',
  39.         output_file
  40.     ]
  41.  
  42.     try:
  43.         result = subprocess.run(command, check=True, capture_output=True, text=True)
  44.         messagebox.showinfo("Success", "Video segment extracted successfully.")
  45.     except subprocess.CalledProcessError as e:
  46.         messagebox.showerror("Error", f"FFmpeg error: {e.stderr}")
  47.  
  48. def extract_pictures():
  49.     input_file = input_file_entry.get()
  50.     output_directory = output_directory_entry.get()
  51.     start_time = start_time_entry.get()
  52.     end_time = end_time_entry.get()
  53.     frame_interval = frame_interval_entry.get()
  54.  
  55.     if not os.path.isfile(input_file):
  56.         messagebox.showerror("Error", "Input file does not exist.")
  57.         return
  58.  
  59.     if not output_directory:
  60.         messagebox.showerror("Error", "Please specify an output directory.")
  61.         return
  62.  
  63.     if not frame_interval:
  64.         messagebox.showerror("Error", "Please specify a frame interval.")
  65.         return
  66.  
  67.     output_pattern = os.path.join(output_directory, 'frame_%04d.png')
  68.     command = [
  69.         'ffmpeg', '-y', '-i', input_file,
  70.         '-ss', start_time, '-to', end_time,
  71.         '-vf', f'fps=1/{frame_interval}',  # Extract one frame per interval
  72.         output_pattern
  73.     ]
  74.  
  75.     try:
  76.         result = subprocess.run(command, check=True, capture_output=True, text=True)
  77.         messagebox.showinfo("Success", "Pictures extracted successfully.")
  78.     except subprocess.CalledProcessError as e:
  79.         messagebox.showerror("Error", f"FFmpeg error: {e.stderr}")
  80.  
  81. # Set up the GUI
  82. root = tk.Tk()
  83. root.title("Najeeb Video Segment and Picture Extractor")
  84.  
  85. # Input file selection
  86. tk.Label(root, text="Input File:").grid(row=0, column=0, padx=5, pady=5)
  87. input_file_entry = tk.Entry(root, width=50)
  88. input_file_entry.grid(row=0, column=1, padx=5, pady=5)
  89. tk.Button(root, text="Browse...", command=select_input_file).grid(row=0, column=2, padx=5, pady=5)
  90.  
  91. # Start time entry
  92. tk.Label(root, text="Start Time (HH:MM:SS):").grid(row=1, column=0, padx=5, pady=5)
  93. start_time_entry = tk.Entry(root, width=20)
  94. start_time_entry.grid(row=1, column=1, padx=5, pady=5)
  95. start_time_entry.insert(0, "00:00:00")  # Default value
  96.  
  97. # End time entry
  98. tk.Label(root, text="End Time (HH:MM:SS):").grid(row=2, column=0, padx=5, pady=5)
  99. end_time_entry = tk.Entry(root, width=20)
  100. end_time_entry.grid(row=2, column=1, padx=5, pady=5)
  101. end_time_entry.insert(0, "00:00:00")  # Default value
  102.  
  103. # Output file selection for video
  104. tk.Label(root, text="Output File (Video):").grid(row=3, column=0, padx=5, pady=5)
  105. output_file_entry = tk.Entry(root, width=50)
  106. output_file_entry.grid(row=3, column=1, padx=5, pady=5)
  107. tk.Button(root, text="Browse...", command=select_output_file).grid(row=3, column=2, padx=5, pady=5)
  108.  
  109. # Extract video segment button
  110. tk.Button(root, text="Extract Segment", command=extract_video_segment).grid(row=4, column=0, columnspan=3, pady=10)
  111.  
  112. # Frame interval entry for image extraction
  113. tk.Label(root, text="Frame Interval (seconds):").grid(row=5, column=0, padx=5, pady=5)
  114. frame_interval_entry = tk.Entry(root, width=20)
  115. frame_interval_entry.grid(row=5, column=1, padx=5, pady=5)
  116.  
  117. # Output directory selection for images
  118. tk.Label(root, text="Output Directory (Images):").grid(row=6, column=0, padx=5, pady=5)
  119. output_directory_entry = tk.Entry(root, width=50)
  120. output_directory_entry.grid(row=6, column=1, padx=5, pady=5)
  121. tk.Button(root, text="Browse...", command=select_output_directory).grid(row=6, column=2, padx=5, pady=5)
  122.  
  123. # Extract pictures button
  124. tk.Button(root, text="Extract Pictures", command=extract_pictures).grid(row=7, column=0, columnspan=3, pady=10)
  125.  
  126. root.mainloop()
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement