Ffmpeg Compose Mask
Context: $ARGUMENTS
Advanced compositing and channel surgery. If you are keying green-screens, see ffmpeg-chromakey. If you are grading color, see ffmpeg-lut-grade. This skill is for pixel-exact compositing math, alpha plumbing, and plane-level operations.
Quick start
- Composite A and B through a mask (white→A, black→B): → Step 1, 2 (
maskedmerge) - Add an alpha channel from a separate mask video: → Step 2 (
alphamerge) - Pull mask out of an RGBA/yuva file: → Step 2 (
alphaextract) - Warp one video using another as a displacement map: → Step 2 (
displace) - Grab a single plane (Y, U, V, R, G, B, A): → Step 2 (
extractplanes) - Rebuild image from separated planes: → Step 2 (
mergeplanes) - Chromatic aberration / per-channel pixel shift: → Step 2 (
rgbashift) - Apply a Photoshop blend mode (screen, multiply, overlay...): → Step 2 (
blend) - Fill connected pixel region (Photoshop paint bucket): → Step 2 (
floodfill)
When to use
- Roto-style compositing where you already have (or can compute) a per-pixel matte.
- Alpha plumbing: moving masks between files, reconstructing RGBA from split streams.
- Plane surgery: YUV/RGB channel isolation, swapping, rebuilding across pixel formats.
- Procedural effects: displacement warps, chromatic aberration, blend-mode layers.
- Edge-linking (
hysteresispaired withedgedetect), mask refinement (maskfun,maskedthreshold,maskedclamp). - Pixel remapping (
remap), connected-region filling (floodfill).
Step 1 — Identify sources and the mask
Count your inputs. maskedmerge and displace need three; alphamerge and blend need two; alphaextract, extractplanes, rgbashift, maskfun, floodfill need one.
Verify each input with ffprobe before building the filter graph:
ffprobe -v error -select_streams v:0 -show_entries stream=width,height,pix_fmt,r_frame_rate -of default=nw=1 INPUT
All inputs to multi-input filters must share width, height, and pixel format. If they don't, scale/convert first (scale=W:H, format=yuva420p, fps=FPS).
Step 2 — Pick the compositing operation
Masked merge (3 inputs: source0, source1, mask)
Mask pixel value drives the blend: white = take from source0, black = from source1, gray = weighted mix.
ffmpeg -i A.mp4 -i B.mp4 -i MASK.mp4 \
-filter_complex "[2:v]format=gray[m];[0:v][1:v][m]maskedmerge[out]" \
-map "[out]" -c:v libx264 -crf 18 out.mp4
Options: planes (bitmask of planes to process; default 0xF = all).
Alpha merge (2 inputs: video + grayscale mask)
Bolts a grayscale matte onto the primary input as alpha. Output is yuva-format.
ffmpeg -i video.mp4 -i mask.mp4 \
-filter_complex "[0:v][1:v]alphamerge[out]" \
-map "[out]" -c:v prores_ks -profile:v 4444 out.mov
Alpha extract (1 RGBA/yuva input → grayscale mask)
ffmpeg -i rgba.mov -vf "alphaextract" -c:v ffv1 mask.mkv
Pre-/un-premultiply around compositing
Straight alpha inputs must NOT be premultiplied into RGB before compositing; premultiplied inputs must be un-multiplied before filters that expect straight, then remultiplied after.
# Composite with correct alpha math:
ffmpeg -i premul.mov -i bg.mov \
-filter_complex "[0:v]unpremultiply=inplace=1[a];\
[1:v][a]overlay=format=auto[composed];\
[composed]premultiply=inplace=1[out]" \
-map "[out]" -c:v prores_ks -profile:v 4444 out.mov
Both premultiply and unpremultiply accept inplace=1 to use the input's own alpha plane (no 2nd stream needed).
Displacement warp (3 inputs: base, xmap, ymap)
xmap and ymap are grayscale; mid-gray (128) means no shift, white = +scale, black = -scale. One map can supply both if you split.
ffmpeg -i base.mp4 -i xmap.mp4 -i ymap.mp4 \
-filter_complex "[0:v][1:v][2:v]displace=edge=smear[out]" \
-map "[out]" -c:v libx264 -crf 18 out.mp4
Options: edge ∈ {blank, smear, wrap, mirror} (default smear).
Plane surgery
Extract a single plane (always output is grayscale):
# Luma only:
ffmpeg -i in.mp4 -vf "format=yuv420p,extractplanes=y" -c:v ffv1 y.mkv
# All three at once:
ffmpeg -i in.mp4 -filter_complex "extractplanes=y+u+v[y][u][v]" \
-map "[y]" y.mkv -map "[u]" u.mkv -map "[v]" v.mkv
Rebuild planes from multiple inputs via mergeplanes with a hex selector:
# 3 gray streams → single yuv444p:
ffmpeg -i y.mkv -i u.mkv -i v.mkv \
-filter_complex "[0:v][1:v][2:v]mergeplanes=0x001020:yuv444p[out]" \
-map "[out]" out.mov
# Add a gray stream as alpha onto a yuv444p stream → yuva444p:
# [yuv][mask]mergeplanes=0x00010210:yuva444p
Swap planes within a single stream with shuffleplanes (per-plane index into input 0):
# Y=plane 0, swap U and V:
ffmpeg -i in.mp4 -vf "shuffleplanes=0:2:1:3" out.mp4
Per-channel pixel shift / chromatic aberration
ffmpeg -i in.mp4 -vf "format=gbrp,rgbashift=rh=2:bh=-2" out.mp4
Options: rh, rv, gh, gv, bh, bv, ah, av (horizontal/vertical shift in pixels per channel; default 0). edge ∈ {smear, wrap}.
Spatial blend (2 inputs)
Photoshop-style blend modes applied pixelwise between two aligned streams:
ffmpeg -i base.mp4 -i overlay.mp4 \
-filter_complex "[0:v][1:v]blend=all_mode=screen:all_opacity=0.5[out]" \
-map "[out]" out.mp4
Modes: addition, and, average, burn, darken, difference, divide, dodge, exclusion, freeze, glow, grainextract, grainmerge, hardlight, hardmix, heat, lighten, linearlight, multiply, negation, normal, or, overlay, phoenix, pinlight, reflect, screen, softdifference, softlight, subtract, vividlight, xor. Per-plane variants: c0_mode, c1_mode, c2_mode, c3_mode plus matching c0_opacity, etc. all_expr accepts arbitrary expressions on A, B, X, Y, W, H, SW, SH, T, N.
Floodfill (paint-bucket fill)
# Replace pixels at (100, 100) and all connected same-color ones with red:
ffmpeg -i in.mp4 -vf "floodfill=x=100:y=100:s0=10:s1=10:s2=10:d0=255:d1=0:d2=0" out.mp4
x, y are the seed pixel (integer). s0,s1,s2,s3 match the source pixel (must equal them exactly to flood); d0,d1,d2,d3 are the destination values. Single component = single plane match/replace.
Hysteresis (edge-linking)
Pair with edgedetect. First input = strong edges (high threshold), second = weak edges (low threshold). Weak edges connected to strong edges are kept.
ffmpeg -i in.mp4 -filter_complex \
"[0:v]split[s][w];[s]edgedetect=high=0.3:low=0.3,format=gray[strong];\
[w]edgedetect=high=0.1:low=0.1,format=gray[weak];\
[strong][weak]hysteresis=threshold=0:planes=1[out]" \
-map "[out]" out.mp4
Mask generation helpers
maskfun=low=N:high=M:fill=F:sum=S — turn input into a mask (low/high thresholds, fill color for masked pixels, sum window).
maskedthreshold=threshold=N:mode=abs|diff — two-input: output 1st where |a-b| ≤ threshold, else 2nd.
maskedclamp=undershoot=U:overshoot=O:planes=P — three-input: clamp input0 to the band defined by input1 (lower) and input2 (upper).
maskedmax, maskedmin — three-input: pick whichever of input1/input2 is farther/closer to input0 per pixel.
Step 3 — Build the filter graph (labels + `-map`)
Always name every intermediate with [label] when you have more than one branch. Every label that leaves -filter_complex must be consumed by -map "[label]" on an output.
ffmpeg \
-i source0.mp4 \
-i source1.mp4 \
-i mask.mp4 \
-filter_complex "\
[0:v]format=yuva420p[a];\
[1:v]format=yuva420p[b];\
[2:v]format=gray[m];\
[a][b][m]maskedmerge[composed];\
[composed]format=yuv420p[out_v];\
[0:a][1:a]amix=inputs=2[out_a]\
" \
-map "[out_v]" -map "[out_a]" \
-c:v libx264 -crf 18 -c:a aac -b:a 192k out.mp4
Step 4 — Verify alpha and channels
After the render, confirm pixel format and plane counts:
ffprobe -v error -show_entries stream=pix_fmt,width,height -of default=nw=1 out.mov
Expected: yuva420p, yuva444p, rgba, or argb if alpha was intended. If you see yuv420p, alpha was lost (either the container doesn't support it or a later filter/encoder dropped it; see Gotchas).
Preview the mask-only pass before committing:
ffplay -f lavfi "movie=out.mov,alphaextract"
Available scripts
scripts/compose.py— argparse-driven wrapper that builds and (optionally) runs the correct filter graph for each operation:mask-merge,alpha-merge,alpha-extract,displace,planes,blend,rgbashift. Stdlib only; supports--dry-runand--verbose.
# Preview the command:
python3 ${CLAUDE_SKILL_DIR}/scripts/compose.py mask-merge \
--source0 A.mp4 --source1 B.mp4 --mask M.mp4 --output out.mp4 --dry-run
# Run it:
python3 ${CLAUDE_SKILL_DIR}/scripts/compose.py blend \
--source0 a.mp4 --source1 b.mp4 --mode screen --output out.mp4