Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import subprocess
- import tkinter as tk
- from tkinter import filedialog, messagebox
- def select_input_file():
- input_file = filedialog.askopenfilename(title="Select Input Video File", filetypes=[("MP4 Files", "*.mp4")])
- input_file_entry.delete(0, tk.END)
- input_file_entry.insert(0, input_file)
- def select_output_directory():
- output_directory = filedialog.askdirectory(title="Select Output Directory for Images")
- output_directory_entry.delete(0, tk.END)
- output_directory_entry.insert(0, output_directory)
- def select_output_file():
- output_file = filedialog.asksaveasfilename(title="Save Output Video File", defaultextension=".mp4", filetypes=[("MP4 Files", "*.mp4")])
- output_file_entry.delete(0, tk.END)
- output_file_entry.insert(0, output_file)
- def extract_video_segment():
- input_file = input_file_entry.get()
- output_file = output_file_entry.get()
- start_time = start_time_entry.get()
- end_time = end_time_entry.get()
- if not os.path.isfile(input_file):
- messagebox.showerror("Error", "Input file does not exist.")
- return
- if not output_file:
- messagebox.showerror("Error", "Please specify an output file.")
- return
- command = [
- 'ffmpeg', '-y', '-i', input_file,
- '-ss', start_time, '-to', end_time,
- '-c:v', 'copy', '-c:a', 'copy',
- output_file
- ]
- try:
- result = subprocess.run(command, check=True, capture_output=True, text=True)
- messagebox.showinfo("Success", "Video segment extracted successfully.")
- except subprocess.CalledProcessError as e:
- messagebox.showerror("Error", f"FFmpeg error: {e.stderr}")
- def extract_pictures():
- input_file = input_file_entry.get()
- output_directory = output_directory_entry.get()
- start_time = start_time_entry.get()
- end_time = end_time_entry.get()
- frame_interval = frame_interval_entry.get()
- if not os.path.isfile(input_file):
- messagebox.showerror("Error", "Input file does not exist.")
- return
- if not output_directory:
- messagebox.showerror("Error", "Please specify an output directory.")
- return
- if not frame_interval:
- messagebox.showerror("Error", "Please specify a frame interval.")
- return
- output_pattern = os.path.join(output_directory, 'frame_%04d.png')
- command = [
- 'ffmpeg', '-y', '-i', input_file,
- '-ss', start_time, '-to', end_time,
- '-vf', f'fps=1/{frame_interval}', # Extract one frame per interval
- output_pattern
- ]
- try:
- result = subprocess.run(command, check=True, capture_output=True, text=True)
- messagebox.showinfo("Success", "Pictures extracted successfully.")
- except subprocess.CalledProcessError as e:
- messagebox.showerror("Error", f"FFmpeg error: {e.stderr}")
- # Set up the GUI
- root = tk.Tk()
- root.title("Najeeb Video Segment and Picture Extractor")
- # Input file selection
- tk.Label(root, text="Input File:").grid(row=0, column=0, padx=5, pady=5)
- input_file_entry = tk.Entry(root, width=50)
- input_file_entry.grid(row=0, column=1, padx=5, pady=5)
- tk.Button(root, text="Browse...", command=select_input_file).grid(row=0, column=2, padx=5, pady=5)
- # Start time entry
- tk.Label(root, text="Start Time (HH:MM:SS):").grid(row=1, column=0, padx=5, pady=5)
- start_time_entry = tk.Entry(root, width=20)
- start_time_entry.grid(row=1, column=1, padx=5, pady=5)
- start_time_entry.insert(0, "00:00:00") # Default value
- # End time entry
- tk.Label(root, text="End Time (HH:MM:SS):").grid(row=2, column=0, padx=5, pady=5)
- end_time_entry = tk.Entry(root, width=20)
- end_time_entry.grid(row=2, column=1, padx=5, pady=5)
- end_time_entry.insert(0, "00:00:00") # Default value
- # Output file selection for video
- tk.Label(root, text="Output File (Video):").grid(row=3, column=0, padx=5, pady=5)
- output_file_entry = tk.Entry(root, width=50)
- output_file_entry.grid(row=3, column=1, padx=5, pady=5)
- tk.Button(root, text="Browse...", command=select_output_file).grid(row=3, column=2, padx=5, pady=5)
- # Extract video segment button
- tk.Button(root, text="Extract Segment", command=extract_video_segment).grid(row=4, column=0, columnspan=3, pady=10)
- # Frame interval entry for image extraction
- tk.Label(root, text="Frame Interval (seconds):").grid(row=5, column=0, padx=5, pady=5)
- frame_interval_entry = tk.Entry(root, width=20)
- frame_interval_entry.grid(row=5, column=1, padx=5, pady=5)
- # Output directory selection for images
- tk.Label(root, text="Output Directory (Images):").grid(row=6, column=0, padx=5, pady=5)
- output_directory_entry = tk.Entry(root, width=50)
- output_directory_entry.grid(row=6, column=1, padx=5, pady=5)
- tk.Button(root, text="Browse...", command=select_output_directory).grid(row=6, column=2, padx=5, pady=5)
- # Extract pictures button
- tk.Button(root, text="Extract Pictures", command=extract_pictures).grid(row=7, column=0, columnspan=3, pady=10)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement