Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.
TypeScript agent-skillscodex-clideskop-appdubbingimage-generationlocalizationskillsttsvideo-editingvideo-generationvideo-transcriptionvideo-translation
fix: harden dubbing planning and fitting
wulien0be8c3211ac0
diff --git a/internal/service/dubbing/fit.go b/internal/service/dubbing/fit.goindex 9de6dad4..40fc3c85 100644--- a/internal/service/dubbing/fit.go+++ b/internal/service/dubbing/fit.go@@ -5,6 +5,11 @@ import ()func FitTimeline(plan []PlanItem, chunks []Chunk, cfg Config) ([]PlanItem, Report, error) {+ cfg = normalizeSpeedConfig(cfg)+ if !(cfg.SpeedMin > 0 && cfg.SpeedMin <= cfg.SpeedAccept && cfg.SpeedAccept <= cfg.SpeedMax) {+ return nil, Report{}, fmt.Errorf("invalid speed config: min %.2f accept %.2f max %.2f", cfg.SpeedMin, cfg.SpeedAccept, cfg.SpeedMax)+ }+fitted := append([]PlanItem(nil), plan...)report := Report{}@@ -15,10 +20,13 @@ func FitTimeline(plan []PlanItem, chunks []Chunk, cfg Config) ([]PlanItem, Repor}actual := 0.0- for _, idx := range chunk.Items {+ for itemIndex, idx := range chunk.Items {if idx < 0 || idx >= len(fitted) {return nil, report, fmt.Errorf("chunk %d references plan item %d out of range", chunk.ID, idx)}+ if fitted[idx].ActualDuration <= 0 {+ return nil, report, fmt.Errorf("chunk %d item %d references plan index %d with non-positive actual duration: %.3f", chunk.ID, itemIndex, idx, fitted[idx].ActualDuration)+ }actual += fitted[idx].ActualDuration}@@ -36,6 +44,9 @@ func FitTimeline(plan []PlanItem, chunks []Chunk, cfg Config) ([]PlanItem, Reporreport.Warnings = append(report.Warnings, fmt.Sprintf("chunk %d speed %.2f exceeds max %.2f", chunk.ID, speed, cfg.SpeedMax))}appliedSpeed := speed+ if appliedSpeed > cfg.SpeedMax {+ appliedSpeed = cfg.SpeedMax+ }if appliedSpeed < cfg.SpeedMin {appliedSpeed = cfg.SpeedMin}@@ -59,3 +70,17 @@ func FitTimeline(plan []PlanItem, chunks []Chunk, cfg Config) ([]PlanItem, Reporreturn fitted, report, nil}++func normalizeSpeedConfig(cfg Config) Config {+ defaults := DefaultConfig()+ if cfg.SpeedMin <= 0 {+ cfg.SpeedMin = defaults.SpeedMin+ }+ if cfg.SpeedAccept <= 0 {+ cfg.SpeedAccept = defaults.SpeedAccept+ }+ if cfg.SpeedMax <= 0 {+ cfg.SpeedMax = defaults.SpeedMax+ }+ return cfg+}diff --git a/internal/service/dubbing/fit_test.go b/internal/service/dubbing/fit_test.goindex a9755e11..f4172bde 100644--- a/internal/service/dubbing/fit_test.go+++ b/internal/service/dubbing/fit_test.go@@ -1,6 +1,9 @@package dubbing-import "testing"+import (+ "strings"+ "testing"+)func TestFitTimelineProducesMonotonicTimesAndChunkSpeed(t *testing.T) {cfg := DefaultConfig()@@ -20,3 +23,94 @@ func TestFitTimelineProducesMonotonicTimesAndChunkSpeed(t *testing.T) {t.Fatalf("MaxSpeedFactor not set: %+v", report)}}++func TestFitTimelineClampsAppliedSpeedToMaxButReportsRequiredSpeed(t *testing.T) {+ cfg := DefaultConfig()+ cfg.SpeedMax = 1.25+ cfg.SpeedAccept = 1.1+ plan := []PlanItem{+ {Index: 1, ActualDuration: 4, ChunkID: 1},+ }+ chunks := []Chunk{{ID: 1, Items: []int{0}, Start: 0, End: 2}}+ got, report, err := FitTimeline(plan, chunks, cfg)+ if err != nil {+ t.Fatalf("FitTimeline() error = %v", err)+ }+ if report.MaxSpeedFactor != 2 {+ t.Fatalf("MaxSpeedFactor = %v, want raw required speed 2", report.MaxSpeedFactor)+ }+ if got[0].SpeedFactor != cfg.SpeedMax {+ t.Fatalf("SpeedFactor = %v, want clamped max %v", got[0].SpeedFactor, cfg.SpeedMax)+ }+ if got[0].NewEnd <= chunks[0].End {+ t.Fatalf("NewEnd = %v, want overflow beyond chunk end %v", got[0].NewEnd, chunks[0].End)+ }+ if !warningsContain(report.Warnings, "exceeds max") || !warningsContain(report.Warnings, "overflows") {+ t.Fatalf("warnings = %+v, want max and overflow warnings", report.Warnings)+ }+}++func TestFitTimelineRejectsNonPositiveActualDuration(t *testing.T) {+ cfg := DefaultConfig()+ plan := []PlanItem{+ {Index: 7, ActualDuration: 1, ChunkID: 3},+ {Index: 8, ActualDuration: 0, ChunkID: 3},+ }+ chunks := []Chunk{{ID: 3, Items: []int{0, 1}, Start: 0, End: 1}}+ _, _, err := FitTimeline(plan, chunks, cfg)+ if err == nil {+ t.Fatal("FitTimeline() error = nil, want non-positive actual duration error")+ }+ msg := err.Error()+ if !strings.Contains(msg, "chunk 3") || !strings.Contains(msg, "item 1") || !strings.Contains(msg, "plan index 1") {+ t.Fatalf("error = %q, want chunk id and item/plan index", msg)+ }+}++func TestFitTimelineNormalizesZeroSpeedConfig(t *testing.T) {+ cfg := DefaultConfig()+ cfg.SpeedMin = 0+ cfg.SpeedAccept = 0+ cfg.SpeedMax = 0+ plan := []PlanItem{+ {Index: 1, ActualDuration: 1.2, ChunkID: 1},+ }+ chunks := []Chunk{{ID: 1, Items: []int{0}, Start: 0, End: 1}}+ got, report, err := FitTimeline(plan, chunks, cfg)+ if err != nil {+ t.Fatalf("FitTimeline() error = %v", err)+ }+ defaults := DefaultConfig()+ if got[0].SpeedFactor != 1.2 {+ t.Fatalf("SpeedFactor = %v, want required speed 1.2", got[0].SpeedFactor)+ }+ if warningsContain(report.Warnings, "acceptable 0") || warningsContain(report.Warnings, "max 0") {+ t.Fatalf("warnings = %+v, want normalized default speed limits %+v", report.Warnings, defaults)+ }+}++func TestFitTimelineRejectsInvalidSpeedOrder(t *testing.T) {+ cfg := DefaultConfig()+ cfg.SpeedMin = 1.4+ cfg.SpeedMax = 1.3+ plan := []PlanItem{+ {Index: 1, ActualDuration: 1, ChunkID: 1},+ }+ chunks := []Chunk{{ID: 1, Items: []int{0}, Start: 0, End: 1}}+ _, _, err := FitTimeline(plan, chunks, cfg)+ if err == nil {+ t.Fatal("FitTimeline() error = nil, want invalid speed config error")+ }+ if !strings.Contains(err.Error(), "speed config") {+ t.Fatalf("error = %q, want speed config error", err.Error())+ }+}++func warningsContain(warnings []string, substr string) bool {+ for _, warning := range warnings {+ if strings.Contains(warning, substr) {+ return true+ }+ }+ return false+}diff --git a/internal/service/dubbing/planner.go b/internal/service/dubbing/planner.goindex 7357c37e..c67b957e 100644--- a/internal/service/dubbing/planner.go+++ b/internal/service/dubbing/planner.go@@ -91,7 +91,9 @@ func (p *Planner) makeChunks(cues []Cue, plan []PlanItem) []Chunk {prev := cues[i-1]gap := cue.Start - prev.End- mustSplit := len(current.Items) >= p.cfg.MaxChunkSize || gap > p.cfg.GapTolerance+ shouldMergeShortCue := gap <= p.cfg.GapTolerance &&+ (prev.Duration() < p.cfg.MinSubtitleDuration || cue.Duration() < p.cfg.MinSubtitleDuration)+ mustSplit := len(current.Items) >= p.cfg.MaxChunkSize || !shouldMergeShortCueif mustSplit {chunks = append(chunks, current)current = Chunk{ID: len(chunks) + 1, Start: cue.Start, End: cue.End, Items: []int{i}}diff --git a/internal/service/dubbing/planner_test.go b/internal/service/dubbing/planner_test.goindex 31414806..34458758 100644--- a/internal/service/dubbing/planner_test.go+++ b/internal/service/dubbing/planner_test.go@@ -27,3 +27,43 @@ func TestPlannerMergesShortAdjacentCues(t *testing.T) {t.Fatalf("large gap should start a new chunk: %+v", plan)}}++func TestPlannerDoesNotMergeLongAdjacentCuesOnlyBecauseGapIsSmall(t *testing.T) {+ cfg := DefaultConfig()+ cues := []Cue{+ {Index: 1, Start: 0, End: 3.0, Text: "这是第一句已经足够长的字幕"},+ {Index: 2, Start: 3.2, End: 6.2, Text: "这是第二句同样足够长的字幕"},+ }+ planner := NewPlanner(cfg, NewStatisticalEstimator(), nil)+ plan, chunks, err := planner.Plan(cues, types.LanguageNameSimplifiedChinese)+ if err != nil {+ t.Fatalf("Plan() error = %v", err)+ }+ if len(plan) != 2 || len(chunks) != 2 {+ t.Fatalf("long adjacent cues should not merge only because gap is small: plan=%+v chunks=%+v", plan, chunks)+ }+ if plan[0].ChunkID == plan[1].ChunkID {+ t.Fatalf("long adjacent cues share chunk: %+v", plan)+ }+}++func TestPlannerRespectsMaxChunkSizeWhenMergingShortCues(t *testing.T) {+ cfg := DefaultConfig()+ cfg.MaxChunkSize = 2+ cues := []Cue{+ {Index: 1, Start: 0, End: 0.5, Text: "一"},+ {Index: 2, Start: 0.6, End: 1.1, Text: "二"},+ {Index: 3, Start: 1.2, End: 1.7, Text: "三"},+ }+ planner := NewPlanner(cfg, NewStatisticalEstimator(), nil)+ plan, chunks, err := planner.Plan(cues, types.LanguageNameSimplifiedChinese)+ if err != nil {+ t.Fatalf("Plan() error = %v", err)+ }+ if len(plan) != 3 || len(chunks) != 2 {+ t.Fatalf("MaxChunkSize should split short cue merges: plan=%+v chunks=%+v", plan, chunks)+ }+ if len(chunks[0].Items) != 2 || len(chunks[1].Items) != 1 {+ t.Fatalf("chunk sizes = %+v, want [2,1]", chunks)+ }+}