Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

This notebook let you crop a video, allowing to specify the top-left and bottom-right coordinates of the crop rectangle. This reduces the amount of data that the next algorithms need to process.

Workflow: load the first frame, view it with a pixel grid, choose crop coordinates, preview the crop, then apply it to the whole video.

Last updated: 2026-10-15

Open in Colab Open in GitHub Codespaces

Step 1: Load the video and its first frame

Sets the input path (VIDEO), reads the first frame, and prints the frame width, height, frame rate and frame count.

Width=1920, Height=1080, FPS=59.94, Frames=222

Step 2: Show the first frame with a pixel grid

Defines show_frame(), which plots a frame with labelled x/y pixel coordinates and a yellow grid (default spacing 100 px; use a smaller grid for finer reading). It can also overlay a crop rectangle. The call below displays the first frame so you can read off the crop coordinates.

<Figure size 1400x787.5 with 1 Axes>

Step 3: Choose and preview the crop

Set top_left and bottom_right as (x, y) pixel coordinates read from the grid (x increases to the right, y increases downward, origin at the top-left of the frame). The other two corners follow automatically. The cell checks the crop is within the frame, makes the width and height even (required by the H.264 encoder), then shows the crop box (red) on the full frame and the cropped result. Re-run it with new values until you are happy.

<Figure size 1400x787.5 with 1 Axes>
Crop (500, 400, 1920, 800) -> 1420 x 400
<Figure size 1400x394.366 with 1 Axes>

Step 4: Trim the whole video

Once the preview looks right, run the cell below. It applies the crop to every frame of the video using ffmpeg (or OpenCV if ffmpeg is not installed; OpenCV drops audio). The result is saved in the same folder as the input, named <input_name>_trimmed.<extension>.