Skip to content

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.

fix(cli): defer transcription dependencies until required

wulienfd1709db6fa6
diff --git a/cmd/cli/main.go b/cmd/cli/main.go
index 06f9c630..20bef207 100644
--- a/cmd/cli/main.go
+++ b/cmd/cli/main.go
@@ -12,7 +12,6 @@ import (
"krillin-ai/internal/service"
"krillin-ai/log"
"os"
- "strings"
"sync"
)
@@ -130,8 +129,7 @@ func requiresTranscriptionAtStart(cmd cli.Command) bool {
if cmd.Subtitle.CaptionSource == pipeline.CaptionSourceWhisper {
return true
}
- input := strings.ToLower(strings.TrimSpace(cmd.Subtitle.Input))
- return !strings.Contains(input, "youtube.com") && !strings.Contains(input, "youtu.be")
+ return !pipeline.IsYouTubeInput(cmd.Subtitle.Input)
}
func errorResponse(err error, kind pipeline.ErrorKind) pipeline.Response {
diff --git a/cmd/cli/main_test.go b/cmd/cli/main_test.go
index e81e9f88..14296fed 100644
--- a/cmd/cli/main_test.go
+++ b/cmd/cli/main_test.go
@@ -22,6 +22,13 @@ func TestRequiresTranscriptionAtStart(t *testing.T) {
}},
want: false,
},
+ {
+ name: "short youtube URLs can start without ASR",
+ cmd: cli.Command{Name: "subtitle", Subtitle: pipeline.SubtitleRequest{
+ Input: "https://youtu.be/demo", CaptionSource: pipeline.CaptionSourcePlatform,
+ }},
+ want: false,
+ },
{
name: "forced speech recognition requires ASR",
cmd: cli.Command{Name: "subtitle", Subtitle: pipeline.SubtitleRequest{
diff --git a/internal/cli/commands.go b/internal/cli/commands.go
index b8d195cf..14e11a86 100644
--- a/internal/cli/commands.go
+++ b/internal/cli/commands.go
@@ -115,7 +115,7 @@ Flags:
--user-lang <lang> UI language for generated messages
--workdir <dir> Task working directory
--task-id <id> Optional task id
- --caption-source <source> any, manual, auto, or whisper
+ --caption-source <source> any, platform, manual, auto, or whisper
--bilingual-top Put target subtitle on top (default true)
--max-word-one-line <n> Max words per subtitle line
--subtitle-style-file <file> JSON subtitle style override file
diff --git a/internal/deps/checker.go b/internal/deps/checker.go
index 1cce8022..ec594192 100644
--- a/internal/deps/checker.go
+++ b/internal/deps/checker.go
@@ -156,7 +156,11 @@ func configurePackagedTranscriptionDependency() error {
if storage.WhisperKitPath, err = packagedExecutable("whisperkit-cli"); err != nil {
return err
}
- if _, err = resourcepath.RequireDir("models", "whisperkit", config.Conf.Transcribe.Whisperkit.Model); err != nil {
+ if _, err = resourcepath.RequireDir(
+ "models",
+ "whisperkit",
+ fmt.Sprintf("openai_whisper-%s", config.Conf.Transcribe.Whisperkit.Model),
+ ); err != nil {
return err
}
}
diff --git a/internal/deps/checker_test.go b/internal/deps/checker_test.go
index a37ca644..56899e01 100644
--- a/internal/deps/checker_test.go
+++ b/internal/deps/checker_test.go
@@ -2,10 +2,14 @@ package deps
import (
"errors"
+ "krillin-ai/config"
+ "krillin-ai/internal/resourcepath"
+ "krillin-ai/internal/storage"
"krillin-ai/log"
"os"
"path/filepath"
"reflect"
+ "runtime"
"testing"
"time"
@@ -17,6 +21,52 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}
+func TestConfigurePackagedTranscriptionDependencyUsesWhisperKitArchiveDirectory(t *testing.T) {
+ root := t.TempDir()
+ t.Setenv(resourcepath.RootEnv, root)
+
+ executableName := "whisperkit-cli"
+ if runtime.GOOS == "windows" {
+ executableName += ".exe"
+ }
+ executablePath := filepath.Join(root, "bin", executableName)
+ modelPath := filepath.Join(
+ root,
+ "models",
+ "whisperkit",
+ "openai_whisper-large-v2",
+ )
+ if err := os.MkdirAll(filepath.Dir(executablePath), 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(executablePath, []byte("test"), 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.MkdirAll(modelPath, 0755); err != nil {
+ t.Fatal(err)
+ }
+
+ previousTranscribe := config.Conf.Transcribe
+ previousWhisperKitPath := storage.WhisperKitPath
+ t.Cleanup(func() {
+ config.Conf.Transcribe = previousTranscribe
+ storage.WhisperKitPath = previousWhisperKitPath
+ })
+ config.Conf.Transcribe.Provider = "whisperkit"
+ config.Conf.Transcribe.Whisperkit.Model = "large-v2"
+
+ if err := configurePackagedTranscriptionDependency(); err != nil {
+ t.Fatalf("configurePackagedTranscriptionDependency() error = %v", err)
+ }
+ if storage.WhisperKitPath != executablePath {
+ t.Fatalf(
+ "WhisperKitPath = %q, want %q",
+ storage.WhisperKitPath,
+ executablePath,
+ )
+ }
+}
+
func TestResolveYtDlpUpdatesExistingBundledBinaryToStableRelease(t *testing.T) {
env := testYtDlpEnv(t, "darwin")
env.stat = func(path string) (os.FileInfo, error) {
diff --git a/internal/pipeline/subtitle.go b/internal/pipeline/subtitle.go
index 3ba03885..6a78f653 100644
--- a/internal/pipeline/subtitle.go
+++ b/internal/pipeline/subtitle.go
@@ -7,6 +7,7 @@ import (
"krillin-ai/internal/service"
subtitlestyle "krillin-ai/internal/subtitle_style"
"krillin-ai/internal/types"
+ "net/url"
"os"
"strings"
)
@@ -58,7 +59,7 @@ func GenerateSubtitles(ctx context.Context, svc StageService, req SubtitleReques
syncPreparedMediaOutputs(manifest, stepParam)
var platformCaptionErr error
- if isYouTubeInput(req.Input) && req.CaptionSource != CaptionSourceWhisper {
+ if IsYouTubeInput(req.Input) && req.CaptionSource != CaptionSourceWhisper {
reportSubtitleProgress(req, "reading_platform_captions", 20, "正在获取平台字幕")
stepParam.TaskPtr.SetProgressReporter(func(percent uint8) {
phase, message, overall := platformSubtitleProgress(percent)
@@ -200,7 +201,7 @@ func subtitleStepParam(req SubtitleRequest) *types.SubtitleTaskStepParam {
VideoSrc: req.Input,
Status: types.SubtitleTaskStatusProcessing,
}
- vttSwitch := isYouTubeInput(req.Input) && req.CaptionSource != CaptionSourceWhisper
+ vttSwitch := IsYouTubeInput(req.Input) && req.CaptionSource != CaptionSourceWhisper
embedSubtitleVideoType := "none"
if req.PrepareVideo && !vttSwitch {
embedSubtitleVideoType = "all"
@@ -291,7 +292,15 @@ func subtitleResponse(ok bool, req SubtitleRequest, manifest *Manifest, captionS
return resp
}
-func isYouTubeInput(input string) bool {
- normalized := strings.ToLower(strings.TrimSpace(input))
- return strings.Contains(normalized, "youtube.com")
+func IsYouTubeInput(input string) bool {
+ parsed, err := url.Parse(strings.TrimSpace(input))
+ if err != nil {
+ return false
+ }
+ hostname := strings.ToLower(strings.TrimSuffix(parsed.Hostname(), "."))
+ return hostname == "youtu.be" ||
+ hostname == "youtube.com" ||
+ strings.HasSuffix(hostname, ".youtube.com") ||
+ hostname == "youtube-nocookie.com" ||
+ strings.HasSuffix(hostname, ".youtube-nocookie.com")
}
diff --git a/internal/pipeline/subtitle_test.go b/internal/pipeline/subtitle_test.go
index 9cd483b7..dbb82347 100644
--- a/internal/pipeline/subtitle_test.go
+++ b/internal/pipeline/subtitle_test.go
@@ -357,3 +357,22 @@ func TestGenerateSubtitlesFallbackPreparesVideoWhenRequested(t *testing.T) {
t.Fatalf("prepare EmbedSubtitleVideoType values = %v, want [none all]", got)
}
}
+
+func TestIsYouTubeInput(t *testing.T) {
+ tests := []struct {
+ input string
+ want bool
+ }{
+ {input: "https://www.youtube.com/watch?v=demo", want: true},
+ {input: "https://m.youtube.com/watch?v=demo", want: true},
+ {input: "https://youtu.be/demo", want: true},
+ {input: "https://www.youtube-nocookie.com/embed/demo", want: true},
+ {input: "https://youtube.com.example.test/watch?v=demo", want: false},
+ {input: "local:/tmp/video.mp4", want: false},
+ }
+ for _, test := range tests {
+ if got := IsYouTubeInput(test.input); got != test.want {
+ t.Fatalf("IsYouTubeInput(%q) = %v, want %v", test.input, got, test.want)
+ }
+ }
+}
diff --git a/internal/pipeline/types.go b/internal/pipeline/types.go
index 684cd126..e794328b 100644
--- a/internal/pipeline/types.go
+++ b/internal/pipeline/types.go
@@ -19,10 +19,11 @@ const (
type CaptionSource string
const (
- CaptionSourceAny CaptionSource = "any"
- CaptionSourceManual CaptionSource = "manual"
- CaptionSourceAuto CaptionSource = "auto"
- CaptionSourceWhisper CaptionSource = "whisper"
+ CaptionSourceAny CaptionSource = "any"
+ CaptionSourceManual CaptionSource = "manual"
+ CaptionSourceAuto CaptionSource = "auto"
+ CaptionSourcePlatform CaptionSource = "platform"
+ CaptionSourceWhisper CaptionSource = "whisper"
)
type LineMode string
diff --git a/internal/service/split_audio.go b/internal/service/split_audio.go
index 89f15c66..1e8f689d 100644
--- a/internal/service/split_audio.go
+++ b/internal/service/split_audio.go
@@ -135,12 +135,18 @@ func GetSplitPoints(input string, segmentDuration float64) ([]float64, error) {
if err := eg.Wait(); err != nil {
return nil, fmt.Errorf("failed to get quietest time points: %w", err)
}
- // 如果最后一个片段短于最小分割时长,则将其合并到前一个片段
- if audioDuration-timePoints[segmentNum-1] < MIN_DURATION {
+ return finalizeSplitPoints(timePoints, audioDuration), nil
+}
+
+func finalizeSplitPoints(timePoints []float64, audioDuration float64) []float64 {
+ segmentNum := len(timePoints) - 1
+ // 如果最后一个片段短于最小分割时长,则将其合并到前一个片段。
+ // 唯一片段不能合并,否则短音频会变成零个处理片段。
+ if segmentNum > 1 && audioDuration-timePoints[segmentNum-1] < MIN_DURATION {
timePoints = timePoints[:segmentNum]
}
timePoints[len(timePoints)-1] = audioDuration
- return timePoints, nil
+ return timePoints
}
func ClipAudio(input, output string, start, end float64) error {
diff --git a/internal/service/split_audio_test.go b/internal/service/split_audio_test.go
new file mode 100644
index 00000000..857ff851
--- /dev/null
+++ b/internal/service/split_audio_test.go
@@ -0,0 +1,30 @@
+package service
+
+import (
+ "reflect"
+ "testing"
+)
+
+func TestFinalizeSplitPointsKeepsOnlyShortSegment(t *testing.T) {
+ got := finalizeSplitPoints([]float64{0, 0}, 5.6)
+ want := []float64{0, 5.6}
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("finalizeSplitPoints() = %v, want %v", got, want)
+ }
+}
+
+func TestFinalizeSplitPointsMergesShortTrailingSegment(t *testing.T) {
+ got := finalizeSplitPoints([]float64{0, 300, 0}, 305)
+ want := []float64{0, 305}
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("finalizeSplitPoints() = %v, want %v", got, want)
+ }
+}
+
+func TestFinalizeSplitPointsKeepsNormalTrailingSegment(t *testing.T) {
+ got := finalizeSplitPoints([]float64{0, 300, 0}, 330)
+ want := []float64{0, 300, 330}
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("finalizeSplitPoints() = %v, want %v", got, want)
+ }
+}