---
agent:
  instructions: "Use this search API to find relevant content across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://framesfield.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
title: "Meta SAM 2 Masking + Wan VACE 14B Video Object Removal Pipeline ($0.42–$0.82 / 5s)"
description: "Erase boom mics, light stands, branded logos, or stray bystanders from moving video without shifting a single untouched pixel: conform smartphone VFR to 24fps CFR via FFmpeg (-fps_mode cfr), generate a temporal mask with Meta SAM 2 (~$0.02 API or $0 Apache-2.0 local), dilate the binary matte +12 pixels to swallow cast shadows, inpaint clean background via Alibaba Wan VACE 14B ($0.08/s = $0.40/5s), and optionally upscale to 4K via Topaz Video API ($0.40)."
canonical: "https://framesfield.com/workflows/sam2-masking-and-wan-vace-video-object-removal-pipeline"
---

# Meta SAM 2 Masking + Wan VACE 14B Video Object Removal Pipeline ($0.42–$0.82 / 5s)

> Erase boom mics, light stands, branded logos, or stray bystanders from moving video without shifting a single untouched pixel: conform smartphone VFR to 24fps CFR via FFmpeg (-fps_mode cfr), generate a temporal mask with Meta SAM 2 (~$0.02 API or $0 Apache-2.0 local), dilate the binary matte +12 pixels to swallow cast shadows, inpaint clean background via Alibaba Wan VACE 14B ($0.08/s = $0.40/5s), and optionally upscale to 4K via Topaz Video API ($0.40).

- **Canonical URL:** https://framesfield.com/workflows/sam2-masking-and-wan-vace-video-object-removal-pipeline
- **Difficulty:** Intermediate
- **Estimated Time:** 20 min
- **Estimated Cost:** $0.42 per 5s 720p removal · $0.82 with Topaz 4K patch finish ($0.00 fully local Apache-2.0)
- **Last Verified:** 2026-12-01
- **Tools Used:** Meta SAM 2 Video Segmentation (https://framesfield.com/specs/sam2-video), Alibaba Wan VACE 14B Video Editor (https://framesfield.com/specs/wan-vace-14b), Topaz Video Upscale API (https://framesfield.com/specs/topaz-video-upscale)

## Pipeline Architecture

01. **CFR & Audio Split** (FFmpeg (-fps_mode cfr)): Lock 24fps constant cadence and archive uncompressed 48 kHz WAV stem
02. **Track & Dilate** (Meta SAM 2 + FFmpeg): Generate temporal mask video and expand white matte +12px for cast shadows
03. **Masked Removal** (Wan VACE 14B ($0.08/s)): Queue async inpaint prompting strictly for revealed empty background
04. **4K Patch & Mux** (Topaz 4K + FFmpeg): Upscale approved 720p patch ($0.40), composite inside feathered mask, re-mux WAV

## Playbook

## Why SAM 2 + FFmpeg Dilation + Wan VACE 14B beats manual VFX paint-and-roto

Manually painting out a creeping boom mic, camera reflection, or pedestrian across 120 frames in After Effects or Nuke routinely eats two to four billable hours. Chaining [Meta SAM 2](/specs/sam2-video) (`~$0.02/clip` API or `$0` Apache-2.0 local) into [Alibaba Wan VACE 14B](/specs/wan-vace-14b) (`$0.08/sec` = `$0.40` per 5s clip, or `$0` Apache-2.0 local) solves the exact same shot in under five minutes—**provided you obey two upstream geometry rules**: constant-frame-rate locking and binary matte dilation.

## Step 1: Lock 24fps CFR & protect uncompressed production audio

iPhones and screen capture tools record variable frame rate (VFR) video. Feeding raw VFR clips straight into temporal tracking makes SAM 2 masks drift 2 to 4 frames behind camera pans. Run `--prep` in the copyable script above (verified locally on **FFmpeg 9.0.1** using `-fps_mode cfr`, replacing the deprecated `-vsync` flag) to emit:
- `plate_cfr24.mp4` — exact integer 24fps constant-frame-rate visual plate (`yuv420p`, even dimensions).
- `original_audio_48k.wav` — untouched 48 kHz dialogue and room tone ready to re-mux at Step 4.

Need custom SDR tone-mapping or broadcast mastering flags alongside CFR? Generate verified commands in our [FFmpeg Studio Command Builder](/tools/ffmpeg-command-builder).

## Step 2: Generate a temporal mask in Meta SAM 2 & dilate `+12px`

Submit `plate_cfr24.mp4` to [Meta SAM 2](/specs/sam2-video) (`fal-ai/sam2/video` or local ComfyUI-SAM2):
- Drop 1–2 **positive points (`label: 1`)** directly onto the unwanted object on frame `0` (plus an extra positive point directly on a long dark shadow if sunset light stretches it far across the pavement).
- Drop **negative points (`label: 0`)** onto any foreground actor passing in front of the target so SAM 2 preserves the actor crisply.

Next, run `--dilate sam2_mask.mp4 12`. Our local verification confirmed `boxblur=12:1 + lut threshold + boxblur=2:1` expands every white edge smoothly by **+11–12 pixels radially** (`+22px` total width/height), engulfing dark contact shadows and motion-blur fringes cleanly so zero phantom smudges survive inpainting.

## Step 3: Run Wan VACE 14B asynchronously with a clean-plate prompt

Invoke `--vace` in the script above—which calls **`queue.fal.run/fal-ai/wan-vace-14b`** (preventing 60-second HTTP 504 timeouts) with `match_input_frames_per_second: true` and `match_input_num_frames: true`.

Build your clean-plate prompt using our [V2V Object Removal & Subject Swap Prompt Builder](/skills/v2v-object-removal-and-subject-swap-prompt-builder), remembering never to name the removed object inside the positive prompt. Put unwanted shapes strictly in `negative_prompt`.

## Step 4: Patch over 4K plates & re-mux original WAV audio

If your source footage is 720p/1080p social video, run `--mux vace_clean_720p.mp4 original_audio_48k.wav final_clean.mp4` and ship. For 4K commercial masters, run `vace_clean_720p.mp4` through [Topaz Video Upscale API](/specs/topaz-video-upscale) (`$0.40` for 5s 4K), track-matte ONLY the feathered `mask_dilated.mp4` area over your untouched 4K camera plate in DaVinci Resolve, and re-mux `original_audio_48k.wav` (`$0.82` total shot cost!).

Hit a shadow smudge or frame drift? Jump straight to [Fix 73: Video Object Removal Ghost Smudges & Mask Bleed](/fixes/ai-video-object-removal-ghost-smudge-and-mask-bleed-fix) or read our complete [AI Video Object Removal & 3D Rigging Guide](/guides/ai-video-object-removal-inpainting-and-3d-rigging-guide).

## Copyable Asset: Verified CFR Conform, +12px SAM 2 Mask Dilation & Wan VACE 14B Queue Script (sam2-wan-vace-removal.sh)

```bash
#!/usr/bin/env bash
set -euo pipefail
# Verified locally on FFmpeg 9.0.1 (uses -fps_mode cfr, never removed -vsync)
# 1) ./sam2-wan-vace-removal.sh --prep raw_clip.mov
# 2) Run Meta SAM 2 (fal-ai/sam2/video or local ComfyUI) on plate_cfr24.mp4 -> sam2_mask.mp4
# 3) ./sam2-wan-vace-removal.sh --dilate sam2_mask.mp4 12
# 4) FAL_KEY="..." ./sam2-wan-vace-removal.sh --vace <plate_url> <dilated_mask_url> "Empty warm plaster wall, soft afternoon light"

MODE="${1:?Use --prep <raw> | --dilate <sam2_mask.mp4> [radius=12] | --vace <plate_url> <mask_url> <bg_prompt> | --mux <vace.mp4> <audio.wav> <final.mp4>}"

if [ "${MODE}" = "--prep" ]; then
  RAW="${2:?Pass raw video file}"
  ffmpeg -v warning -y -i "${RAW}" \
    -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2,format=yuv420p" \
    -r 24 -fps_mode cfr -c:v libx264 -crf 16 -an -movflags +faststart plate_cfr24.mp4
  ffmpeg -v warning -y -i "${RAW}" -vn -c:a pcm_s16le -ar 48000 original_audio_48k.wav || true
  echo "Saved plate_cfr24.mp4 + original_audio_48k.wav"
  exit 0
fi

if [ "${MODE}" = "--dilate" ]; then
  MASK="${2:?Pass raw SAM 2 mask video}"
  RAD="${3:-12}"
  ffmpeg -v warning -y -i "${MASK}" \
    -vf "format=gray,boxblur=${RAD}:1,lut=y='if(gt(val,18),255,0)',boxblur=2:1,format=yuv420p" \
    -r 24 -fps_mode cfr -c:v libx264 -crf 18 -an -movflags +faststart mask_dilated.mp4
  echo "Saved +${RAD}px dilated mask -> mask_dilated.mp4"
  exit 0
fi

if [ "${MODE}" = "--vace" ]; then
  PLATE_URL="${2:?Pass HTTPS plate_cfr24.mp4 URL}"
  MASK_URL="${3:?Pass HTTPS mask_dilated.mp4 URL}"
  BG_PROMPT="${4:?Describe ONLY the empty background behind the removed object}"
  SUBMIT=$(curl -sS -X POST "https://queue.fal.run/fal-ai/wan-vace-14b" \
    -H "Authorization: Key ${FAL_KEY:?Set FAL_KEY}" \
    -H "Content-Type: application/json" \
    -d "{
      \"video_url\": \"${PLATE_URL}\",
      \"mask_video_url\": \"${MASK_URL}\",
      \"prompt\": \"${BG_PROMPT}\",
      \"negative_prompt\": \"blur, ghost shadow, dark smudge, floating pole, distortion, artifacts\",
      \"resolution\": \"720p\",
      \"match_input_frames_per_second\": true,
      \"match_input_num_frames\": true,
      \"seed\": 2048
    }")
  REQ_ID=$(echo "${SUBMIT}" | jq -r '.request_id // empty')
  STATUS_URL=$(echo "${SUBMIT}" | jq -r '.status_url // empty')
  RESP_URL=$(echo "${SUBMIT}" | jq -r '.response_url // empty')
  if [ -z "${REQ_ID}" ]; then echo "VACE submit failed: ${SUBMIT}" >&2; exit 1; fi
  echo "Queued Wan VACE 14B request_id=${REQ_ID}..."
  while true; do
    POLL=$(curl -sS -H "Authorization: Key ${FAL_KEY}" "${STATUS_URL}")
    STATE=$(echo "${POLL}" | jq -r '.status // "UNKNOWN"')
    echo "[$(date +%H:%M:%S)] status=${STATE}"
    if [ "${STATE}" = "COMPLETED" ]; then break; fi
    if [ "${STATE}" = "FAILED" ] || [ "${STATE}" = "CANCELED" ]; then echo "${POLL}" >&2; exit 1; fi
    sleep 8
  done
  OUT_URL=$(curl -sS -H "Authorization: Key ${FAL_KEY}" "${RESP_URL}" | jq -r '.video.url // empty')
  curl -fsSL "${OUT_URL}" -o vace_clean_720p.mp4
  echo "Saved clean plate -> vace_clean_720p.mp4"
  exit 0
fi

if [ "${MODE}" = "--mux" ]; then
  VID="${2:?Pass vace video}"; AUD="${3:?Pass original_audio_48k.wav}"; OUT="${4:-final_clean_muxed.mp4}"
  ffmpeg -v warning -y -i "${VID}" -i "${AUD}" \
    -map 0:v:0 -map 1:a:0 -c:v copy -c:a aac -b:a 192k -ar 48000 -shortest -movflags +faststart "${OUT}"
  echo "Saved audio-synced master -> ${OUT}"
  exit 0
fi
```

## FAQ

### Why shouldn't I write 'remove the boom mic' inside the Wan VACE prompt?

Diffusion text encoders attend strongly to object nouns even when preceded by 'remove' or 'no'. Naming the boom mic increases the odds of phantom poles re-appearing inside the mask—describe only the empty plaster wall, floor planks, or foliage that belongs behind it.

### Can I run this entire pipeline offline at $0 for confidential unreleased footage?

Yes. Both Meta SAM 2 (SAM 2.1) and Alibaba Wan VACE 14B (Wan-AI/Wan2.1-VACE-14B) ship under the permissive Apache-2.0 license and execute completely offline inside local 24GB VRAM ComfyUI with zero external uploads.

### How do I keep 4K camera sharpness when Wan VACE outputs 720p?

Upscale the approved 5-second Wan VACE output through Topaz Video Upscale API ($0.40), feather mask_dilated.mp4 slightly in DaVinci Resolve or After Effects, and composite only the patched pixels over your original 4K camera plate so 90%+ of the image remains 100% untouched camera footage.
