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
package pipeline
import (
"context"
"errors"
"fmt"
"krillin-ai/internal/service"
subtitlestyle "krillin-ai/internal/subtitle_style"
"krillin-ai/internal/types"
"krillin-ai/pkg/util"
"krillin-ai/pkg/whisper"
"os"
"strings"
)
const defaultSubtitleMaxWordOneLine = 12
type ProgressCounts struct {
Completed int
Total int
}
type SubtitleRequest struct {
Input string
Workdir string
TaskID string
OriginLang string
TargetLang string
UserLang string
CaptionSource CaptionSource
BilingualTop bool
MaxWordOneLine int
SubtitleStyle *subtitlestyle.StyleSet
PrepareVideo bool
SourceOnly bool
InputSRT string
SRTTranslated bool
ReportProgress ProgressReporter
}
func GenerateSubtitles(ctx context.Context, svc StageService, req SubtitleRequest) (Response, error) {
req.ReportProgress = monotonicProgress(req.ReportProgress)
if req.CaptionSource == "" {
req.CaptionSource = CaptionSourceAny
}
manifest, err := subtitleManifest(req)
if err != nil {
return subtitleFailureResponse(req, nil, ErrorKindInternal, "load_manifest_failed", err), err
}
manifest.TaskID = req.TaskID
manifest.Workdir = req.Workdir
manifest.InputURL = req.Input
manifest.OriginLanguage = req.OriginLang
manifest.TargetLanguage = req.TargetLang
manifest.CaptionSource = string(req.CaptionSource)
if err := manifest.ApplyDefaultOutputs(); err != nil {
return subtitleFailureResponse(req, manifest, ErrorKindInternal, "apply_outputs_failed", err), err
}
preparePercent, importPercent := 2, 30
preparePhase, prepareMessage := "preparing_source", "正在准备视频来源"
if req.InputSRT != "" && !req.PrepareVideo {
preparePercent, importPercent = 1, 3
preparePhase, prepareMessage = "importing_subtitles", "正在读取本地字幕"
}
reportSubtitleProgress(req, preparePhase, preparePercent, prepareMessage)
stepParam := subtitleStepParam(req)
stepParam.TaskPtr.SetProgressReporter(func(percent uint8) {
reportSubtitleProgress(req, "preparing_source", 2+minInt(int(percent), 10)*23/10, "正在准备视频来源")
})
forwardTaskProgress(stepParam.TaskPtr, req.ReportProgress, func(percent int) int { return 2 + percent*23/100 })
localInput := strings.TrimPrefix(req.Input, "local:")
if info, err := os.Stat(localInput); err == nil && info.Mode().IsRegular() {
stepParam.InputVideoPath = localInput
}
syncPreparedMediaOutputs(manifest, stepParam)
if req.InputSRT != "" {
if req.PrepareVideo {
stepParam.SkipAudio = true
if err := svc.PrepareMedia(ctx, stepParam); err != nil {
return failSubtitleStage(req, manifest, ErrorKindRetryable, "prepare_media_failed", err)
}
syncPreparedMediaOutputs(manifest, stepParam)
}
reportSubtitleProgress(req, "importing_subtitles", importPercent, "正在读取本地字幕")
if err := importSubtitleFile(ctx, svc, req, stepParam, manifest); err != nil {
return failSubtitleStage(req, manifest, ErrorKindRetryable, "subtitle_import_failed", err)
}
if req.SRTTranslated {
manifest.Outputs.OriginSRT = ""
manifest.Outputs.BilingualSRT = ""
}
reportSubtitleProgress(req, "collecting_outputs", 98, "正在整理字幕产物")
return saveSubtitleSuccess(manifest, req, CaptionSource("local_srt"))
}
var platformCaptionErr error
if IsYouTubeInput(req.Input) && req.CaptionSource != CaptionSourceWhisper {
reportSubtitleProgress(req, "reading_platform_captions", 5, "正在获取平台字幕")
stepParam.TaskPtr.SetProgressReporter(func(percent uint8) {
phase, message, overall := platformSubtitleProgress(percent)
if req.SourceOnly && phase == "translating_subtitles" {
phase, message = "collecting_subtitles", "正在生成原文字幕"
}
reportSubtitleProgress(req, phase, overall, message)
})
stepParam.TaskPtr.SetTranslationProgressReporter(func(completed, total int) {
reportSubtitleProgress(req, "translating_subtitles", 15+completed*70/total, "正在翻译平台字幕", ProgressCounts{completed, total})
})
youtubeReq := subtitleYouTubeReq(req, stepParam.TaskPtr)
vttFile, err := svc.DownloadYouTubeSubtitle(ctx, youtubeReq)
if err == nil {
reportSubtitleProgress(req, "processing_platform_captions", 8, "正在解析平台字幕")
youtubeReq.VttFile = vttFile
_, err = svc.ProcessYouTubeSubtitle(ctx, youtubeReq)
if err != nil && !errors.Is(err, service.ErrYouTubeCaptionInvalid) {
return failSubtitleStage(req, manifest, ErrorKindRetryable, "platform_caption_processing_failed", err)
}
}
if err == nil {
manifest.OriginLanguage = youtubeReq.OriginLanguage
manifest.CaptionSource = "youtube_vtt"
if req.PrepareVideo {
reportSubtitleProgress(req, "preparing_original_media", 88, "正在补齐原始视频")
stepParam.TaskPtr.SetProgressReporter(func(percent uint8) {
overall := 88 + minInt(int(percent), 10)*7/10
reportSubtitleProgress(req, "preparing_original_media", overall, "正在补齐原始视频")
})
forwardTaskProgress(stepParam.TaskPtr, req.ReportProgress, func(percent int) int { return 88 + percent*7/100 })
if err := prepareOriginalMediaForRendering(ctx, svc, stepParam); err != nil {
return failSubtitleStage(req, manifest, ErrorKindRetryable, "prepare_media_for_render_failed", err)
}
syncPreparedMediaOutputs(manifest, stepParam)
}
reportSubtitleProgress(req, "collecting_outputs", 98, "正在整理字幕和视频产物")
return saveSubtitleSuccess(manifest, req, CaptionSource("youtube_vtt"))
}
if errors.Is(err, service.ErrYouTubeCaptionAccess) || ctx.Err() != nil {
return failSubtitleStage(req, manifest, ErrorKindRetryable, "platform_caption_access_failed", err)
}
if req.CaptionSource != CaptionSourceAny {
code := "platform_caption_failed"
if errors.Is(err, service.ErrYouTubeCaptionInvalid) {
code = "platform_caption_invalid"
}
return failSubtitleStage(req, manifest, ErrorKindRetryable, code, err)
}
platformCaptionErr = err
fallbackMessage := "未找到可用的平台字幕,已自动切换到语音转录"
if errors.Is(err, service.ErrYouTubeCaptionInvalid) {
fallbackMessage = "平台字幕解析失败,已自动切换到语音转录"
}
manifest.Warnings = append(manifest.Warnings, fallbackMessage)
reportSubtitleProgress(req, "fallback_to_transcription", 8, fallbackMessage)
stepParam.VttSwitch = false
if req.PrepareVideo {
stepParam.EmbedSubtitleVideoType = "all"
}
reportSubtitleProgress(req, "preparing_audio", 8, "平台字幕不可用,正在准备音频转录")
stepParam.TaskPtr.SetProgressReporter(func(percent uint8) {
reportSubtitleProgress(req, "preparing_source", 2+minInt(int(percent), 10)*23/10, "正在准备视频和音频")
})
if err := svc.PrepareMedia(ctx, stepParam); err != nil {
return failSubtitleStage(req, manifest, ErrorKindRetryable, "prepare_audio_fallback_failed", fmt.Errorf(
"platform caption attempt failed: %v; audio fallback preparation failed: %w",
platformCaptionErr,
err,
))
}
syncPreparedMediaOutputs(manifest, stepParam)
}
if platformCaptionErr == nil {
stepParam.TaskPtr.SetProgressReporter(func(percent uint8) {
reportSubtitleProgress(req, "preparing_source", 2+minInt(int(percent), 10)*23/10, "正在准备视频和音频")
})
if err := svc.PrepareMedia(ctx, stepParam); err != nil {
return failSubtitleStage(req, manifest, ErrorKindRetryable, "prepare_media_failed", err)
}
syncPreparedMediaOutputs(manifest, stepParam)
}
stepParam.TaskPtr.SetProgressReporter(audioSubtitleProgressReporter(req))
forwardTaskProgress(stepParam.TaskPtr, req.ReportProgress, func(percent int) int { return 25 + max(0, percent-15)*65/75 })
reportSubtitleProgress(req, "transcribing_audio", 30, "正在转录并翻译音频字幕")
if err := svc.GenerateSubtitlesFromAudio(ctx, stepParam); err != nil {
if platformCaptionErr != nil {
err = fmt.Errorf(
"platform caption attempt failed: %v; audio transcription fallback failed: %w",
platformCaptionErr,
err,
)
}
code, retryable := "audio_transcription_failed", true
var classified *whisper.TranscriptionError
if errors.As(err, &classified) {
code, retryable = classified.Code, classified.Retryable()
}
kind := ErrorKindRetryable
if !retryable {
kind = ErrorKindUsage
if code == "audio_transcription_invalid_response" {
kind = ErrorKindInternal
}
}
return failSubtitleStage(req, manifest, kind, code, err)
}
manifest.CaptionSource = string(CaptionSourceWhisper)
reportSubtitleProgress(req, "collecting_outputs", 95, "正在整理字幕产物")
return saveSubtitleSuccess(manifest, req, CaptionSourceWhisper)
}
func reportSubtitleProgress(req SubtitleRequest, phase string, percent int, message string, counts ...ProgressCounts) {
if req.ReportProgress == nil {
return
}
req.ReportProgress(phase, percent, message, counts...)
}
func platformSubtitleProgress(percent uint8) (string, string, int) {
value := int(percent)
if value < 40 {
return "processing_platform_captions", "正在解析平台字幕", 8 + value*7/40
}
if value < 90 {
return "translating_subtitles", "正在翻译字幕", 15 + (value-40)*70/50
}
return "collecting_subtitles", "正在生成双语字幕", 87
}
func audioSubtitleProgressReporter(req SubtitleRequest) func(uint8) {
return func(percent uint8) {
value := int(percent)
overall := 25 + max(0, value-15)*65/75
phase := "transcribing_audio"
message := "正在转录并翻译音频字幕"
if req.SourceOnly {
message = "正在转录音频字幕"
if value <= 10 {
phase = "preparing_audio"
message = "正在准备音频转录"
} else if value >= 90 {
phase = "collecting_subtitles"
message = "正在生成原文字幕"
}
reportSubtitleProgress(req, phase, overall, message)
return
}
if value <= 10 {
phase = "preparing_audio"
message = "正在准备音频转录"
} else if value >= 53 && value < 90 {
phase = "translating_subtitles"
message = "正在翻译字幕"
} else if value >= 90 {
phase = "collecting_subtitles"
message = "正在生成字幕文件"
}
reportSubtitleProgress(req, phase, overall, message)
}
}
func minInt(left, right int) int {
if left < right {
return left
}
return right
}
func syncPreparedMediaOutputs(manifest *Manifest, stepParam *types.SubtitleTaskStepParam) {
if stepParam.InputVideoPath != "" {
manifest.Outputs.OriginVideo = stepParam.InputVideoPath
}
if stepParam.AudioFilePath != "" {
manifest.Outputs.OriginAudio = stepParam.AudioFilePath
}
}
func subtitleManifest(req SubtitleRequest) (*Manifest, error) {
manifest, err := LoadManifest(req.Workdir)
if err == nil {
return manifest, nil
}
if errors.Is(err, os.ErrNotExist) {
return NewManifest(req.TaskID, req.Workdir), nil
}
return nil, err
}
func subtitleStepParam(req SubtitleRequest) *types.SubtitleTaskStepParam {
userLang := req.UserLang
if userLang == "" {
userLang = string(types.LanguageNameSimplifiedChinese)
}
resultType := types.SubtitleResultTypeBilingualTranslationOnBottom
if req.SourceOnly {
resultType = types.SubtitleResultTypeOriginOnly
} else if req.BilingualTop {
resultType = types.SubtitleResultTypeBilingualTranslationOnTop
}
maxWordOneLine := req.MaxWordOneLine
if maxWordOneLine <= 0 {
maxWordOneLine = defaultSubtitleMaxWordOneLine
}
taskPtr := &types.SubtitleTask{
TaskId: req.TaskID,
VideoSrc: req.Input,
Status: types.SubtitleTaskStatusProcessing,
}
vttSwitch := req.InputSRT == "" && IsYouTubeInput(req.Input) && req.CaptionSource != CaptionSourceWhisper
embedSubtitleVideoType := "none"
if req.PrepareVideo && !vttSwitch {
embedSubtitleVideoType = "all"
}
return &types.SubtitleTaskStepParam{
TaskId: req.TaskID,
TaskPtr: taskPtr,
TaskBasePath: req.Workdir,
Link: req.Input,
SubtitleResultType: resultType,
OriginLanguage: types.StandardLanguageCode(req.OriginLang),
TargetLanguage: types.StandardLanguageCode(req.TargetLang),
UserUILanguage: types.StandardLanguageCode(userLang),
MaxWordOneLine: maxWordOneLine,
VttSwitch: vttSwitch,
EmbedSubtitleVideoType: embedSubtitleVideoType,
SubtitleStyle: req.SubtitleStyle,
}
}
func prepareOriginalMediaForRendering(ctx context.Context, svc StageService, stepParam *types.SubtitleTaskStepParam) error {
stepParam.VttSwitch = false
stepParam.EmbedSubtitleVideoType = "all"
stepParam.SkipAudio = true
return svc.PrepareMedia(ctx, stepParam)
}
func subtitleYouTubeReq(req SubtitleRequest, taskPtr *types.SubtitleTask) *service.YoutubeSubtitleReq {
return &service.YoutubeSubtitleReq{
TaskBasePath: req.Workdir,
TaskId: req.TaskID,
URL: req.Input,
OriginLanguage: req.OriginLang,
TargetLanguage: req.TargetLang,
TaskPtr: taskPtr,
SourceOnly: req.SourceOnly,
TargetLanguageFirst: req.BilingualTop,
}
}
func saveSubtitleSuccess(manifest *Manifest, req SubtitleRequest, captionSource CaptionSource) (Response, error) {
videoInfo, videoErr := os.Stat(manifest.Outputs.OriginVideo)
if videoErr != nil || !videoInfo.Mode().IsRegular() || videoInfo.Size() == 0 {
manifest.Outputs.OriginVideo = ""
if req.PrepareVideo {
return failSubtitleStage(req, manifest, ErrorKindRetryable, "source_video_missing", errors.New("original video was requested but was not produced"))
}
}
if req.SourceOnly {
manifest.Outputs.TargetSRT = ""
manifest.Outputs.BilingualSRT = ""
manifest.Outputs.ShortOriginSRT = ""
manifest.Outputs.ShortOriginMixedSRT = ""
manifest.Outputs.TargetText = ""
}
if err := validateExistingSubtitleOutputs(manifest.Outputs); err != nil {
manifest.MarkStage(StageSubtitle, false, err.Error())
_ = manifest.Save()
return subtitleFailureResponse(req, manifest, ErrorKindInternal, "invalid_subtitle_timeline", err), err
}
manifest.MarkStage(StageSubtitle, true, "")
if err := manifest.Save(); err != nil {
return subtitleFailureResponse(req, manifest, ErrorKindInternal, "save_manifest_failed", err), err
}
return subtitleResponse(true, req, manifest, captionSource, nil), nil
}
func failSubtitleStage(req SubtitleRequest, manifest *Manifest, kind ErrorKind, code string, err error) (Response, error) {
if manifest != nil {
manifest.MarkStage(StageSubtitle, false, err.Error())
_ = manifest.Save()
}
return subtitleFailureResponse(req, manifest, kind, code, err), err
}
func subtitleFailureResponse(req SubtitleRequest, manifest *Manifest, kind ErrorKind, code string, err error) Response {
pipelineErr := &Error{
Kind: kind,
Code: code,
Message: err.Error(),
Retryable: kind == ErrorKindRetryable,
}
return subtitleResponse(false, req, manifest, req.CaptionSource, pipelineErr)
}
func subtitleResponse(ok bool, req SubtitleRequest, manifest *Manifest, captionSource CaptionSource, pipelineErr *Error) Response {
resp := Response{
OK: ok,
Stage: StageSubtitle,
Workdir: req.Workdir,
TaskID: req.TaskID,
CaptionSource: captionSource,
Error: pipelineErr,
}
if manifest != nil {
resp.Workdir = manifest.Workdir
resp.TaskID = manifest.TaskID
resp.Outputs = manifest.Outputs
resp.Warnings = manifest.Warnings
if manifest.CaptionSource != "" {
resp.CaptionSource = CaptionSource(manifest.CaptionSource)
}
}
return resp
}
func IsYouTubeInput(input string) bool {
return util.IsYouTubeURL(input)
}