FFmpeg Crop Video to Shape Using Alpha Mask
This article explains how to crop a video into a custom shape using a static alpha mask image with FFmpeg. You will learn the exact FFmpeg command structure, the key filters required for the process, and how to output the final video with transparency preserved.
To crop a video using an alpha mask, you need two assets: your source video and a black-and-white mask image of the exact same dimensions. In the mask image, white areas represent the parts of the video you want to keep, while black areas represent the parts that will become transparent.
The FFmpeg Command for WebM (Web Friendly)
If you need a lightweight format for web use, use the VP9 codec in a WebM container, as it supports alpha transparency.
ffmpeg -i input.mp4 -i mask.png -filter_complex "[0:v][1:v]alphamerge" -c:v libvpx-vp9 -pix_fmt yuva420p output.webmThe FFmpeg Command for QuickTime (Editing Friendly)
If you plan to import the cropped video into video editing software like Apple Motion, Premiere Pro, or DaVinci Resolve, use the ProRes codec in a MOV container.
ffmpeg -i input.mp4 -i mask.png -filter_complex "[0:v][1:v]alphamerge" -c:v prores_ks -pix_fmt yuva444p10le output.movCommand Breakdown
-i input.mp4: Specifies the primary source video.-i mask.png: Specifies the black-and-white shape mask.-filter_complex "[0:v][1:v]alphamerge": This is the core filter. It takes the video stream from the first input[0:v]and merges it with the grayscale mask from the second input[1:v]. The grayscale values of the mask are converted directly into the alpha (transparency) channel of the video.-c:v libvpx-vp9/-c:v prores_ks: Selects a video codec that supports transparency. Standard H.264 (MP4) does not support alpha channels, so you must use VP9, VP8, ProRes, or Qt RLE.-pix_fmt yuva420p/yuva444p10le: Sets the pixel format. The “a” in the pixel format is critical because it instructs FFmpeg to render the alpha channel.
Handling Aspect Ratio Mismatches
If your mask image is a different size than your video, the command
will fail or stretch the mask. You can scale the mask on the fly to
match the video’s dimensions by adding the scale2ref
filter:
ffmpeg -i input.mp4 -i mask.png -filter_complex "[1:v][0:v]scale2ref[mask][video];[video][mask]alphamerge" -c:v libvpx-vp9 -pix_fmt yuva420p output.webm