diff --git a/Makefile b/Makefile index 93ee4d5..a7c39cf 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ GO ?= go .DEFAULT_GOAL := default version := v0.2.2 -VS_PYTORCH_VERSION := v0.1.1 +VS_PYTORCH_VERSION := dev .PHONY: tidy tidy: ## go mod tidy diff --git a/common/constant/constant.go b/common/constant/constant.go new file mode 100644 index 0000000..645fd8d --- /dev/null +++ b/common/constant/constant.go @@ -0,0 +1,18 @@ +package constant + +import "strings" + +type FinalRipConstant string + +const ( + FINALRIP = FinalRipConstant("FINALRIP") + ENV_FINALRIP_SOURCE = FINALRIP + FinalRipConstant("_SOURCE") + FINALRIP_SOURCE_MKV = FINALRIP + FinalRipConstant("_SOURCE.mkv") + FINALRIP_ENCODED_CLIP = FINALRIP + FinalRipConstant("_ENCODED_CLIP") + FINALRIP_ENCODED_CLIP_MKV = FINALRIP + FinalRipConstant("_ENCODED_CLIP.mkv") +) + +// ContainsFinalRipInString 检查字符串中是否包含 FinalRip 常量 +func ContainsFinalRipInString(s string, constant FinalRipConstant) bool { + return strings.Contains(s, string(constant)) +} diff --git a/module/ffmpeg/vapoursynth.go b/module/ffmpeg/vapoursynth.go index 938b92e..63fbe73 100644 --- a/module/ffmpeg/vapoursynth.go +++ b/module/ffmpeg/vapoursynth.go @@ -5,6 +5,7 @@ import ( "os/exec" "runtime" + "github.com/TensoRaws/FinalRip/common/constant" "github.com/TensoRaws/FinalRip/module/log" "github.com/TensoRaws/FinalRip/module/util" ) @@ -12,7 +13,7 @@ import ( // EncodeVideo 压制视频,压制后的视频文件名为 encoded.mkv,压制参数由 encodeParam 指定,压制视频从环境变量 FINALRIP_SOURCE 读取 func EncodeVideo(encodeScript string, encodeParam string) error { encodeScriptPath := "encode.py" - // encodedVideo := "encoded.mkv" + // 根据操作系统创建脚本文件 var commandStr string var scriptPath string @@ -25,8 +26,8 @@ func EncodeVideo(encodeScript string, encodeParam string) error { scriptPath = "temp_script.sh" condaInitScript = "#!/bin/bash\n" // linux 下默认激活 conda 环境 } - commandStr = condaInitScript + "vspipe -c y4m encode.py - | " + encodeParam + " encoded.mkv" - log.Logger.Info("commandStr: " + commandStr) + commandStr = condaInitScript + "vspipe -c y4m " + encodeScriptPath + " - | " + encodeParam + log.Logger.Info("Encode Command: " + commandStr) // 清理临时文件 _ = util.ClearTempFile(encodeScriptPath, scriptPath) @@ -60,7 +61,7 @@ func EncodeVideo(encodeScript string, encodeParam string) error { log.Logger.Error("vs error: " + err.Error()) return err } - log.Logger.Info("clip encoded, output: encoded.mkv") + log.Logger.Info("clip encoded, output: " + constant.FINALRIP_ENCODED_CLIP_MKV) return nil } diff --git a/server/internal/service/task/start.go b/server/internal/service/task/start.go index 72f7d70..60d86bc 100644 --- a/server/internal/service/task/start.go +++ b/server/internal/service/task/start.go @@ -5,6 +5,7 @@ import ( "sync" "time" + "github.com/TensoRaws/FinalRip/common/constant" "github.com/TensoRaws/FinalRip/common/db" "github.com/TensoRaws/FinalRip/common/task" "github.com/TensoRaws/FinalRip/module/log" @@ -34,6 +35,16 @@ func Start(c *gin.Context) { return } + // 检查传入的 Script 和 EncodeParam 是否合法 + if !constant.ContainsFinalRipInString(req.Script, constant.ENV_FINALRIP_SOURCE) { + resp.AbortWithMsg(c, "VS script code must contain "+string(constant.ENV_FINALRIP_SOURCE)+" environment variable to specify the source video.") //nolint:lll + return + } + if !constant.ContainsFinalRipInString(req.EncodeParam, constant.FINALRIP_ENCODED_CLIP_MKV) { + resp.AbortWithMsg(c, "Encode param must contain "+string(constant.FINALRIP_ENCODED_CLIP_MKV)+" to specify the output video clip.") //nolint:lll + return + } + // 检查任务是否 new 上传 if !db.CheckTaskExist(req.VideoKey) { resp.AbortWithMsg(c, "Task not found, please upload video first.") diff --git a/worker/internal/encode/worker.go b/worker/internal/encode/worker.go index 0577136..13827ec 100644 --- a/worker/internal/encode/worker.go +++ b/worker/internal/encode/worker.go @@ -6,6 +6,7 @@ import ( "os" "time" + "github.com/TensoRaws/FinalRip/common/constant" "github.com/TensoRaws/FinalRip/common/db" "github.com/TensoRaws/FinalRip/common/task" "github.com/TensoRaws/FinalRip/module/ffmpeg" @@ -40,8 +41,8 @@ func Handler(ctx context.Context, t *asynq.Task) error { log.Logger.Errorf("Failed to kill vspipe process: %v", err) } - tempSourceVideo := "source.mkv" - tempEncodedVideo := "encoded.mkv" + tempSourceVideo := string(constant.FINALRIP_SOURCE_MKV) + tempEncodedVideo := string(constant.FINALRIP_ENCODED_CLIP_MKV) // 清理临时文件 _ = util.ClearTempFile(tempSourceVideo, tempEncodedVideo) @@ -67,7 +68,7 @@ func Handler(ctx context.Context, t *asynq.Task) error { log.Logger.Infof("Downloaded video clip %s", p.Clip.ClipKey) // 设置临时视频的环境变量 - err = os.Setenv("FINALRIP_SOURCE", tempSourceVideo) + err = os.Setenv(string(constant.ENV_FINALRIP_SOURCE), tempSourceVideo) if err != nil { log.Logger.Errorf("Failed to set env FINALRIP_SOURCE: %v", err) return err