Skip to content

Instantly share code, notes, and snippets.

Last active June 5, 2023 02:18
Show Gist options
  • Save royshil/236ee8d51961768511c1fe56461c3817 to your computer and use it in GitHub Desktop.
Save royshil/236ee8d51961768511c1fe56461c3817 to your computer and use it in GitHub Desktop.
Quicrop! A minimal tool for cropping video with Tkinter and ffmpeg-python
import tkinter as tk
from tkVideoPlayer import TkinterVideo
from ffmpeg import FFmpeg, Progress
import os
import sys
import argparse
parser = argparse.ArgumentParser(description="Crop a video")
parser.add_argument("video", metavar="video", type=str, help="video file to crop")
args = parser.parse_args()
videofile =
if not os.path.exists(videofile):
print(f"video file {videofile} doesnt exist")
# Create the main window
window = tk.Tk()
window.minsize(640, 480)
my_label = tk.Label(window)
my_label.pack(expand=True, fill="both")
videoplayer = TkinterVideo(master=my_label, scaled=True, keep_aspect=False)
videoplayer.pack(expand=True, fill="both")
progress_value = tk.IntVar(my_label)
def seek(value):
"""used to seek a specific timeframe"""
def update_scale(event):
"""updates the scale value"""
progress_slider = tk.Scale(
my_label, variable=progress_value, from_=0, to=0, orient="horizontal", command=seek
# progress_slider.bind("<ButtonRelease-1>", seek)
progress_slider.pack(side="left", fill="x", expand=True)
def update_duration(event):
"""updates the duration after finding the duration"""
duration = videoplayer.video_info()["duration"]
progress_slider["to"] = duration
canvas = tk.Canvas(videoplayer, width=0, height=0, bd=0, highlightthickness=0), y=0)
fields = ["Width:", "Height:", "X-coordinate:", "Y-coordinate:"]
entries = []
inputsroot = tk.Label(window)
for i, f in enumerate(fields):
label = tk.Label(inputsroot, text=f)
label.grid(row=i + 1, column=0)
entry_var = tk.StringVar()
entry = tk.Entry(inputsroot, textvariable=entry_var)
entry.grid(row=i + 1, column=1)
entries += [entry_var]
# Create a submit button that prints the numbers when clicked
def on_submit():
print(f"Width: {entries[0].get()}")
print(f"Height: {entries[1].get()}")
print(f"X-coordinate: {entries[2].get()}")
print(f"Y-coordinate: {entries[3].get()}")
ffmpeg = (
os.path.splitext(videofile)[0] + "_crop.mp4",
def on_start(arguments: list[str]):
print("arguments:", arguments)
def on_stderr(line):
print("stderr:", line)
def on_progress(progress: Progress):
def on_completed():
def on_terminated():
submit_button = tk.Button(inputsroot, text="Crop", command=on_submit)
submit_button.grid(row=5, column=1)
clickx = 0
origclickx = 0
clicky = 0
origclicky = 0
def vidframecoord(x, y):
fx, fy = videoplayer.video_info()["framesize"]
return (
int(x / float(videoplayer.winfo_width()) * fx),
int(y / float(videoplayer.winfo_height()) * fy),
def getorigin(e):
global clickx, clicky, origclickx, origclicky
clickx, clicky = vidframecoord(e.x, e.y)
origclickx = e.x
origclicky = e.y
canvas.configure(width=0, height=0)
def dragging(e):
dragx, dragy = vidframecoord(e.x, e.y)
realx = min(clickx, dragx)
realy = min(clicky, dragy)
width = abs(clickx - dragx)
height = abs(clicky - dragy)
# make sure width and height are within image bounds
if (realx + width) > videoplayer.video_info()["framesize"][0]:
width = videoplayer.video_info()["framesize"][0] - realx
if (realy + height) > videoplayer.video_info()["framesize"][1]:
height = videoplayer.video_info()["framesize"][1] - realy
# hack the canvas to show up in the right place over the image
canvasx = min(origclickx, e.x)
canvasy = min(origclicky, e.y)
rw = abs(origclickx - e.x)
rh = abs(origclicky - e.y), y=canvasy)
canvas.configure(width=rw + 1, height=rh + 1)
canvas.create_rectangle(0, 0, rw, rh, fill="#ff0000")
canvas.create_line(0, 0, rw, rh, fill="#ff5555")
canvas.create_line(0, rh, rw, 0, fill="#ff5555")
canvas.create_text(rw / 2, rh / 2, text="CROP", fill="white")
def loopvideo(e):
videoplayer.bind("<<SecondChanged>>", update_scale)
videoplayer.bind("<<Duration>>", update_duration)
videoplayer.bind("<<Ended>>", loopvideo) # play the video
window.bind("<Escape>", lambda e: window.destroy())
videoplayer.bind("<Button 1>", getorigin)
videoplayer.bind("<B1-Motion>", dragging)
# Start the GUI event loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment