I will provide you a script. can you fully modify and optimize the script so that I can…
- User
I will provide you a script. can you fully modify and optimize the script so that I can use in Google colab and crop Google drive videos. you can improve user interface also. here is my script -import tkinter as tk from tkinter import filedialog, messagebox import cv2 import ffmpeg from PIL import Image, ImageTk import os
Initialize global variables
start_x, start_y, end_x, end_y = 0, 0, 0, 0 rect_id = None input_files = [] crop_areas = {} current_file_index = 0 original_width, original_height = 0, 0 canvas_width, canvas_height = 640, 480 resize_ratio = 1.0
def select_area(event): global start_x, start_y start_x, start_y = event.x, event.y print(f"Start area selected at ({start_x}, {start_y})")
def crop_area(event): global end_x, end_y, rect_id end_x, end_y = event.x, event.y if rect_id: canvas.delete(rect_id) rect_id = canvas.create_rectangle(start_x, start_y, end_x, end_y, outline='red') print(f"Crop area updated to ({end_x}, {end_y})")
def load_frames(): global frame, img_tk, input_files, current_file_index while True: files = filedialog.askopenfilenames(filetypes=[("Video files", ".mp4;.avi;.mov;.mkv")]) if not files: break input_files.extend(files) if not input_files: return current_file_index = 0 load_next_frame()
def load_next_frame(): global frame, img_tk, input_files, current_file_index, original_width, original_height, resize_ratio if current_file_index >= len(input_files): messagebox.showinfo("Info", "All rectangles drawn. Starting to crop videos.") crop_videos() return input_file = input_files[current_file_index] cap = cv2.VideoCapture(input_file) ret, frame = cap.read() cap.release() if ret: original_height, original_width = frame.shape[:2] aspect_ratio = original_width / original_height if aspect_ratio > 1: new_width = canvas_width new_height = int(canvas_width / aspect_ratio) else: new_height = canvas_height new_width = int(canvas_height * aspect_ratio) resize_ratio = original_width / new_width resized_frame = cv2.resize(frame, (new_width, new_height)) frame = cv2.cvtColor(resized_frame, cv2.COLOR_BGR2RGB) img = Image.fromarray(frame) img_tk = ImageTk.PhotoImage(image=img) canvas.create_image(0, 0, anchor=tk.NW, image=img_tk) root.update() print(f"Loaded frame from {input_file}")
def on_enter(event): global current_file_index crop_areas[input_files[current_file_index]] = (start_x, start_y, end_x, end_y) print(f"Crop area for {input_files[current_file_index]}: ({start_x}, {start_y}, {end_x}, {end_y})") current_file_index += 1 load_next_frame()
def crop_videos(): try: for input_file in input_files: parent_dir = os.path.dirname(input_file) output_dir = os.path.join(parent_dir, "CROPPED VIDEO") os.makedirs(output_dir, exist_ok=True) output_file = os.path.join(output_dir, os.path.basename(input_file)) start_x, start_y, end_x, end_y = crop_areas[input_file] # Map coordinates back to original size start_x = int(start_x * resize_ratio) start_y = int(start_y * resize_ratio) end_x = int(end_x * resize_ratio) end_y = int(end_y * resize_ratio) width = end_x - start_x height = end_y - start_y if width <= 0 or height <= 0: print(f"Invalid crop dimensions for {input_file}") continue print(f"Cropping {input_file} from ({start_x}, {start_y}) to ({end_x}, {end_y})") ffmpeg.input(input_file).crop(x=start_x, y=start_y, width=width, height=height).output(output_file).run() print(f"Video {input_file} cropped successfully!") messagebox.showinfo("Task Completed", "All videos have been cropped successfully!") except Exception as e: print(f"Error: {e}")
- Assistant