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
Improve features and logging
沈浩bfee405006d3
diff --git a/.gitignore b/.gitignoreindex d2b81075..538d5a0c 100644--- a/.gitignore+++ b/.gitignore@@ -1,2 +1,4 @@.idea-config/config.toml\ No newline at end of file+config/config.toml+tasks/+cookies.txt\ No newline at end of filediff --git a/config/config-example.toml b/config/config-example.tomlindex ade1ed30..5b453c65 100644--- a/config/config-example.toml+++ b/config/config-example.toml@@ -1,3 +1,8 @@+[app]+ base_path = "./tasks"+ segment_duration = 5+ translate_parallel_num = 5+[server]host = "127.0.0.1"port = 8888diff --git a/config/config.go b/config/config.goindex b03448b8..7d59636a 100644--- a/config/config.go+++ b/config/config.go@@ -4,6 +4,12 @@ import ("github.com/BurntSushi/toml")+type App struct {+ BasePath string `toml:"base_path"`+ SegmentDuration int `toml:"segment_duration"`+ TranslateParallelNum int `toml:"translate_parallel_num"`+}+type Server struct {Host string `toml:"host"`Port int `toml:"port"`@@ -14,6 +20,7 @@ type Openai struct {}type Config struct {+ App App `toml:"app"`Openai Openai `toml:"openai"`Server Server `toml:"server"`}diff --git a/go.mod b/go.modindex 61fcc6e9..97d0ef46 100644--- a/go.mod+++ b/go.mod@@ -5,6 +5,7 @@ go 1.22.0require (github.com/BurntSushi/toml v1.3.2github.com/gin-gonic/gin v1.10.0+ github.com/samber/lo v1.38.1github.com/sashabaranov/go-openai v1.36.0github.com/u2takey/ffmpeg-go v0.5.0github.com/wulien/jupiter v0.11.13@@ -53,6 +54,7 @@ require (go.uber.org/zap v1.25.0 // indirectgolang.org/x/arch v0.8.0 // indirectgolang.org/x/crypto v0.23.0 // indirect+ golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 // indirectgolang.org/x/net v0.25.0 // indirectgolang.org/x/sys v0.20.0 // indirectgolang.org/x/text v0.15.0 // indirectdiff --git a/go.sum b/go.sumindex 1d30fb28..dbf24498 100644--- a/go.sum+++ b/go.sum@@ -107,6 +107,8 @@ github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+Pymzigithub.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM=github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=+github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=+github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=github.com/sashabaranov/go-openai v1.36.0 h1:fcSrn8uGuorzPWCBp8L0aCR95Zjb/Dd+ZSML0YZy9EI=github.com/sashabaranov/go-openai v1.36.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=@@ -152,6 +154,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkgolang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=+golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 h1:QfTh0HpN6hlw6D3vu8DAwC8pBIwikq0AI1evdm+FksE=+golang.org/x/exp v0.0.0-20221031165847-c99f073a8326/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=diff --git a/internal/dto/subtitle_task.go b/internal/dto/subtitle_task.goindex f8fe572d..541fc0dd 100644--- a/internal/dto/subtitle_task.go+++ b/internal/dto/subtitle_task.go@@ -24,7 +24,7 @@ type StartVideoSubtitleTaskRes struct {}type GetVideoSubtitleTaskReq struct {- TaskId string `query:"task_id"`+ TaskId string `form:"taskId"`}type VideoInfo struct {@@ -42,7 +42,7 @@ type SubtitleInfo struct {type GetVideoSubtitleTaskResData struct {TaskId string `json:"task_id"`- ProcessPercent uint32 `json:"process_percent"`+ ProcessPercent uint8 `json:"process_percent"`VideoInfo *VideoInfo `json:"video_info"`SubtitleInfo []*SubtitleInfo `json:"subtitle_info"`TargetLanguage string `json:"target_language"`diff --git a/internal/handler/subtitle_task.go b/internal/handler/subtitle_task.goindex 1c39f779..3732ea19 100644--- a/internal/handler/subtitle_task.go+++ b/internal/handler/subtitle_task.go@@ -4,7 +4,6 @@ import ("github.com/gin-gonic/gin""krillin-ai/internal/dto""krillin-ai/internal/response"- "net/http")func (h Handler) StartSubtitleTask(c *gin.Context) {@@ -20,7 +19,7 @@ func (h Handler) StartSubtitleTask(c *gin.Context) {svc := h.Service- taskID, err := svc.StartSubtitleTask(req)+ data, err := svc.StartSubtitleTask(req)if err != nil {response.R(c, response.Response{Error: -1,@@ -29,14 +28,36 @@ func (h Handler) StartSubtitleTask(c *gin.Context) {})return}-- c.JSON(http.StatusOK, gin.H{- "error": 0,- "msg": "成功",- "data": gin.H{"task_id": taskID},+ response.R(c, response.Response{+ Error: 0,+ Msg: "成功",+ Data: data,})}-func GetSubtitleTaskStatus(c *gin.Context) {- // todo+func (h Handler) GetSubtitleTask(c *gin.Context) {+ var req dto.GetVideoSubtitleTaskReq+ if err := c.ShouldBindQuery(&req); err != nil {+ response.R(c, response.Response{+ Error: -1,+ Msg: "参数错误",+ Data: nil,+ })+ return+ }+ svc := h.Service+ data, err := svc.GetTaskStatus(req)+ if err != nil {+ response.R(c, response.Response{+ Error: -1,+ Msg: err.Error(),+ Data: nil,+ })+ return+ }+ response.R(c, response.Response{+ Error: 0,+ Msg: "成功",+ Data: data,+ })}diff --git a/internal/router/router.go b/internal/router/router.goindex 47f7940e..53d95d4a 100644--- a/internal/router/router.go+++ b/internal/router/router.go@@ -10,6 +10,6 @@ func SetupRouter(r *gin.Engine) {hdl := handler.NewHandler(){api.POST("/subtitleTask", hdl.StartSubtitleTask)- //api.GET("/subtitleTask", controllers.GetSubtitleTaskStatus)+ api.GET("/subtitleTask", hdl.GetSubtitleTask)}}diff --git a/internal/service/subtitle_service.go b/internal/service/subtitle_service.goindex 833db5dd..8a5510a0 100644--- a/internal/service/subtitle_service.go+++ b/internal/service/subtitle_service.go@@ -5,13 +5,15 @@ import ("context""errors""fmt"+ "github.com/samber/lo"ffmpeg "github.com/u2takey/ffmpeg-go"- "github.com/wulien/jupiter/pkg/conf"- "github.com/wulien/jupiter/pkg/xlog"+ "go.uber.org/zap""golang.org/x/sync/errgroup"+ "krillin-ai/config""krillin-ai/internal/dto""krillin-ai/internal/storage""krillin-ai/internal/types"+ "krillin-ai/log""krillin-ai/pkg/openai""krillin-ai/pkg/util""os"@@ -62,19 +64,19 @@ func (s Service) StartSubtitleTask(req dto.StartVideoSubtitleTaskReq) (*dto.Starif len(beforeAfter) == 2 {replaceWordsMap[beforeAfter[0]] = beforeAfter[1]} else {- xlog.Default().Info("generateAudioSubtitles replace param length err", xlog.Any("replace", replace), xlog.Any("taskId", taskId))+ log.GetLogger().Info("generateAudioSubtitles replace param length err", zap.Any("replace", replace), zap.Any("taskId", taskId))}}}var err errorctx := context.Background()// 创建字幕任务文件夹- taskBasePath := conf.GetString("subtitle.base_path") + taskId // 工作目录: ....../service/subtitle/task_id+ taskBasePath := filepath.Join(config.Conf.App.BasePath, taskId)if _, err = os.Stat(taskBasePath); os.IsNotExist(err) {// 不存在则创建err = os.MkdirAll(taskBasePath, os.ModePerm)if err != nil {- xlog.Default().Error("StartVideoSubtitleTask MkdirAll err", xlog.Any("req", req), xlog.FieldErr(err))+ log.GetLogger().Error("StartVideoSubtitleTask MkdirAll err", zap.Any("req", req), zap.Error(err))}}@@ -102,48 +104,48 @@ func (s Service) StartSubtitleTask(req dto.StartVideoSubtitleTaskReq) (*dto.Starconst size = 64 << 10buf := make([]byte, size)buf = buf[:runtime.Stack(buf, false)]- //xlog.Default().Error("autoVideoSubtitle panic", xlog.Any("panic:", r), xlog.Any("stack:", buf))+ log.GetLogger().Error("autoVideoSubtitle panic", zap.Any("panic:", r), zap.Any("stack:", buf))storage.SubtitleTasks[taskId].Status = types.SubtitleTaskStatusFailed}}()- //xlog.Default().Info("video subtitle start task", xlog.String("taskId", taskId))+ log.GetLogger().Info("video subtitle start task", zap.String("taskId", taskId))err = s.linkToAudioFile(ctx, &stepParam)if err != nil {- //xlog.Default().Error("StartVideoSubtitleTask linkToAudioFile err", xlog.Any("req", req), xlog.FieldErr(err))+ log.GetLogger().Error("StartVideoSubtitleTask linkToAudioFile err", zap.Any("req", req), zap.Error(err))storage.SubtitleTasks[stepParam.TaskId].Status = types.SubtitleTaskStatusFailedstorage.SubtitleTasks[stepParam.TaskId].FailReason = "link to audio error"return}err = s.getVideoInfo(ctx, &stepParam)if err != nil {- //xlog.Default().Error("StartVideoSubtitleTask getVideoInfo err", xlog.Any("req", req), xlog.FieldErr(err))+ log.GetLogger().Error("StartVideoSubtitleTask getVideoInfo err", zap.Any("req", req), zap.Error(err))storage.SubtitleTasks[stepParam.TaskId].Status = types.SubtitleTaskStatusFailedstorage.SubtitleTasks[stepParam.TaskId].FailReason = "get video info error"return}err = s.audioToSubtitle(ctx, &stepParam)if err != nil {- //xlog.Default().Error("StartVideoSubtitleTask audioToSubtitle err", xlog.Any("req", req), xlog.FieldErr(err))+ log.GetLogger().Error("StartVideoSubtitleTask audioToSubtitle err", zap.Any("req", req), zap.Error(err))storage.SubtitleTasks[stepParam.TaskId].Status = types.SubtitleTaskStatusFailedstorage.SubtitleTasks[stepParam.TaskId].FailReason = "audio to subtitle error"return}//err = s.srtFileToSpeech(ctx, &stepParam)//if err != nil {- // //xlog.Default().Error("StartVideoSubtitleTask srtFileToSpeech err", xlog.Any("req", req), xlog.FieldErr(err))+ // //zap.Default().Error("StartVideoSubtitleTask srtFileToSpeech err", zap.Any("req", req), zap.FieldErr(err))// storage.SubtitleTasks[stepParam.TaskId].Status = types.SubtitleTaskStatusFailed// storage.SubtitleTasks[stepParam.TaskId].FailReason = "srt file to speech error"// return//}err = s.uploadSubtitles(ctx, &stepParam)if err != nil {- //xlog.Default().Error("StartVideoSubtitleTask uploadSubtitles err", xlog.Any("req", req), xlog.FieldErr(err))+ log.GetLogger().Error("StartVideoSubtitleTask uploadSubtitles err", zap.Any("req", req), zap.Error(err))storage.SubtitleTasks[stepParam.TaskId].Status = types.SubtitleTaskStatusFailedstorage.SubtitleTasks[stepParam.TaskId].FailReason = "upload subtitles error"return}- //xlog.Default().Info("video subtitle task end", xlog.String("taskId", taskId))+ log.GetLogger().Info("video subtitle task end", zap.String("taskId", taskId))}()return &dto.StartVideoSubtitleTaskResData{@@ -151,9 +153,29 @@ func (s Service) StartSubtitleTask(req dto.StartVideoSubtitleTaskReq) (*dto.Star}, nil}-func (s Service) GetTaskStatus(req dto.GetVideoSubtitleTaskReq) (*dto.GetVideoSubtitleTaskRes, error) {- // todo- return nil, nil+func (s Service) GetTaskStatus(req dto.GetVideoSubtitleTaskReq) (*dto.GetVideoSubtitleTaskResData, error) {+ task := storage.SubtitleTasks[req.TaskId]+ if task == nil {+ return nil, errors.New("任务不存在")+ }+ return &dto.GetVideoSubtitleTaskResData{+ TaskId: task.TaskId,+ ProcessPercent: task.ProcessPct,+ VideoInfo: &dto.VideoInfo{+ Title: task.Title,+ Description: task.Description,+ TranslatedTitle: task.TranslatedTitle,+ TranslatedDescription: task.TranslatedDescription,+ },+ SubtitleInfo: lo.Map(task.SubtitleInfos, func(item types.SubtitleInfo, _ int) *dto.SubtitleInfo {+ return &dto.SubtitleInfo{+ Name: item.Name,+ DownloadUrl: item.DownloadUrl,+ }+ }),+ TargetLanguage: task.TargetLanguage,+ SpeechDownloadUrl: task.SpeechDownloadUrl,+ }, nil}// 新版流程:链接->本地音频文件->扣费->视频信息获取(若有)->本地字幕文件->cos上的字幕信息@@ -168,21 +190,18 @@ func (s Service) linkToAudioFile(ctx context.Context, stepParam *types.SubtitleTvar videoId stringvideoId, err = util.GetYouTubeID(link)if err != nil {- //xlog.Default().Error("linkToAudioFile.GetYouTubeID err", xlog.Any("step param", stepParam), xlog.FieldErr(err))+ log.GetLogger().Error("linkToAudioFile.GetYouTubeID err", zap.Any("step param", stepParam), zap.Error(err))return err}stepParam.Link = "https://www.youtube.com/watch?v=" + videoId// 使用 yt-dlp 下载音频并保存到指定目录cmdArgs := []string{"-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "192K", "-o", audioPath, stepParam.Link}- proxy := conf.GetString("subtitle.proxy")- if proxy != "" {- cmdArgs = append(cmdArgs, "--proxy", proxy)- //cmdArgs = append(cmdArgs, "--cookies", "./cookies.txt")- }++ cmdArgs = append(cmdArgs, "--cookies", "./cookies.txt")cmd := exec.Command("yt-dlp", cmdArgs...)err = cmd.Run()if err != nil {- //xlog.Default().Error("generateAudioSubtitles.Step2DownloadAudio yt-dlp err", xlog.Any("step param", stepParam), xlog.FieldErr(err))+ log.GetLogger().Error("generateAudioSubtitles.Step2DownloadAudio yt-dlp err", zap.Any("step param", stepParam), zap.Error(err))return err}} else if strings.Contains(link, "bilibili.com") {@@ -192,18 +211,18 @@ func (s Service) linkToAudioFile(ctx context.Context, stepParam *types.SubtitleT}stepParam.Link = "https://www.bilibili.com/video/" + videoIdcmdArgs := []string{"-f", "bestaudio[ext=m4a]", "-x", "--audio-format", "mp3", "-o", audioPath, stepParam.Link}- proxy := conf.GetString("subtitle.proxy")- if proxy != "" {- cmdArgs = append(cmdArgs, "--proxy", proxy)- }+ //proxy := conf.GetString("subtitle.proxy")+ //if proxy != "" {+ // cmdArgs = append(cmdArgs, "--proxy", proxy)+ //}cmd := exec.Command("yt-dlp", cmdArgs...)err = cmd.Run()if err != nil {- //xlog.Default().Error("generateAudioSubtitles.Step2DownloadAudio yt-dlp err", xlog.Any("step param", stepParam), xlog.FieldErr(err))+ log.GetLogger().Error("generateAudioSubtitles.Step2DownloadAudio yt-dlp err", zap.Any("step param", stepParam), zap.Error(err))return err}} else {- //xlog.Default().Info("linkToAudioFile.unsupported link type", xlog.Any("step param", stepParam))+ log.GetLogger().Info("linkToAudioFile.unsupported link type", zap.Any("step param", stepParam))return errors.New("invalid link")}stepParam.AudioFilePath = audioPath@@ -222,35 +241,37 @@ func (s Service) getVideoInfo(ctx context.Context, stepParam *types.SubtitleTask// 获取标题titleCmdArgs := []string{"--skip-download", "--encoding", "utf-8", "--get-title", stepParam.Link}descriptionCmdArgs := []string{"--skip-download", "--encoding", "utf-8", "--get-description", stepParam.Link}- proxy := conf.GetString("subtitle.proxy")- if proxy != "" {- titleCmdArgs = append(titleCmdArgs, "--proxy", proxy)- descriptionCmdArgs = append(descriptionCmdArgs, "--proxy", proxy)- //titleCmdArgs = append(titleCmdArgs, "--cookies", "./cookies.txt")- //descriptionCmdArgs = append(descriptionCmdArgs, "--cookies", "./cookies.txt")- }+ //proxy := conf.GetString("subtitle.proxy")+ //if proxy != "" {+ // titleCmdArgs = append(titleCmdArgs, "--proxy", proxy)+ // descriptionCmdArgs = append(descriptionCmdArgs, "--proxy", proxy)+ // titleCmdArgs = append(titleCmdArgs, "--cookies", "./cookies.txt")+ // descriptionCmdArgs = append(descriptionCmdArgs, "--cookies", "./cookies.txt")+ //}+ titleCmdArgs = append(titleCmdArgs, "--cookies", "./cookies.txt")+ descriptionCmdArgs = append(descriptionCmdArgs, "--cookies", "./cookies.txt")cmd := exec.Command("yt-dlp", titleCmdArgs...)var output []byteoutput, err = cmd.Output()if err != nil {- //xlog.Default().Error("getVideoInfo yt-dlp error", xlog.Any("stepParam", stepParam), xlog.FieldErr(err))+ log.GetLogger().Error("getVideoInfo yt-dlp error", zap.Any("stepParam", stepParam), zap.Error(err))// 不需要整个流程退出}title = string(output)cmd = exec.Command("yt-dlp", descriptionCmdArgs...)output, err = cmd.Output()if err != nil {- //xlog.Default().Error("getVideoInfo yt-dlp error", xlog.Any("stepParam", stepParam), xlog.FieldErr(err))+ log.GetLogger().Error("getVideoInfo yt-dlp error", zap.Any("stepParam", stepParam), zap.Error(err))}description = string(output)- //xlog.Default().Debug("getVideoInfo title and description", xlog.String("title", title), xlog.String("description", description))+ log.GetLogger().Debug("getVideoInfo title and description", zap.String("title", title), zap.String("description", description))// 翻译var result stringresult, err = s.OpenaiClient.ChatCompletion(fmt.Sprintf(types.TranslateVideoTitleAndDescriptionPrompt, types.GetStandardLanguageName(stepParam.TargetLanguage), title+"####"+description))if err != nil {- //xlog.Default().Error("getVideoInfo openai chat completion error", xlog.Any("stepParam", stepParam), xlog.FieldErr(err))+ log.GetLogger().Error("getVideoInfo openai chat completion error", zap.Any("stepParam", stepParam), zap.Error(err))}- //xlog.Default().Debug("getVideoInfo translate video info result", xlog.String("result", result))+ log.GetLogger().Debug("getVideoInfo translate video info result", zap.String("result", result))storage.SubtitleTasks[stepParam.TaskId].Title = titlestorage.SubtitleTasks[stepParam.TaskId].Description = description@@ -264,7 +285,7 @@ func (s Service) getVideoInfo(ctx context.Context, stepParam *types.SubtitleTaskstorage.SubtitleTasks[stepParam.TaskId].TranslatedTitle = splitResult[0]storage.SubtitleTasks[stepParam.TaskId].TranslatedDescription = splitResult[1]} else {- //xlog.Default().Error("getVideoInfo translate video info error split result length != 1 and 2", xlog.Any("stepParam", stepParam), xlog.Any("translate result", result), xlog.FieldErr(err))+ log.GetLogger().Error("getVideoInfo translate video info error split result length != 1 and 2", zap.Any("stepParam", stepParam), zap.Any("translate result", result), zap.Error(err))}}return nil@@ -298,7 +319,7 @@ func (s Service) uploadSubtitles(ctx context.Context, stepParam *types.SubtitleTreplacedSrcFile := util.AddSuffixToFileName(srcFile, "_replaced")err = util.ReplaceFileContent(srcFile, replacedSrcFile, stepParam.ReplaceWordsMap)if err != nil {- //xlog.Default().Error("generateAudioSubtitles.uploadSubtitles ReplaceFileContent err", xlog.Any("stepParam", stepParam), xlog.FieldErr(err))+ log.GetLogger().Error("generateAudioSubtitles.uploadSubtitles ReplaceFileContent err", zap.Any("stepParam", stepParam), zap.Error(err))return err}srcFile = replacedSrcFile@@ -321,11 +342,11 @@ func (s Service) uploadSubtitles(ctx context.Context, stepParam *types.SubtitleT}func (s Service) splitAudio(ctx context.Context, stepParam *types.SubtitleTaskStepParam) error {- xlog.Default().Info("audioToSubtitle.splitAudio start", xlog.String("task id", stepParam.TaskId))+ log.GetLogger().Info("audioToSubtitle.splitAudio start", zap.String("task id", stepParam.TaskId))var err error// 使用ffmpeg-go分割音频outputPattern := filepath.Join(stepParam.TaskBasePath, types.SubtitleTaskSplitAudioFileNamePattern) // 输出文件格式- segmentDuration := conf.GetInt("subtitle.segment_duration") * 60 // 每段的时长+ segmentDuration := config.Conf.App.SegmentDuration * 60 // 每段的时长err = ffmpeg.Input(stepParam.AudioFilePath).Output(outputPattern, ffmpeg.KwArgs{"f": "segment", // 设置输出文件类型为分段@@ -334,18 +355,18 @@ func (s Service) splitAudio(ctx context.Context, stepParam *types.SubtitleTaskSt}).OverWriteOutput().ErrorToStdOut().Run()if err != nil {- xlog.Default().Error("audioToSubtitle.splitAudio ffmpeg err", xlog.Any("stepParam", stepParam), xlog.FieldErr(err))+ log.GetLogger().Error("audioToSubtitle.splitAudio ffmpeg err", zap.Any("stepParam", stepParam), zap.Error(err))return err}// 获取分割后的文件列表audioFiles, err := filepath.Glob(filepath.Join(stepParam.TaskBasePath, fmt.Sprintf("%s_*.mp3", types.SubtitleTaskSplitAudioFileNamePrefix)))if err != nil {- xlog.Default().Error("audioToSubtitle.splitAudio filepath.Glob err", xlog.Any("stepParam", stepParam), xlog.FieldErr(err))+ log.GetLogger().Error("audioToSubtitle.splitAudio filepath.Glob err", zap.Any("stepParam", stepParam), zap.Error(err))return err}if len(audioFiles) == 0 {- xlog.Default().Error("audioToSubtitle.splitAudio no audio files found", xlog.Any("stepParam", stepParam))+ log.GetLogger().Error("audioToSubtitle.splitAudio no audio files found", zap.Any("stepParam", stepParam))return errors.New("no audio files found")}@@ -361,15 +382,15 @@ func (s Service) splitAudio(ctx context.Context, stepParam *types.SubtitleTaskSt// 更新字幕任务信息storage.SubtitleTasks[stepParam.TaskId].ProcessPct = 20- //xlog.Default().Info("audioToSubtitle.splitAudio end", xlog.String("task id", stepParam.TaskId))+ log.GetLogger().Info("audioToSubtitle.splitAudio end", zap.String("task id", stepParam.TaskId))return nil}func (s Service) audioToSrt(ctx context.Context, stepParam *types.SubtitleTaskStepParam) error {- xlog.Default().Info("audioToSubtitle.audioToSrt start", xlog.Any("taskId", stepParam.TaskId))+ log.GetLogger().Info("audioToSubtitle.audioToSrt start", zap.Any("taskId", stepParam.TaskId))var (stepNum = 0- parallelControlChan = make(chan struct{}, conf.GetInt("subtitle.translate_parallel_num"))+ parallelControlChan = make(chan struct{}, config.Conf.App.TranslateParallelNum)eg errgroup.GroupstepNumMu sync.Mutexerr error@@ -394,7 +415,7 @@ func (s Service) audioToSrt(ctx context.Context, stepParam *types.SubtitleTaskSt}}if err != nil {- xlog.Default().Error("audioToSubtitle.audioToSrt.Transcription err", xlog.Any("stepParam", stepParam), xlog.FieldErr(err))+ log.GetLogger().Error("audioToSubtitle.audioToSrt.Transcription err", zap.Any("stepParam", stepParam), zap.Error(err))return err}@@ -403,20 +424,20 @@ func (s Service) audioToSrt(ctx context.Context, stepParam *types.SubtitleTaskSt// 更新字幕任务信息stepNumMu.Lock()stepNum++- processPct := int8(20 + 70*stepNum/(len(stepParam.SmallAudios)*2))+ processPct := uint8(20 + 70*stepNum/(len(stepParam.SmallAudios)*2))stepNumMu.Unlock()storage.SubtitleTasks[stepParam.TaskId].ProcessPct = processPct// 拆分字幕并翻译err = s.splitTextAndTranslate(stepParam.TaskId, stepParam.TaskBasePath, stepParam.TargetLanguage, stepParam.EnableModalFilter, audioFile)if err != nil {- xlog.Default().Error("audioToSubtitle.audioToSrt.splitTextAndTranslate err", xlog.Any("stepParam", stepParam), xlog.FieldErr(err))+ log.GetLogger().Error("audioToSubtitle.audioToSrt.splitTextAndTranslate err", zap.Any("stepParam", stepParam), zap.Error(err))return err}stepNumMu.Lock()stepNum++- processPct = int8(20 + 70*stepNum/(len(stepParam.SmallAudios)*2))+ processPct = uint8(20 + 70*stepNum/(len(stepParam.SmallAudios)*2))stepNumMu.Unlock()storage.SubtitleTasks[stepParam.TaskId].ProcessPct = processPct@@ -424,7 +445,7 @@ func (s Service) audioToSrt(ctx context.Context, stepParam *types.SubtitleTaskSt// 生成时间戳err = s.generateTimestamps(stepParam.TaskId, stepParam.TaskBasePath, stepParam.OriginLanguage, stepParam.SubtitleResultType, audioFile)if err != nil {- xlog.Default().Error("audioToSubtitle.audioToSrt.generateTimestamps err", xlog.Any("stepParam", stepParam), xlog.FieldErr(err))+ log.GetLogger().Error("audioToSubtitle.audioToSrt.generateTimestamps err", zap.Any("stepParam", stepParam), zap.Error(err))return err}return nil@@ -432,7 +453,7 @@ func (s Service) audioToSrt(ctx context.Context, stepParam *types.SubtitleTaskSt}if err = eg.Wait(); err != nil {- xlog.Default().Error("audioToSubtitle.audioToSrt.eg.Wait err", xlog.Any("taskId", stepParam.TaskId), xlog.FieldErr(err))+ log.GetLogger().Error("audioToSubtitle.audioToSrt.eg.Wait err", zap.Any("taskId", stepParam.TaskId), zap.Error(err))return err}@@ -450,8 +471,8 @@ func (s Service) audioToSrt(ctx context.Context, stepParam *types.SubtitleTaskStoriginNoTsFile := fmt.Sprintf("%s/%s", stepParam.TaskBasePath, types.SubtitleTaskSrtNoTimestampFileName)err = util.MergeFile(originNoTsFile, originNoTsFiles...)if err != nil {- xlog.Default().Error("audioToSubtitle.audioToSrt.MergeFile originNoTs err",- xlog.Any("stepParam", stepParam), xlog.FieldErr(err))+ log.GetLogger().Error("audioToSubtitle.audioToSrt.MergeFile originNoTs err",+ zap.Any("stepParam", stepParam), zap.Error(err))return err}@@ -459,8 +480,8 @@ func (s Service) audioToSrt(ctx context.Context, stepParam *types.SubtitleTaskStbilingualFile := fmt.Sprintf("%s/%s", stepParam.TaskBasePath, types.SubtitleTaskBilingualSrtFileName)err = util.MergeSrtFiles(bilingualFile, bilingualFiles...)if err != nil {- xlog.Default().Error("audioToSubtitle.audioToSrt.MergeFile ts err",- xlog.Any("stepParam", stepParam), xlog.FieldErr(err))+ log.GetLogger().Error("audioToSubtitle.audioToSrt.MergeFile ts err",+ zap.Any("stepParam", stepParam), zap.Error(err))return err}@@ -470,20 +491,20 @@ func (s Service) audioToSrt(ctx context.Context, stepParam *types.SubtitleTaskSt// 更新字幕任务信息storage.SubtitleTasks[stepParam.TaskId].ProcessPct = 90- xlog.Default().Info("audioToSubtitle.audioToSrt end", xlog.Any("taskId", stepParam.TaskId))+ log.GetLogger().Info("audioToSubtitle.audioToSrt end", zap.Any("taskId", stepParam.TaskId))return nil}func (s Service) splitSrt(ctx context.Context, stepParam *types.SubtitleTaskStepParam) error {- xlog.Default().Info("audioToSubtitle.splitSrt start", xlog.Any("task id", stepParam.TaskId))+ log.GetLogger().Info("audioToSubtitle.splitSrt start", zap.Any("task id", stepParam.TaskId))originLanguageSrtFilePath := filepath.Join(stepParam.TaskBasePath, types.SubtitleTaskOriginLanguageSrtFileName)targetLanguageSrtFilePath := filepath.Join(stepParam.TaskBasePath, types.SubtitleTaskTargetLanguageSrtFileName)// 打开双语字幕文件file, err := os.Open(stepParam.BilingualSrtFilePath)if err != nil {- xlog.Default().Error("audioToSubtitle.splitSrt os.Open err", xlog.Any("stepParam", stepParam), xlog.FieldErr(err))+ log.GetLogger().Error("audioToSubtitle.splitSrt os.Open err", zap.Any("stepParam", stepParam), zap.Error(err))return err}defer file.Close()@@ -491,13 +512,13 @@ func (s Service) splitSrt(ctx context.Context, stepParam *types.SubtitleTaskStep// 打开输出文件originLanguageSrtFile, err := os.Create(originLanguageSrtFilePath)if err != nil {- xlog.Default().Error("audioToSubtitle.splitSrt os.Create originLanguageSrtFile err", xlog.Any("stepParam", stepParam), xlog.FieldErr(err))+ log.GetLogger().Error("audioToSubtitle.splitSrt os.Create originLanguageSrtFile err", zap.Any("stepParam", stepParam), zap.Error(err))return err}defer originLanguageSrtFile.Close()targetLanguageSrtFile, err := os.Create(targetLanguageSrtFilePath)if err != nil {- xlog.Default().Error("audioToSubtitle.splitSrt os.Create targetLanguageSrtFile err", xlog.Any("stepParam", stepParam), xlog.FieldErr(err))+ log.GetLogger().Error("audioToSubtitle.splitSrt os.Create targetLanguageSrtFile err", zap.Any("stepParam", stepParam), zap.Error(err))return err}defer targetLanguageSrtFile.Close()@@ -525,7 +546,7 @@ func (s Service) splitSrt(ctx context.Context, stepParam *types.SubtitleTaskStep}if err = scanner.Err(); err != nil {- xlog.Default().Error("audioToSubtitle.splitSrt scanner.Err err", xlog.Any("stepParam", stepParam), xlog.FieldErr(err))+ log.GetLogger().Error("audioToSubtitle.splitSrt scanner.Err err", zap.Any("stepParam", stepParam), zap.Error(err))return err}// 添加原语言单语字幕@@ -568,7 +589,7 @@ func (s Service) splitSrt(ctx context.Context, stepParam *types.SubtitleTaskStepstepParam.TtsSourceFilePath = stepParam.BilingualSrtFilePath}- xlog.Default().Info("audioToSubtitle.splitSrt end", xlog.Any("task id", stepParam.TaskId))+ log.GetLogger().Info("audioToSubtitle.splitSrt end", zap.Any("task id", stepParam.TaskId))return nil}@@ -806,7 +827,7 @@ func (s Service) generateTimestamps(taskId, basePath string, originLanguage type// 获取原始无时间戳字幕内容srtBlocks, err := util.ParseSrtNoTsToSrtBlock(audioFile.SrtNoTsFile)if err != nil {- xlog.Default().Error("generateAudioSubtitles.generateTimestamps.ReadSrtBlocks err", xlog.String("taskId", taskId), xlog.FieldErr(err))+ log.GetLogger().Error("generateAudioSubtitles.generateTimestamps.ReadSrtBlocks err", zap.String("taskId", taskId), zap.Error(err))return err}@@ -821,7 +842,7 @@ func (s Service) generateTimestamps(taskId, basePath string, originLanguage typecontinue}lastTs = ts- tsOffset := conf.GetFloat64("subtitle.segment_duration") * 60 * float64(audioFile.Num-1)+ tsOffset := float64(config.Conf.App.SegmentDuration) * 60 * float64(audioFile.Num-1)srtBlock.Timestamp = fmt.Sprintf("%s --> %s", util.FormatTime(float32(sentenceTs.Start+tsOffset)), util.FormatTime(float32(sentenceTs.End+tsOffset)))}@@ -829,7 +850,7 @@ func (s Service) generateTimestamps(taskId, basePath string, originLanguage typefinalBilingualSrtFileName := fmt.Sprintf("%s/%s", basePath, fmt.Sprintf(types.SubtitleTaskSplitBilingualSrtFileNamePattern, audioFile.Num))finalBilingualSrtFile, err := os.Create(finalBilingualSrtFileName)if err != nil {- xlog.Default().Error("generateAudioSubtitles.generateTimestamps.os.Open err", xlog.String("taskId", taskId), xlog.FieldErr(err))+ log.GetLogger().Error("generateAudioSubtitles.generateTimestamps.os.Open err", zap.String("taskId", taskId), zap.Error(err))return err}defer finalBilingualSrtFile.Close()@@ -874,7 +895,7 @@ func (s Service) splitTextAndTranslate(taskId, baseTaskPath string, targetLangua}if err != nil {- xlog.Default().Error("generateAudioSubtitles.splitTextAndTranslate.ChatCompletion err", xlog.Any("taskId", taskId), xlog.FieldErr(err))+ log.GetLogger().Error("generateAudioSubtitles.splitTextAndTranslate.ChatCompletion err", zap.Any("taskId", taskId), zap.Error(err))return err}@@ -882,7 +903,7 @@ func (s Service) splitTextAndTranslate(taskId, baseTaskPath string, targetLanguaoriginNoTsSrtFile := fmt.Sprintf("%s/%s", baseTaskPath, fmt.Sprintf(types.SubtitleTaskSplitSrtNoTimestampFileNamePattern, audioFile.Num))err = os.WriteFile(originNoTsSrtFile, []byte(splitContent), 0644)if err != nil {- xlog.Default().Error("generateAudioSubtitles.splitTextAndTranslate.os.WriteFile err", xlog.Any("taskId", taskId), xlog.FieldErr(err))+ log.GetLogger().Error("generateAudioSubtitles.splitTextAndTranslate.os.WriteFile err", zap.Any("taskId", taskId), zap.Error(err))return err}diff --git a/internal/types/subtitle_task.go b/internal/types/subtitle_task.goindex 2721a669..73a0d0c2 100644--- a/internal/types/subtitle_task.go+++ b/internal/types/subtitle_task.go@@ -270,7 +270,7 @@ type SubtitleTask struct {Status uint8 `json:"status" gorm:"column:status"` // 1-处理中,2-成功,3-失败LastSuccessStepNum uint8 `json:"last_success_step_num" gorm:"column:last_success_step_num"` // 最后成功的子任务序号,用于任务恢复FailReason string `json:"fail_reason" gorm:"column:fail_reason"` // 失败原因- ProcessPct int8 `json:"process_percent" gorm:"column:process_percent"` // 处理进度+ ProcessPct uint8 `json:"process_percent" gorm:"column:process_percent"` // 处理进度Duration uint32 `json:"duration" gorm:"column:duration"` // 视频时长SrtNum int `json:"srt_num" gorm:"column:srt_num"` // 字幕数量SubtitleInfos []SubtitleInfo `gorm:"foreignKey:TaskId;references:TaskId"`diff --git a/log/zap.go b/log/zap.gonew file mode 100644index 00000000..c0dcc604--- /dev/null+++ b/log/zap.go@@ -0,0 +1,17 @@+package log++import "go.uber.org/zap"++var Logger *zap.Logger++func InitLogger() {+ var err error+ Logger, err = zap.NewProduction()+ if err != nil {+ panic("无法初始化日志: " + err.Error())+ }+}++func GetLogger() *zap.Logger {+ return Logger+}diff --git a/logger/logger.go b/logger/logger.gonew file mode 100644index 00000000..94ab776d--- /dev/null+++ b/logger/logger.go@@ -0,0 +1,9 @@+package logger++import "go.uber.org/zap"++var logger *zap.Logger++func init() {+ logger, _ = zap.NewProduction()+}diff --git a/main.go b/main.goindex ba701d94..58ea6bd9 100644--- a/main.go+++ b/main.go@@ -5,6 +5,7 @@ import ("github.com/gin-gonic/gin""krillin-ai/config""krillin-ai/internal/router"+ "krillin-ai/log")type App struct {@@ -17,9 +18,14 @@ func main() {if err != nil {panic(fmt.Sprintf("加载配置文件失败: %v", err))}++ log.InitLogger()+ defer log.GetLogger().Sync() // 确保日志被正确写入+app := App{Engine: gin.Default(),}+app.Engine = gin.Default()router.SetupRouter(app.Engine)_ = app.Engine.Run(fmt.Sprintf("%s:%d", config.Conf.Server.Host, config.Conf.Server.Port))