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.

feat: add subtitle embed

沈浩90023c7b960f
diff --git a/internal/dto/subtitle_task.go b/internal/dto/subtitle_task.go
index abf5ee87..ef025f77 100644
--- a/internal/dto/subtitle_task.go
+++ b/internal/dto/subtitle_task.go
@@ -13,6 +13,9 @@ type StartVideoSubtitleTaskReq struct {
TtsVoiceCloneSrcFileUrl string `json:"tts_voice_clone_src_file_url"`
Replace []string `json:"replace"`
Language string `json:"language"`
+ EmbedSubtitleVideoType string `json:"embed_subtitle_video_type"`
+ VerticalMajorTitle string `json:"vertical_major_title"`
+ VerticalMinorTitle string `json:"vertical_minor_title"`
}
type StartVideoSubtitleTaskResData struct {
diff --git a/internal/service/front.go b/internal/service/front.go
deleted file mode 100644
index 63995484..00000000
--- a/internal/service/front.go
+++ /dev/null
@@ -1,29 +0,0 @@
-package service
-
-import (
- "fmt"
- "net/http"
- "net/url"
-)
-
-// ...existing code...
-
-func RenderPage(w http.ResponseWriter, r *http.Request) {
- if r.Method == http.MethodPost {
- link := r.FormValue("link")
- // 处理链接的后台HTTP请求
- resp, err := http.PostForm("http://127.0.0.1:8888/capability/subtitleTask", url.Values{"link": {link}})
- if err != nil {
- http.Error(w, "请求失败", http.StatusInternalServerError)
- return
- }
- defer resp.Body.Close()
- // 处理响应...
- fmt.Fprintf(w, "请求成功,状态码:%d", resp.StatusCode)
- return
- }
-
- http.Error(w, "仅支持POST请求", http.StatusMethodNotAllowed)
-}
-
-// ...existing code...
diff --git a/internal/service/srt_embed.go b/internal/service/srt_embed.go
new file mode 100644
index 00000000..0c1127d9
--- /dev/null
+++ b/internal/service/srt_embed.go
@@ -0,0 +1,345 @@
+package service
+
+import (
+ "bufio"
+ "bytes"
+ "fmt"
+ "go.uber.org/zap"
+ "krillin-ai/internal/storage"
+ "krillin-ai/internal/types"
+ "krillin-ai/log"
+ "krillin-ai/pkg/util"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "regexp"
+ "runtime"
+ "strconv"
+ "strings"
+ "time"
+)
+
+func splitMajorTextInHorizontal(text string) []string {
+ // 按语言情况分割
+ var segments []string
+ containsAlphabetic := util.ContainsAlphabetic(text)
+ if !containsAlphabetic {
+ segments = regexp.MustCompile(`.`).FindAllString(text, -1)
+ } else {
+ segments = strings.Split(text, " ")
+ }
+
+ totalWidth := len(segments)
+
+ // 直接返回原句子
+ if (containsAlphabetic && totalWidth <= 10) || (!containsAlphabetic && totalWidth <= 20) {
+ return []string{text}
+ }
+
+ // 确定拆分点,按2/5和3/5的比例拆分
+ line1MaxWidth := int(float64(totalWidth) * 2 / 5)
+ currentWidth := 0
+ splitIndex := 0
+
+ for i, _ := range segments {
+ currentWidth++
+
+ // 当达到 2/5 宽度时,设置拆分点
+ if currentWidth >= line1MaxWidth {
+ splitIndex = i + 1
+ break
+ }
+ }
+
+ // 分割文本,保留原有句子格式
+ line1 := strings.Join(segments[:splitIndex], "")
+ line2 := strings.Join(segments[splitIndex:], "")
+ line1 = util.CleanPunction(line1)
+ line2 = util.CleanPunction(line2)
+
+ return []string{line1, line2}
+}
+
+func splitChineseText(text string, maxWordLine int) []string {
+ var lines []string
+ words := []rune(text)
+ for i := 0; i < len(words); i += maxWordLine {
+ end := i + maxWordLine
+ if end > len(words) {
+ end = len(words)
+ }
+ lines = append(lines, string(words[i:end]))
+ }
+ return lines
+}
+
+func parseSRTTime(timeStr string) (time.Duration, error) {
+ timeStr = strings.Replace(timeStr, ",", ".", 1)
+ parts := strings.Split(timeStr, ":")
+ if len(parts) != 3 {
+ return 0, fmt.Errorf("invalid time format: %s", timeStr)
+ }
+
+ hours, err := strconv.Atoi(parts[0])
+ if err != nil {
+ return 0, err
+ }
+ minutes, err := strconv.Atoi(parts[1])
+ if err != nil {
+ return 0, err
+ }
+ secondsAndMilliseconds := strings.Split(parts[2], ".")
+ if len(secondsAndMilliseconds) != 2 {
+ return 0, fmt.Errorf("invalid time format: %s", timeStr)
+ }
+ seconds, err := strconv.Atoi(secondsAndMilliseconds[0])
+ if err != nil {
+ return 0, err
+ }
+ milliseconds, err := strconv.Atoi(secondsAndMilliseconds[1])
+ if err != nil {
+ return 0, err
+ }
+
+ duration := time.Duration(hours)*time.Hour +
+ time.Duration(minutes)*time.Minute +
+ time.Duration(seconds)*time.Second +
+ time.Duration(milliseconds)*time.Millisecond
+
+ return duration, nil
+}
+
+func formatTimestamp(t time.Duration) string {
+ hours := int(t.Hours())
+ minutes := int(t.Minutes()) % 60
+ seconds := int(t.Seconds()) % 60
+ milliseconds := int(t.Milliseconds()) % 1000 / 10
+ return fmt.Sprintf("%02d:%02d:%02d.%02d", hours, minutes, seconds, milliseconds)
+}
+
+func srtToAss(inputSRT, outputASS string, isHorizontal bool) error {
+ file, err := os.Open(inputSRT)
+ if err != nil {
+ return err
+ }
+ defer file.Close()
+
+ assFile, err := os.Create(outputASS)
+ if err != nil {
+ return err
+ }
+ defer assFile.Close()
+ scanner := bufio.NewScanner(file)
+
+ if isHorizontal {
+ _, _ = assFile.WriteString(types.AssHeaderHorizontal)
+ for scanner.Scan() {
+ line := scanner.Text()
+ if line == "" {
+ continue
+ }
+ // 读取时间戳行
+ if !scanner.Scan() {
+ break
+ }
+ timestampLine := scanner.Text()
+ parts := strings.Split(timestampLine, " --> ")
+ if len(parts) != 2 {
+ continue // 无效时间戳格式
+ }
+
+ startTimeStr := strings.TrimSpace(parts[0])
+ endTimeStr := strings.TrimSpace(parts[1])
+ startTime, err := parseSRTTime(startTimeStr)
+ if err != nil {
+ return err
+ }
+ endTime, err := parseSRTTime(endTimeStr)
+ if err != nil {
+ return err
+ }
+
+ var subtitleLines []string
+ for scanner.Scan() {
+ textLine := scanner.Text()
+ if textLine == "" {
+ break // 字幕块结束
+ }
+ subtitleLines = append(subtitleLines, textLine)
+ }
+
+ if len(subtitleLines) < 2 {
+ continue
+ }
+ majorLine := strings.Join(splitMajorTextInHorizontal(subtitleLines[0]), " \\N")
+ minorLine := util.CleanPunction(subtitleLines[1])
+
+ // ASS条目
+ startFormatted := formatTimestamp(startTime)
+ endFormatted := formatTimestamp(endTime)
+ combinedText := fmt.Sprintf("{\\an2}{\\rMajor}%s\\N{\\rMinor}%s", majorLine, minorLine)
+ _, _ = assFile.WriteString(fmt.Sprintf("Dialogue: 0,%s,%s,Major,,0,0,0,,%s\n", startFormatted, endFormatted, combinedText))
+ }
+ } else {
+ _, _ = assFile.WriteString(types.AssHeaderVertical)
+ for scanner.Scan() {
+ line := scanner.Text()
+ if line == "" {
+ continue
+ }
+ if !scanner.Scan() {
+ break
+ }
+ timestampLine := scanner.Text()
+ parts := strings.Split(timestampLine, " --> ")
+ if len(parts) != 2 {
+ continue // 无效时间戳格式
+ }
+
+ startTimeStr := strings.TrimSpace(parts[0])
+ endTimeStr := strings.TrimSpace(parts[1])
+ startTime, err := parseSRTTime(startTimeStr)
+ if err != nil {
+ return err
+ }
+ endTime, err := parseSRTTime(endTimeStr)
+ if err != nil {
+ return err
+ }
+
+ var content string
+ scanner.Scan()
+ content = scanner.Text()
+ if content == "" {
+ continue
+ }
+ totalTime := endTime - startTime
+
+ if !util.ContainsAlphabetic(content) {
+ // 处理中文字幕
+ chineseLines := splitChineseText(content, 10)
+ for i, line := range chineseLines {
+ iStart := startTime + time.Duration(float64(i)*float64(totalTime)/float64(len(chineseLines)))
+ iEnd := startTime + time.Duration(float64(i+1)*float64(totalTime)/float64(len(chineseLines)))
+ if iEnd > endTime {
+ iEnd = endTime
+ }
+
+ startFormatted := formatTimestamp(iStart)
+ endFormatted := formatTimestamp(iEnd)
+ cleanedText := util.CleanPunction(line)
+ combinedText := fmt.Sprintf("{\\an2}{\\rMajor}%s", cleanedText)
+ _, _ = assFile.WriteString(fmt.Sprintf("Dialogue: 0,%s,%s,Major,,0,0,0,,%s\n", startFormatted, endFormatted, combinedText))
+ }
+ } else {
+ // 处理英文字幕
+ startFormatted := formatTimestamp(startTime)
+ endFormatted := formatTimestamp(endTime)
+ cleanedText := util.CleanPunction(content)
+ combinedText := fmt.Sprintf("{\\an2}{\\rMinor}%s", cleanedText)
+ _, _ = assFile.WriteString(fmt.Sprintf("Dialogue: 0,%s,%s,Minor,,0,0,0,,%s\n", startFormatted, endFormatted, combinedText))
+ }
+ }
+ }
+ return nil
+}
+
+func embedSubtitles(videoFilePath, srtFilePath, workDir string, 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 {
+ 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)))
+ 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))
+ return err
+ }
+ return nil
+}
+
+func getFontPaths() (string, string, error) {
+ switch runtime.GOOS {
+ case "windows":
+ return "C\\:/Windows/Fonts/msyhbd.ttc", "C\\:/Windows/Fonts/msyh.ttc", nil // 在ffmpeg参数里必须这样写
+ case "darwin":
+ return "/System/Library/Fonts/Supplemental/Arial Bold.ttf", "/System/Library/Fonts/Supplemental/Arial.ttf", nil
+ case "linux":
+ return "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", nil
+ default:
+ return "", "", fmt.Errorf("unsupported OS: %s", runtime.GOOS)
+ }
+}
+
+func getResolution(inputVideo string) (int, int, error) {
+ // 获取视频信息
+ cmdArgs := []string{
+ "-v", "error",
+ "-select_streams", "v:0",
+ "-show_entries", "stream=width,height",
+ "-of", "csv=s=x:p=0",
+ inputVideo,
+ }
+ cmd := exec.Command(storage.FfprobePath, cmdArgs...)
+ var out bytes.Buffer
+ cmd.Stdout = &out
+ cmd.Stderr = &out
+
+ if err := cmd.Run(); err != nil {
+ log.GetLogger().Error("获取视频分辨率失败", zap.String("output", out.String()), zap.Error(err))
+ return 0, 0, err
+ }
+
+ output := strings.TrimSpace(out.String())
+ dimensions := strings.Split(output, "x")
+ if len(dimensions) != 2 {
+ log.GetLogger().Error("获取视频分辨率失败", zap.String("output", output))
+ return 0, 0, fmt.Errorf("invalid resolution format: %s", output)
+ }
+ width, _ := strconv.Atoi(dimensions[0])
+ height, _ := strconv.Atoi(dimensions[1])
+ return width, height, nil
+}
+
+func convertToVertical(inputVideo, outputVideo, majorTitle, minorTitle string) error {
+ if _, err := os.Stat(outputVideo); err == nil {
+ log.GetLogger().Info("竖屏视频已存在", zap.String("outputVideo", outputVideo))
+ return nil
+ }
+
+ fontBold, fontRegular, err := getFontPaths()
+ if err != nil {
+ log.GetLogger().Error("获取字体路径失败", zap.Error(err))
+ return err
+ }
+
+ cmdArgs := []string{
+ "-i", inputVideo,
+ "-vf", fmt.Sprintf("scale=720:1280:force_original_aspect_ratio=decrease,pad=720:1280:(ow-iw)/2:(oh-ih)*2/5,drawbox=y=0:h=100:c=black@1:t=fill,drawtext=text='%s':x=(w-text_w)/2:y=210:fontsize=55:fontcolor=yellow:box=1:[email protected]:fontfile='%s',drawtext=text='%s':x=(w-text_w)/2:y=280:fontsize=40:fontcolor=yellow:box=1:[email protected]:fontfile='%s'",
+ majorTitle, fontBold, minorTitle, fontRegular),
+ "-r", "30",
+ "-b:v", "7587k",
+ "-c:a", "aac",
+ "-b:a", "192k",
+ "-c:v", "libx264",
+ "-preset", "fast",
+ "-y",
+ outputVideo,
+ }
+ cmd := exec.Command(storage.FfmpegPath, cmdArgs...)
+ var output []byte
+ output, err = cmd.CombinedOutput()
+ if err != nil {
+ log.GetLogger().Error("视频转竖屏失败", zap.String("output", string(output)), zap.Error(err))
+ return err
+ }
+
+ fmt.Printf("竖屏视频已保存到: %s\n", outputVideo)
+ return nil
+}
diff --git a/internal/service/subtitle_service.go b/internal/service/subtitle_service.go
index 6642f732..8564c961 100644
--- a/internal/service/subtitle_service.go
+++ b/internal/service/subtitle_service.go
@@ -77,7 +77,7 @@ func (s Service) StartSubtitleTask(req dto.StartVideoSubtitleTaskReq) (*dto.Star
taskBasePath := filepath.Join("./tasks", taskId)
if _, err = os.Stat(taskBasePath); os.IsNotExist(err) {
// 不存在则创建
- err = os.MkdirAll(taskBasePath, os.ModePerm)
+ err = os.MkdirAll(filepath.Join(taskBasePath, "output"), os.ModePerm)
if err != nil {
log.GetLogger().Error("StartVideoSubtitleTask MkdirAll err", zap.Any("req", req), zap.Error(err))
}
@@ -111,18 +111,21 @@ 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),
+ 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,
}
go func() {
defer func() {
@@ -163,6 +166,13 @@ func (s Service) StartSubtitleTask(req dto.StartVideoSubtitleTaskReq) (*dto.Star
storage.SubtitleTasks[stepParam.TaskId].FailReason = "srt file to speech error"
return
}
+ err = s.embedSubtitles(ctx, &stepParam)
+ if err != nil {
+ log.GetLogger().Error("StartVideoSubtitleTask embedSubtitles err", zap.Any("req", req), zap.Error(err))
+ storage.SubtitleTasks[stepParam.TaskId].Status = types.SubtitleTaskStatusFailed
+ storage.SubtitleTasks[stepParam.TaskId].FailReason = "embed subtitles error"
+ return
+ }
err = s.uploadSubtitles(ctx, &stepParam)
if err != nil {
log.GetLogger().Error("StartVideoSubtitleTask uploadSubtitles err", zap.Any("req", req), zap.Error(err))
@@ -219,6 +229,7 @@ func (s Service) linkToAudioFile(ctx context.Context, stepParam *types.SubtitleT
if strings.Contains(link, "local:") {
// 本地文件
videoPath := strings.ReplaceAll(link, "local:", "")
+ stepParam.InputVideoPath = videoPath
cmd := exec.Command(storage.FfmpegPath, "-i", videoPath, "-vn", "-ar", "44100", "-ac", "2", "-ab", "192k", "-f", "mp3", audioPath)
output, err = cmd.CombinedOutput()
if err != nil {
@@ -361,6 +372,50 @@ func (s Service) audioToSubtitle(ctx context.Context, stepParam *types.SubtitleT
return nil
}
+func (s Service) embedSubtitles(ctx context.Context, stepParam *types.SubtitleTaskStepParam) error {
+ var err error
+ if stepParam.EmbedSubtitleVideoType != "none" {
+ var width, height int
+ width, height, err = getResolution(stepParam.InputVideoPath)
+ // 横屏可以合成竖屏的,但竖屏暂时不支持合成横屏的
+ if stepParam.EmbedSubtitleVideoType == "horizontal" || stepParam.EmbedSubtitleVideoType == "all" {
+ if width < height {
+ log.GetLogger().Info("检测到输入视频是竖屏,无法合成横屏视频,跳过")
+ return nil
+ }
+ log.GetLogger().Info("合成字幕嵌入视频:横屏")
+ err = embedSubtitles(stepParam.InputVideoPath, stepParam.BilingualSrtFilePath, stepParam.TaskBasePath, 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.InputVideoPath
+ if width > height {
+ // 生成竖屏视频
+ transferredVerticalVideoPath := filepath.Join(stepParam.TaskBasePath, types.SubtitleTaskTransferredVerticalVideoFileName)
+ err = convertToVertical(stepParam.InputVideoPath, transferredVerticalVideoPath, stepParam.VerticalVideoMajorTitle, stepParam.VerticalVideoMinorTitle)
+ if err != nil {
+ log.GetLogger().Error("生成竖屏视频失败", zap.Any("step param", stepParam), zap.Error(err))
+ return err
+ }
+ verticalVideoPath = transferredVerticalVideoPath
+ }
+ log.GetLogger().Info("合成字幕嵌入视频:竖屏")
+ err = embedSubtitles(verticalVideoPath, stepParam.ShortOriginMixedSrtFilePath, stepParam.TaskBasePath, false)
+ if err != nil {
+ log.GetLogger().Error("generateAudioSubtitles embedSubtitles err", zap.Any("step param", stepParam), zap.Error(err))
+ return err
+ }
+ }
+ log.GetLogger().Info("字幕嵌入视频成功")
+ return nil
+ }
+ log.GetLogger().Info("合成字幕嵌入视频:不合成")
+ return nil
+}
+
func (s Service) uploadSubtitles(ctx context.Context, stepParam *types.SubtitleTaskStepParam) error {
subtitleInfos := make([]types.SubtitleInfo, 0)
var err error
@@ -569,6 +624,7 @@ func (s Service) audioToSrt(ctx context.Context, stepParam *types.SubtitleTaskSt
zap.Any("stepParam", stepParam), zap.Error(err))
return err
}
+ stepParam.ShortOriginMixedSrtFilePath = shortOriginMixedFile
// 合并最终原始字幕 短英文
shortOriginFile := fmt.Sprintf("%s/%s", stepParam.TaskBasePath, types.SubtitleTaskShortOriginSrtFileName)
diff --git a/internal/types/embed_subtitle.go b/internal/types/embed_subtitle.go
new file mode 100644
index 00000000..ddc7ac63
--- /dev/null
+++ b/internal/types/embed_subtitle.go
@@ -0,0 +1,32 @@
+package types
+
+const AssHeaderHorizontal = `[Script Info]
+Title: Example
+Original Script:
+ScriptType: v4.00+
+PlayDepth: 0
+
+[V4+ Styles]
+Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
+Style: Major,Arial,18,&H00BFFF,&H000000FF,&H00000000,&H64000000,-1,0,0,0,100,100,0,0,1,2.5,1.5,2,10,10,20,1
+Style: Minor,Arial,12,&H00BFFF,&H000000FF,&H00000000,&H64000000,-1,0,0,0,100,100,0,0,1,2.5,1.5,2,10,10,30,1
+
+
+[Events]
+Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
+`
+const AssHeaderVertical = `[Script Info]
+Title: Example
+Original Script:
+ScriptType: v4.00+
+PlayDepth: 0
+
+[V4+ Styles]
+Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
+Style: Major,Arial,15,&H00BFFF,&H000000FF,&H00000000,&H64000000,-1,0,0,0,100,100,-10,0,1,2.5,1.5,2,10,10,80,1
+Style: Minor,Arial,8,&H00BFFF,&H000000FF,&H00000000,&H64000000,-1,0,0,0,100,100,-10,0,1,2.5,1.5,2,10,10,100,1
+
+
+[Events]
+Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
+`
diff --git a/internal/types/subtitle_task.go b/internal/types/subtitle_task.go
index 241d0de7..f561bd8a 100644
--- a/internal/types/subtitle_task.go
+++ b/internal/types/subtitle_task.go
@@ -133,6 +133,9 @@ const (
SubtitleTaskOriginLanguageSrtFileName = "origin_language_srt.srt"
SubtitleTaskTargetLanguageSrtFileName = "target_language_srt.srt"
SubtitleTaskStepParamGobPersistenceFileName = "step_param.gob"
+ SubtitleTaskTransferredVerticalVideoFileName = "transferred_vertical_video.mp4"
+ SubtitleTaskHorizontalEmbedVideoFileName = "horizontal_embed.mp4"
+ SubtitleTaskVerticalEmbedVideoFileName = "vertical_embed.mp4"
)
const (
@@ -234,6 +237,10 @@ type SubtitleTaskStepParam struct {
SubtitleInfos []SubtitleFileInfo
TtsSourceFilePath string
TtsResultFilePath string
+ InputVideoPath string // 源视频路径
+ EmbedSubtitleVideoType string // 合成字幕嵌入的视频类型 none不嵌入 horizontal横屏 vertical竖屏
+ VerticalVideoMajorTitle string // 合成竖屏视频的主标题
+ VerticalVideoMinorTitle string
}
type SrtSentence struct {
diff --git a/pkg/util/base.go b/pkg/util/base.go
index 45f1c294..98a62533 100644
--- a/pkg/util/base.go
+++ b/pkg/util/base.go
@@ -131,3 +131,34 @@ func CleanPunction(word string) string {
return unicode.IsPunct(r)
})
}
+
+func IsAlphabetic(r rune) bool {
+ if unicode.IsLetter(r) { // 中文在IsLetter中会返回true
+ switch {
+ // 英语及其他拉丁字母的范围
+ case r >= 'A' && r <= 'Z', r >= 'a' && r <= 'z':
+ return true
+ // 扩展拉丁字母(法语、西班牙语等使用的附加字符)
+ case r >= '\u00C0' && r <= '\u024F':
+ return true
+ // 希腊字母
+ case r >= '\u0370' && r <= '\u03FF':
+ return true
+ // 西里尔字母(俄语等)
+ case r >= '\u0400' && r <= '\u04FF':
+ return true
+ default:
+ return false
+ }
+ }
+ return false
+}
+
+func ContainsAlphabetic(text string) bool {
+ for _, r := range text {
+ if IsAlphabetic(r) {
+ return true
+ }
+ }
+ return false
+}
diff --git a/static/index.html b/static/index.html
index a6a6e0ed..98d9f7ee 100644
--- a/static/index.html
+++ b/static/index.html
@@ -344,6 +344,21 @@
<button type="button" id="add-row">新增替换</button>
</div>
</div>
+
+ <!-- 合成字幕嵌入视频 -->
+ <div class="formGroup hidden" id="embed-subtitle-container">
+ <label>合成字幕嵌入视频:</label>
+ <input type="checkbox" id="embed-subtitle-toggle">
+ <select id="embed-subtitle">
+ <option value="horizontal">横屏视频</option>
+ <option value="vertical">竖屏视频</option>
+ <option value="all">横屏+竖屏视频</option>
+ </select>
+ <div id="subtitle-text-inputs" class="hidden">
+ <input type="text" id="subtitle-text-1" placeholder="请输入竖屏视频主标题">
+ <input type="text" id="subtitle-text-2" placeholder="请输入竖屏视频副标题">
+ </div>
+ </div>
</div>
<button type="submit" id="executeButton">执 行</button>
@@ -384,6 +399,13 @@
const wordReplacementToggle = document.getElementById("word-replacement-toggle");
const wordReplacementContainer = document.getElementById("word-replacement-container");
const addRowButton = document.getElementById("add-row");
+ const embedSubtitleVideoTypeToggle = document.getElementById("embed-subtitle-toggle");
+ const embedSubtitleVideoType = document.getElementById("embed-subtitle");
+ const embedSubtitleContainer = document.getElementById("embed-subtitle-container");
+ const embedSubtitle = document.getElementById("embed-subtitle");
+ const subtitleTextInputs = document.getElementById("subtitle-text-inputs");
+ const verticalMajorTitle = document.getElementById("subtitle-text-1");
+ const verticalMinorTitle = document.getElementById("subtitle-text-2");
const API_SUBMIT_URL = "/api/capability/subtitleTask";
const API_PROGRESS_URL = "/api/capability/subtitleTask";
@@ -454,6 +476,16 @@
voiceFileInputRemove.style.display = 'none';
});
+ // 监听“合成字幕嵌入视频”选项的变化
+ embedSubtitle.addEventListener("change", () => {
+ const selectedValue = embedSubtitle.value;
+ if (selectedValue === "vertical" || selectedValue === "all") {
+ subtitleTextInputs.classList.remove("hidden");
+ } else {
+ subtitleTextInputs.classList.add("hidden");
+ }
+ });
+
// 提交表单
function getParams () {
const formData = {
@@ -464,6 +496,7 @@
translation_subtitle_pos: +bilingualPosition.value,
tts: voiceoverToggle.checked ? 1 : 2,
modal_filter: document.getElementById("filler-filter-toggle").checked ? 1 : 2,
+ embed_subtitle_video_type: embedSubtitleVideoTypeToggle.checked ? embedSubtitleVideoType.value : 'none',
};
if (formData.tts === 1) {
@@ -488,6 +521,12 @@
}).filter(item => item !== null)
}
+ // 添加字幕文本到请求参数中
+ if (embedSubtitleVideoTypeToggle.checked && (embedSubtitle.value === "vertical" || embedSubtitle.value === "all")) {
+ formData.vertical_major_title = verticalMajorTitle.value;
+ formData.vertical_minor_title = verticalMinorTitle.value;
+ }
+
console.log("表单数据:", formData);
return formData;
@@ -502,12 +541,14 @@
if (radio.value === 'url') {
urlInputContainer.classList.remove('hidden');
fileInputContainer.classList.add('hidden');
+ embedSubtitleContainer.classList.add('hidden');
fileInput.required = false;
urlInput.required = true;
executeButton.disabled = false; // 启用“执行”按钮
} else {
urlInputContainer.classList.add('hidden');
fileInputContainer.classList.remove('hidden');
+ embedSubtitleContainer.classList.remove('hidden');
urlInput.required = false;
fileInput.required = true;
executeButton.disabled = true; // 禁用“执行”按钮,等待上传完成
@@ -590,6 +631,11 @@
return;
}
+ if (!bilingualToggle.checked && embedSubtitleVideoTypeToggle) {
+ alert('合成字幕嵌入视频必须打开双语选项')
+ return;
+ }
+
const inputType = document.querySelector('input[name="inputType"]:checked').value;
// const params = {
// bilingual: 1,
@@ -676,40 +722,12 @@
displayDownloadLinks(data.subtitle_info || [], data.speech_download_url || '');
return;
}
- await new Promise(resolve => setTimeout(resolve, 3000));
+ await new Promise(resolve => setTimeout(resolve, 5000));
}
} catch (error) {
alert(error);
console.error('查询进度失败:', error);
}
-
- // return new Promise(resolve => {
- // let progress = 0;
- // const interval = setInterval(() => {
- // progress += 10;
- // progressBar.style.width = progress + '%';
- // progressBar.textContent = progress + '%';
- //
- // if (progress >= 100) {
- // clearInterval(interval);
- // const subtitleInfo = [
- // {
- // name:"中文字幕", // 字幕名称
- // download_url:"https://cdn.krillin.ai/srt/6b10jGNNbXQ_zh.srt"
- // },
- // {
- // name:"英文字幕", // 字幕名称
- // download_url:"https://cdn.krillin.ai/srt/6b10jGNNbXQ_en.srt"
- // },
- // {
- // name:"中英文字幕", // 字幕名称
- // download_url:"https://cdn.krillin.ai/srt/6b10jGNNbXQ_zh_en.srt"
- // }
- // ];
- // displayDownloadLinks(subtitleInfo);
- // }
- // }, 500); // 每 500 毫秒更新一次进度
- // });
}
// 显示下载链接