Skip to content

Commit

Permalink
feat: support more flexible encode param format (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tohrusky authored Jan 31, 2025
1 parent 7d4aa78 commit fe654b6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions common/constant/constant.go
Original file line number Diff line number Diff line change
@@ -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))
}
9 changes: 5 additions & 4 deletions module/ffmpeg/vapoursynth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"os/exec"
"runtime"

"github.com/TensoRaws/FinalRip/common/constant"
"github.com/TensoRaws/FinalRip/module/log"
"github.com/TensoRaws/FinalRip/module/util"
)

// 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
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
11 changes: 11 additions & 0 deletions server/internal/service/task/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.")
Expand Down
7 changes: 4 additions & 3 deletions worker/internal/encode/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit fe654b6

Please sign in to comment.