wjs-reframing-video
Original:🇺🇸 English
Translated
1 scriptsChecked / no sensitive code detected
Use when the user wants to convert a video between horizontal and vertical orientations while preserving the inverted aspect ratio (16:9 ↔ 9:16, 4:3 ↔ 3:4, 21:9 ↔ 9:21). The skill crops a narrow band from the source and tracks the active speaker — the person whose mouth is moving — via MediaPipe face landmarks and mouth-aspect-ratio variance, so the talker stays in frame even when other people are visible. Triggers — "横转竖", "竖转横", "做成竖屏发抖音/视频号/小红书", "16:9 to 9:16", "make this vertical for Reels / TikTok / YouTube Shorts", "crop to portrait", "convert to landscape".
5installs
Sourcejianshuo/claude-skills
Added on
NPX Install
npx skill4agent add jianshuo/claude-skills wjs-reframing-videoTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →wjs-reframing-video
Convert a video's orientation by cropping a narrow band from the source — not by physically rotating it. The crop window follows the active speaker (the face whose mouth is moving), not just the largest or most-confident face. A sidecar records the crop plan, the per-segment speaker decisions, and the parameters used. The original input is never modified.
.crop.jsonWhen to use
- Repurposing a 16:9 podcast / interview / talk for vertical short-video platforms (WeChat Channels 视频号, Douyin 抖音, Xiaohongshu 小红书, YouTube Shorts, TikTok, Reels).
- Repurposing a 9:16 phone recording for horizontal players (YouTube long-form, blog embeds).
- Repurposing 4:3 archive footage for 3:4 mobile, or vice versa.
The output aspect is the source aspect with width and height swapped — 16:9 → 9:16, not "letterboxed 16:9 in a 9:16 frame".
When NOT to use
- Multi-person Q&A where each face needs its own crop — this skill picks one crop track per video. For per-speaker split renders, use wjs-editing-multicam instead.
- Animated content / B-roll with no faces — falls back to center crop, usually wrong for the intent.
- Heavy camera motion in the source (handheld pan/zoom) — the face tracker amplifies camera shake. Stabilize first.
- Source already at target aspect — no work to do.
What this skill IS — and IS NOT
| Is | Is not |
|---|---|
| Visual active-speaker detection via MAR (mouth-aspect-ratio) variance | Audio-visual fusion (audio energy + lip motion cross-correlated) |
| Stable face tracking across frames by center-distance matching | Re-identification across long gaps / occlusions |
| Speaker-aligned segments with hysteresis to prevent flicker | Frame-by-frame switching on every flicker |
| |
Hard cuts between segments, fixed crop within each segment ( | Smooth panning that drifts during a speaker's turn (opt-in |
| Audio stream-copy (bit-exact) | Audio reprocessing / re-encoding |
MediaPipe Tasks | Per-frame neural inpainting / out-painting |
One | Frame-by-frame Python compositor |
Falls back to "largest face" automatically when no one is talking (silence, music-only stretches).
Dependencies
bash
pip install mediapipe opencv-python numpy(MediaPipe lives outside the standard Python distribution; ffmpeg and ffprobe must be on .)
PATHFirst-run model download: MediaPipe 0.10+ uses the Tasks API, which needs a model file (~4 MB). On the first call, downloads it to and caches it for subsequent runs. The script fails offline on first run.
face_landmarker.taskcrop.py~/.claude/skills/wjs-reframing-video/models/Range limitation: The bundled landmarker is tuned for faces within ~2 m of the camera (selfie / podcast / interview distance). Wide event shots with small faces may not detect — sample a frame first to confirm.
Crop math
Source aspect = . Target aspect = (inverted). Compute crop window:
W / HH / W| Source orientation | Crop window |
|---|---|
| Horizontal (W > H) → Portrait | |
| Portrait (W < H) → Horizontal | |
For 1920×1080 → portrait, , . Final scale to 1080×1920 (upscale ~1.78×).
For 1080×1920 → landscape, , . Final scale to 1920×1080.
W_crop = 608H_crop = 1080W_crop = 1080H_crop = 608Override the final size via if you want native crop dimensions instead of upscaling.
--output-size 1080x1920Pipeline
- Probe input dimensions, fps, duration via ffprobe.
- Decide orientation — auto from aspect (to override).
--target portrait|landscape - Sample frames at (default 5; high enough to catch mouth motion — Nyquist for speech is ~10 Hz, we need at least 4–5 fps).
--sample-fps - Detect face landmarks per sampled frame with MediaPipe Tasks (478 landmarks). For each detected face record: center, size proxy, MAR (mouth-aspect-ratio = inner-lip vertical distance / horizontal mouth-corner distance).
FaceLandmarker - Track faces across frames by center-distance matching → each face gets a stable .
face_id - Per-sample active speaker: for each face track, variance of MAR over a sliding window (, default 1 s). The face with the highest variance is "speaking". Below
--mar-var-window-sec, no one is speaking → fall back to largest face.--mar-var-threshold - Hysteresis: a candidate switch only commits if the new speaker is stable for (default 1.5 s). Shorter flickers are squashed — prevents the crop from ping-ponging on a one-frame mis-detection.
--min-segment-sec - Speaker-aligned segments → for each segment, mean (cx, cy) of that speaker's face over the segment becomes the crop center, fixed for the full duration of the segment.
- Build a ffmpeg step-function expression (, default) that holds each segment's crop position constant and jumps instantly at each segment boundary — the visual feel of a real cut between camera angles. (
--motion cutswitches to piecewise-linear pan between segment midpoints; rarely the right call for talking-head content because the camera appears to drift mid-sentence.)--motion smooth - Render one ffmpeg pass — . The crop filter evaluates
crop=W:H:x='expr':y='expr', scale=OUT_W:OUT_Handxper frame natively. Audio stream-copied.y
scripts/crop.py- — sidecar with the crop plan
<input>.crop.json - — final cropped + scaled video
<input>_cropped.mp4
Sidecar schema (<input>.crop.json
)
<input>.crop.jsonjson
{
"_about": "wjs-reframing-video crop plan for cam_a.MOV. Active-speaker detected via MAR variance.",
"_help": {
"source_size": "[width, height] in pixels.",
"target_size": "[width, height] of the final rendered output.",
"crop_window": "[width, height] of the moving crop in source coords.",
"chunks": "Speaker-aligned segments: {t0, t1, cx, cy, speaker_id}.",
"face_pick_mode": "speaker = MAR-variance active-speaker; largest = old behavior.",
"speaker_id": "Stable face track id. null means no face / silence fallback."
},
"schema_version": 2,
"source": "cam_a.MOV",
"source_size": [1920, 1080],
"target": "portrait",
"target_size": [1080, 1920],
"crop_window": [608, 1080],
"face_pick_mode": "speaker",
"sample_fps": 5.0,
"mar_var_window_sec": 1.0,
"mar_var_threshold": 1.5e-4,
"min_segment_sec": 1.5,
"chunks": [
{"t0": 0.0, "t1": 4.2, "cx": 808, "cy": 540, "speaker_id": 0},
{"t0": 4.2, "t1": 11.6, "cx": 1182, "cy": 540, "speaker_id": 1},
{"t0": 11.6, "t1": 14.0, "cx": 808, "cy": 540, "speaker_id": 0}
],
"face_sample_count": 1234,
"track_count": 2
}Performance
- Detection is the slow step. On Apple Silicon at 2 fps sampling, expect ~10–20× realtime (a 30-min source detects in ~1–2 min). Bumping makes detection slower but tracking more responsive.
--sample-fps - Render is fast — single ffmpeg pass with hardware encode (on macOS). Often <1× realtime for a 1080p source.
hevc_videotoolbox - For very long sources (>200 chunks), the ffmpeg expression gets cumbersome; the script auto-downsamples chunk midpoints to keep the expression under ~200 control points.
Common pitfalls
- Mouth gestures aren't speech — a yawn, laugh, eating, or sucking-in-air all raise MAR variance. The detector can briefly mistake these for talking. For high-stakes content, eyeball the speaker timeline in the sidecar (the script prints a summary) and re-run with a different
face#N: Xs on screen (Y%)if needed.--mar-var-threshold - Side-profile or down-tilted faces — when a face is rotated >60° from camera, MediaPipe may fail to land mouth landmarks reliably, so MAR variance flatlines. The speaker fallback to "largest face" kicks in. If you have a long stretch of profile shots, consider .
--face-pick largest - Two faces with overlapping speech (interruption / talking over) — both faces have MAR variance, only one wins. The losing face is treated as listener. For accurate per-speaker tracking under crosstalk, use wjs-editing-multicam with separate cams.
- Long stretches of silence (B-roll, music) — falls back to largest face. If the largest face is wrong (e.g. a listener stays still while the speaker's mic feeds music), you'll see drift. Pre-segment around music-only sections.
- Source has burned-in lower-thirds / subtitles — for H→V, the lower band gets cropped out; for V→H, it stays but gets stretched. Strip burn-ins before running.
- Wide-angle / fish-eye lenses — landmarks miss faces near edges. Pre-correct distortion with first.
ffmpeg lenscorrection - Upscaling artifacts — is a 1.78× upscale and visible on sharp text. Render at native crop dims (
608×1080 → 1080×1920) and let the platform upscale, if you have overlays you want to keep sharp.--output-size 608x1080 - Output bitrate > platform limit — default is . WeChat Channels (视频号) caps at 10 Mbps; pass
--bitrate 12Mfor that target.--bitrate 8M