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: Asian language subtitle overflow video
沈浩b09497fc7ff6
diff --git a/internal/service/audio2subtitle.go b/internal/service/audio2subtitle.goindex 999366a6..58694f1c 100644--- a/internal/service/audio2subtitle.go+++ b/internal/service/audio2subtitle.go@@ -159,7 +159,7 @@ func (s Service) audioToSrt(ctx context.Context, stepParam *types.SubtitleTaskStstorage.SubtitleTasks[stepParam.TaskId].ProcessPct = processPct// 生成时间戳- err = s.generateTimestamps(stepParam.TaskId, stepParam.TaskBasePath, stepParam.OriginLanguage, stepParam.SubtitleResultType, audioFile, stepParam.OriginLanguageWordOneLine)+ err = s.generateTimestamps(stepParam.TaskId, stepParam.TaskBasePath, stepParam.OriginLanguage, stepParam.SubtitleResultType, audioFile, stepParam.MaxWordOneLine)if err != nil {cancel()log.GetLogger().Error("audioToSubtitle.audioToSrt.generateTimestamps err", zap.Any("stepParam", stepParam), zap.String("audio file", audioFileItem.AudioFile), zap.Error(err))diff --git a/internal/service/srt_embed.go b/internal/service/srt_embed.goindex dd164fcb..606e64f8 100644--- a/internal/service/srt_embed.go+++ b/internal/service/srt_embed.go@@ -32,14 +32,13 @@ func (s Service) embedSubtitles(ctx context.Context, stepParam *types.SubtitleTareturn nil}log.GetLogger().Info("合成字幕嵌入视频:横屏")- err = embedSubtitles(stepParam.InputVideoPath, stepParam.BilingualSrtFilePath, stepParam.TaskBasePath, true)+ err = embedSubtitles(stepParam, true)if err != nil {log.GetLogger().Error("generateAudioSubtitles embedSubtitles err", zap.Any("step param", stepParam), zap.Error(err))return err}}if stepParam.EmbedSubtitleVideoType == "vertical" || stepParam.EmbedSubtitleVideoType == "all" {- verticalVideoPath := stepParam.InputVideoPathif width > height {// 生成竖屏视频transferredVerticalVideoPath := filepath.Join(stepParam.TaskBasePath, types.SubtitleTaskTransferredVerticalVideoFileName)@@ -48,10 +47,10 @@ func (s Service) embedSubtitles(ctx context.Context, stepParam *types.SubtitleTalog.GetLogger().Error("生成竖屏视频失败", zap.Any("step param", stepParam), zap.Error(err))return err}- verticalVideoPath = transferredVerticalVideoPath+ stepParam.InputVideoPath = transferredVerticalVideoPath}log.GetLogger().Info("合成字幕嵌入视频:竖屏")- err = embedSubtitles(verticalVideoPath, stepParam.ShortOriginMixedSrtFilePath, stepParam.TaskBasePath, false)+ err = embedSubtitles(stepParam, false)if err != nil {log.GetLogger().Error("generateAudioSubtitles embedSubtitles err", zap.Any("step param", stepParam), zap.Error(err))return err@@ -64,11 +63,11 @@ func (s Service) embedSubtitles(ctx context.Context, stepParam *types.SubtitleTareturn nil}-func splitMajorTextInHorizontal(text string) []string {+func splitMajorTextInHorizontal(text string, language types.StandardLanguageName, maxWordOneLine int) []string {// 按语言情况分割var segments []string- containsAlphabetic := util.ContainsAlphabetic(text)- if !containsAlphabetic {+ if language == types.LanguageNameSimplifiedChinese || language == types.LanguageNameTraditionalChinese ||+ language == types.LanguageNameJapanese || language == types.LanguageNameKorean || language == types.LanguageNameThai {segments = regexp.MustCompile(`.`).FindAllString(text, -1)} else {segments = strings.Split(text, " ")@@ -77,7 +76,7 @@ func splitMajorTextInHorizontal(text string) []string {totalWidth := len(segments)// 直接返回原句子- if (containsAlphabetic && totalWidth <= 10) || (!containsAlphabetic && totalWidth <= 20) {+ if totalWidth <= maxWordOneLine {return []string{text}}@@ -162,7 +161,7 @@ func formatTimestamp(t time.Duration) string {return fmt.Sprintf("%02d:%02d:%02d.%02d", hours, minutes, seconds, milliseconds)}-func srtToAss(inputSRT, outputASS string, isHorizontal bool) error {+func srtToAss(inputSRT, outputASS string, isHorizontal bool, stepParam *types.SubtitleTaskStepParam) error {file, err := os.Open(inputSRT)if err != nil {return err@@ -216,7 +215,14 @@ func srtToAss(inputSRT, outputASS string, isHorizontal bool) error {if len(subtitleLines) < 2 {continue}- majorLine := strings.Join(splitMajorTextInHorizontal(subtitleLines[0]), " \\N")+ var majorTextLanguage types.StandardLanguageName+ if stepParam.SubtitleResultType == types.SubtitleResultTypeBilingualTranslationOnTop { // 一定是bilingual+ majorTextLanguage = stepParam.TargetLanguage+ } else {+ majorTextLanguage = stepParam.OriginLanguage+ }++ majorLine := strings.Join(splitMajorTextInHorizontal(subtitleLines[0], majorTextLanguage, stepParam.MaxWordOneLine), " \\N")minorLine := util.CleanPunction(subtitleLines[1])// ASS条目@@ -226,6 +232,7 @@ func srtToAss(inputSRT, outputASS string, isHorizontal bool) error {_, _ = assFile.WriteString(fmt.Sprintf("Dialogue: 0,%s,%s,Major,,0,0,0,,%s\n", startFormatted, endFormatted, combinedText))}} else {+ // TODO 竖屏拆分调优_, _ = assFile.WriteString(types.AssHeaderVertical)for scanner.Scan() {line := scanner.Text()@@ -289,21 +296,21 @@ func srtToAss(inputSRT, outputASS string, isHorizontal bool) error {return nil}-func embedSubtitles(videoFilePath, srtFilePath, workDir string, isHorizontal bool) error {+func embedSubtitles(stepParam *types.SubtitleTaskStepParam, isHorizontal bool) error {outputFileName := types.SubtitleTaskVerticalEmbedVideoFileName- assPath := filepath.Join(workDir, "formatted_subtitles.ass")-if isHorizontal {outputFileName = types.SubtitleTaskHorizontalEmbedVideoFileName}- if err := srtToAss(srtFilePath, assPath, isHorizontal); err != nil {+ assPath := filepath.Join(stepParam.TaskBasePath, "formatted_subtitles.ass")++ if err := srtToAss(stepParam.BilingualSrtFilePath, assPath, isHorizontal, stepParam); err != nil {return err}- cmd := exec.Command(storage.FfmpegPath, "-y", "-i", videoFilePath, "-vf", fmt.Sprintf("ass=%s", strings.ReplaceAll(assPath, "\\", "/")), "-c:a", "copy", filepath.Join(workDir, fmt.Sprintf("/output/%s", outputFileName)))+ cmd := exec.Command(storage.FfmpegPath, "-y", "-i", stepParam.InputVideoPath, "-vf", fmt.Sprintf("ass=%s", strings.ReplaceAll(assPath, "\\", "/")), "-c:a", "aac", "-b:a", "192k", filepath.Join(stepParam.TaskBasePath, fmt.Sprintf("/output/%s", outputFileName)))output, err := cmd.CombinedOutput()if err != nil {- log.GetLogger().Error("embedSubtitles ffmpeg err", zap.Any("step param", videoFilePath), zap.String("output", string(output)), zap.Error(err))+ log.GetLogger().Error("embedSubtitles ffmpeg err", zap.Any("video path", stepParam.InputVideoPath), zap.String("output", string(output)), zap.Error(err))return err}return nildiff --git a/internal/service/subtitle_service.go b/internal/service/subtitle_service.goindex 838358e6..35325f55 100644--- a/internal/service/subtitle_service.go+++ b/internal/service/subtitle_service.go@@ -101,25 +101,25 @@ func (s Service) StartSubtitleTask(req dto.StartVideoSubtitleTaskReq) (*dto.Star}stepParam := types.SubtitleTaskStepParam{- TaskId: taskId,- TaskBasePath: taskBasePath,- Link: req.Url,- SubtitleResultType: resultType,- EnableModalFilter: req.ModalFilter == types.SubtitleTaskModalFilterYes,- EnableTts: req.Tts == types.SubtitleTaskTtsYes,- TtsVoiceCode: ttsVoiceCode,- VoiceCloneAudioUrl: voiceCloneAudioUrl,- ReplaceWordsMap: replaceWordsMap,- OriginLanguage: types.StandardLanguageName(req.OriginLanguage),- TargetLanguage: types.StandardLanguageName(req.TargetLang),- UserUILanguage: types.StandardLanguageName(req.Language),- EmbedSubtitleVideoType: req.EmbedSubtitleVideoType,- VerticalVideoMajorTitle: req.VerticalMajorTitle,- VerticalVideoMinorTitle: req.VerticalMinorTitle,- OriginLanguageWordOneLine: 12, // 默认值+ TaskId: taskId,+ TaskBasePath: taskBasePath,+ Link: req.Url,+ SubtitleResultType: resultType,+ EnableModalFilter: req.ModalFilter == types.SubtitleTaskModalFilterYes,+ EnableTts: req.Tts == types.SubtitleTaskTtsYes,+ TtsVoiceCode: ttsVoiceCode,+ VoiceCloneAudioUrl: voiceCloneAudioUrl,+ ReplaceWordsMap: replaceWordsMap,+ OriginLanguage: types.StandardLanguageName(req.OriginLanguage),+ TargetLanguage: types.StandardLanguageName(req.TargetLang),+ UserUILanguage: types.StandardLanguageName(req.Language),+ EmbedSubtitleVideoType: req.EmbedSubtitleVideoType,+ VerticalVideoMajorTitle: req.VerticalMajorTitle,+ VerticalVideoMinorTitle: req.VerticalMinorTitle,+ MaxWordOneLine: 12, // 默认值}if req.OriginLanguageWordOneLine != 0 {- stepParam.OriginLanguageWordOneLine = req.OriginLanguageWordOneLine+ stepParam.MaxWordOneLine = req.OriginLanguageWordOneLine}go func() {defer func() {diff --git a/internal/types/subtitle_task.go b/internal/types/subtitle_task.goindex e45ecaa5..7fff5e00 100644--- a/internal/types/subtitle_task.go+++ b/internal/types/subtitle_task.go@@ -231,7 +231,7 @@ type SubtitleTaskStepParam struct {EmbedSubtitleVideoType string // 合成字幕嵌入的视频类型 none不嵌入 horizontal横屏 vertical竖屏VerticalVideoMajorTitle string // 合成竖屏视频的主标题VerticalVideoMinorTitle string- OriginLanguageWordOneLine int // 源语言字幕一行显示多少个字+ MaxWordOneLine int // 字幕一行最多显示多少个字}type SrtSentence struct {