Communitygithub.com

muyunxue/dance-mmd-subtitle-kit

為MMD舞蹈影片設計的人物避讓字幕佈局——測量舞者逐幀包圍盒、計算每句歌詞的聯集區域、將文字安全放置在側邊/頂部欄位,並在每一幀驗證零重疊。當被要求「字幕不要擋住人物/完全不擋/避讓」或文字與翅膀/身體重疊時載入此技能。

dance-mmd-subtitle-kit 是什麼?

這項技能專為MMD舞蹈影片中的字幕避讓(人物不被遮擋)設計,提供兩種目標模式:「完全不擋」——強制每幀字框與人物遮罩交集為0,文字僅能置於兩側或頂部窄欄,字型尺寸受限;「盡量不擋」——允許壓到翅膀邊緣或背景,但臉部和身體必須乾淨,可繼續使用大字風格。 核心演算法以1/4解析度(640×270)快速計算人物包圍盒(bbox):利用背景板做幀差、設定閾值38、透過MaxFilter(5)膨脹後取最大連通域,再還原至全解析度。按每句歌詞的視窗將各幀bbox聯集,算出最寬範圍後將文字放置在左側、右側、頂部或底部,並按可用寬度縮小字號。關鍵步驟是逐幀複核:將每個字框映射回640×270,與每幀人物遮罩求交集,若worst hit不為0則整體縮小字號再驗,直到0。 支援平台包括Claude Code與其他能執行Python腳本的開發工具。實作可直接複用`字幕\build\avoid_side2.py`(推薦完整版),其中包含pass1測bbox→佈局→驗證→渲染→關鍵幀的完整流程。使用者需更換常數(SRC、OUT、T0/T1、CUES、EL中的字號/位置候選、左右邊界)以適應不同片段。 舊片實測顯示:固定底部排版有6/6句壓到人物(遮擋率7%–12%),而側欄避讓則0/6句壓到人物(單句遮擋≤0.2%),逐幀複核達到0像素。人物在畫面中央且翅膀展開時佔寬48%–82%,近景特寫時幾乎佔滿,此時只能將文字放在上下角。排版策略建議主歌詞放左欄、點綴詞放右欄、長句擺頂部、底部留小字,並注意字框估算要保守(ASCII寬≈fs×0.66、CJK≈fs×1.06,留10–14px padding)。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/muyunxue/dance-mmd-subtitle-kit/tree/HEAD/skills/avoid-subtitles

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

人物避讓字幕(完全/盡量不擋人)

0. 兩種目標要分清

  • 「完全不擋」:每個字框與每一幀人物 mask 的交集 = 0。代價是字只能放兩側/頂部窄欄,字號受限。
  • 「盡量不擋」:允許壓翅膀邊緣/背景,但臉和身體必須乾淨。可以繼續用大字風格(S4/S6 等),只檢查近景幀。

本技能預設做「完全不擋」並給出可驗證資料;如果使用者說「炸裂/大字/藝術字」,大概率是後者。

1. 核心演算法(快版:只求 bbox)

不再逐像素求精確 matte,只需要人物包圍盒,用 1/4 解析度算,極快:

  1. 背景板 bg_half.npy(1280×540,見 subject-color-grade 的 2.1/3.1)。
  2. 每幀縮到 640×270,與背景板做差:d = |frame-bg|.max(axis=2),閾值 38(低閾值會把 LOVE 燈牌/光斑算進來)。
  3. MaxFilter(5) 膨脹 → 最大連通域(BFS,640×270 上很快)→ 取 bbox ×4 還原到全解析度。
  4. 按歌詞句視窗把每幀 bbox 併起來(一句內人物會動,用整句最寬範圍),得到每句 bounds = (left,right,top,bottom)。
  5. 排版:文字列放在 left-50 左側 / right+50 右側 / 頂部 / 底部;字號按可用寬度收縮。
  6. 逐幀複核:把每個字框映射回 640×270,與每幀 mask 求交集,報告 worst hit;不為 0 就整體縮字號再驗,直到 0。

2. 可直接複用的實現

字幕\build\avoid_side2.py(推薦,完整版:pass1 測 bbox → layout → verify → 渲染 → 關鍵幀):

$py='C:\Users\35530\.dsh\dsh-runtimes\dsh-primary-runtime\dependencies\python\python.exe'
& $py '字幕\build\avoid_side2.py'

它固定處理舊片(0001-4956)的 15.4s 片段;換片/換段時改這些常數:

  • SRC / OUT / T0,T1
  • CUES(每句起止,相對片段)
  • EL 裡的字號/位置候選(L/R 列、y、fs)
  • LEFT_MARGIN/右側可用寬度邏輯 產物:NEON_avoid_15s.ass + 渲染影片 + 關鍵幀表。 舊版(合成演示):avoid_demo.py、avoid_debug.py、avoid_layout.py(佔用熱區圖 分析_佔位與選位.jpg)。

3. 本專案的實測資料(舊片 0:51–1:06.4)

  • 固定底部排版:6/6 句壓到人物,人物區域遮擋率 7%–12%。
  • 側欄避讓:0/6 句壓到人物,單句遮擋 ≤0.2%;逐幀複核 0 像素。
  • 每句可用側欄(舊片,全 2560 寬):左欄約 0–500px,右欄約 2100–2560px,具體每句不同,必須現算。
  • 人物在畫面中央、翅膀展開時佔寬約 48%–82%,近景/特寫時幾乎佔滿 → 特寫幀只能把字放上下角。

4. 排版策略(不擋人時也好看)

  • 主歌詞放左欄、點綴詞放右欄,形成「左右對唱」的觀感;
  • 頂部放長句(MAKING EVERYBODY GO 之類),底部只留小字/等寬資訊;
  • 每句只有一個「視覺主角」,其餘詞做小字,避免整句全大導致側欄放不下;
  • 字框估算要保守:ASCII 寬 ≈ fs×0.66、CJK ≈ fs×1.06;留 10–14px pad。

5. 驗證清單

  • 每句視窗的 bbox 是否漏掉翅膀尖/頭頂(看 分析_佔用區域.jpg)
  • 逐幀複核 worst hit = 0(腳本會列印)
  • 抽 3–4 幀看:字是否貼邊/被裁、是否壓到小指/翅膀
  • 近景/特寫幀單獨抽查
  • 若允許「盡量不擋」,記錄哪些幀壓到了翅膀(可接受)

6. 已知坑

  • 背景 LOL 燈牌/光斑會被幀差誤判 → 閾值 38 + 最大連通域通常能去掉;仍不行就加空間先驗(人物在中央)。
  • 人物投影可能被當成人物(bbox 變大)→ 對避讓是安全的(你會避開影子),但會讓側欄更窄。
  • 濾鏡 MaxFilter 在 640×270 上 ≈ 外擴 8px 全解析度,複核時不要再額外放大 pad。
  • 若換了新影片/新機位,所有 bounds 都要重算,不能複用舊片數字。

Individual skills in this repo

This repo contains 4 individual skills — each has its own dedicated page.

muyunxue/dance-mmd-subtitle-kit

Hard rules, templates, ffmpeg commands and failure diagnosis for authoring ASS/SSA subtitles and burning them with libass on this machine. Load this before writing any ASS file, when a burn "succeeds" but no text appears, when text is misplaced/too big, when \p1 drawings are offset, or when you need the transparent-overlay export recipe. Contains the exact pitfalls hit in this project (timestamp format, style colour trailing ampersand, fontsdir, drawing anchor, chained \t, Permanent Marker sizing).

muyunxue/dance-mmd-subtitle-kit

End-to-end recipe for making subtitles for the MMD dance videos in this workspace (字幕/ project) — probe the source, pick a segment, map LRC cues, choose a style, build ASS, burn, verify, and deliver. Load this when starting or resuming any subtitle task (静态稿/5s小样/30s小样/全片/透明叠加层), when a render "succeeded" but the video has no text, or when you need this project's paths, font list, cue sheet and acceptance criteria.

muyunxue/dance-mmd-subtitle-kit

Design and deliver subtitle style mockups and animated comparison samples for the MMD/K-pop dance videos — S1–S6 style catalogue (Y2K chrome, minimal kinetic, glitch, mix&match editorial, graphic emphasis, hand-marker), PIL/numpy effect recipes, font list, contact-sheet/comparison video conventions, and the parallel-subagent workflow used for S4 vs S6. Load this when asked 出几个静态帧方案/参考女团PV字幕/做个小样/对比几个风格, or when choosing a visual direction before building a full-length subtitle track.

muyunxue/dance-mmd-subtitle-kit

Subject-aware colour grading for the MMD dance videos in this workspace — make the dancer stand out (background dim/cool/desat, subject lift/warm/sharpen/bloom) using either a soft stage-light ellipse or a hard character matte, with ffmpeg recipes, measured parameter sets and known failure modes (cast-shadow matte contamination, banding, truncated sources). Load this when asked to 调色/突出人物/压背景/统一色调, or when a graded clip shows weird bright blobs around the dancer.

相關技能