Skip to content

Commit

Permalink
feat: add fonts api
Browse files Browse the repository at this point in the history
  • Loading branch information
gsxhnd committed Apr 29, 2024
1 parent 41d62c3 commit 4abaf58
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 42 deletions.
3 changes: 2 additions & 1 deletion garage_cmd/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ var ffmpegBatchAddSubCmd = &cli.Command{

if !opt.Exec {
for _, cmd := range cmds {
logger.Infof(cmd)
fmt.Println(cmd)
// logger.Infof(cmd)
}
return nil
}
Expand Down
40 changes: 23 additions & 17 deletions garage_ffmpeg/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ type VideoBatchOption struct {
}

type VideoBatcher interface {
GetVideosList() ([]string, error) // 获取视频列表s
GetFontsList() ([]string, error) // 获取字体列表
GetFontsParams() ([]string, error) // 获取字体列表
GetConvertBatch() ([][]string, error) // 获取转换视频命令
GetAddFontsBatch() ([][]string, error) // 获取添加字体命令
GetAddSubtittleBatch() ([]string, error) // 获取添加字幕命令
GetVideosList() ([]string, error) // 获取视频列表s
GetFontsList() ([]string, error) // 获取字体列表
GetFontsParams() ([]string, error) // 获取字体列表
GetConvertBatch() ([][]string, error) // 获取转换视频命令
GetAddFontsBatch() ([][]string, error) // 获取添加字体命令
GetAddSubtittleBatch() ([][]string, error) // 获取添加字幕命令
ExecuteBatch(wOut, wError io.Writer, batchCmd [][]string) error
// GetExecBatch() rxgo.Observable
}
Expand All @@ -46,8 +46,6 @@ type videoBatch struct {

var FONT_EXT = []string{".ttf", ".otf", ".ttc"}

const ADD_SUB_TEMPLATE = `-i "%s" -sub_charenc UTF-8 -i "%s" -map 0 -map 1 -metadata:s:s:%v language=%v -metadata:s:s:%v title="%v" -c copy %s "%v"`

func NewVideoBatch(opt *VideoBatchOption) (VideoBatcher, error) {
if err := utils.MakeDir(opt.OutputPath); err != nil {
return nil, err
Expand Down Expand Up @@ -178,7 +176,7 @@ func (vb *videoBatch) GetAddFontsBatch() ([][]string, error) {
return vb.cmdBatchs, nil
}

func (vb *videoBatch) GetAddSubtittleBatch() ([]string, error) {
func (vb *videoBatch) GetAddSubtittleBatch() ([][]string, error) {
videosList, err := vb.GetVideosList()
if err != nil {
return nil, err
Expand All @@ -189,18 +187,26 @@ func (vb *videoBatch) GetAddSubtittleBatch() ([]string, error) {
return nil, err
}

outputVideosMap := vb.filterOutput(videosList)

for _, v := range videosList {
sourceVideo := filepath.Join(vb.option.InputPath, v+vb.option.InputFormat)
var cmd = []string{}
// TODO: sub title error
sourceSubtitle := filepath.Join(vb.option.InputPath, v+vb.option.InputSubSuffix)
destVideo := filepath.Join(vb.option.OutputPath, v+vb.option.InputFormat)
s := fmt.Sprintf(ADD_SUB_TEMPLATE,
sourceVideo, sourceSubtitle, vb.option.InputSubNo,
vb.option.InputSubLang, vb.option.InputSubNo, vb.option.InputSubTitle,
fontsParams, destVideo)
vb.cmdBatch = append(vb.cmdBatch, s)
cmd = append(cmd, "-i", fmt.Sprintf(`"%v"`, v))
cmd = append(cmd, "-sub_charenc", "UTF-8")
cmd = append(cmd, "-i", fmt.Sprintf(`"%v"`, sourceSubtitle), "-map", "0", "-map", "1")
cmd = append(cmd, fmt.Sprintf("-metadata:s:s:%v", vb.option.InputSubNo))
cmd = append(cmd, fmt.Sprintf("language=%v", vb.option.InputSubLang))
cmd = append(cmd, fmt.Sprintf("-metadata:s:s:%v", vb.option.InputSubNo))
cmd = append(cmd, fmt.Sprintf(`title="%v"`, vb.option.InputSubTitle))
cmd = append(cmd, "-c", "copy")
cmd = append(cmd, fontsParams...)
cmd = append(cmd, fmt.Sprintf(`"%v"`, outputVideosMap[v]))
vb.cmdBatchs = append(vb.cmdBatchs, cmd)
}

return vb.cmdBatch, nil
return vb.cmdBatchs, nil
}

func (vb *videoBatch) ExecuteBatch(wOut, wError io.Writer, cmdBatch [][]string) error {
Expand Down
66 changes: 60 additions & 6 deletions garage_server/handler/ffmpeg_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (

type FFmpegHandler interface {
Convert(ctx *fiber.Ctx) error
AddFonts(ctx *fiber.Ctx) error
AddSubtitle(ctx *fiber.Ctx) error
}

type ffmpegHander struct {
Expand All @@ -31,25 +33,35 @@ func NewFFmpegHandler(l utils.Logger, v *validator.Validate, t task.TaskMgr, svc
}
}

type convertModel struct {
Name string `json:"name" validate:"required"`
type model struct {
InputPath string `json:"inputPath" validate:"required"`
InputFormat string `json:"inputFormat" validate:"required"`
OutputPath string `json:"outputPath" validate:"required"`
OutputFormat string `json:"outputFormat" validate:"required"`
Advance string `json:"advance"`
Exec bool `json:"exec"`
}

type addFontsModel struct {
model
FontsPath string `json:"fontsPath" validate:"required"`
}

func (h *ffmpegHander) Convert(ctx *fiber.Ctx) error {
body := new(convertModel)
body := new(model)

if err := ctx.BodyParser(body); err != nil {
h.logger.Errorf("boyd parser error: %s", err.Error())
return nil
}

if err := h.validator.Struct(body); err != nil {
h.logger.Errorf("body validation error: %s", err.Error())
return nil
}

h.logger.Debugf("ffmpeg handler input path: %s", body.Name)

task, err := task.NewFFmpegTask(&garage_ffmpeg.VideoBatchOption{
InputPath: body.Name,
InputPath: body.InputPath,
InputFormat: "mp4",
OutputPath: "/home/gsxhnd/Code/personal/garage/data",
OutputFormat: "mkv",
Expand All @@ -68,3 +80,45 @@ func (h *ffmpegHander) Convert(ctx *fiber.Ctx) error {
}
return nil
}

func (h *ffmpegHander) AddFonts(ctx *fiber.Ctx) error {
body := new(addFontsModel)

if err := ctx.BodyParser(body); err != nil {
h.logger.Errorf("boyd parser error: %s", err.Error())
return nil
}

if err := h.validator.Struct(body); err != nil {
h.logger.Errorf("body validation error: %s", err.Error())
return nil
}

task, err := task.NewFFmpegTask(&garage_ffmpeg.VideoBatchOption{
InputPath: body.InputPath,
InputFormat: body.InputFormat,
OutputPath: body.OutputPath,
OutputFormat: body.OutputFormat,
FontsPath: body.FontsPath,
Advance: body.Advance,
Exec: body.Exec,
}, "add_fonts")
if err != nil {
h.logger.Errorf("init task error: %s", err.Error())
return nil
}

h.taskManager.AddTask(task)
h.logger.Debugf("Task id: %s", task.GetId())

var data = map[string]interface{}{
"id": task.GetId(),
"cmds": task.GetCmds(),
}

return ctx.JSON(data)
}

func (h *ffmpegHander) AddSubtitle(ctx *fiber.Ctx) error {
return nil
}
2 changes: 2 additions & 0 deletions garage_server/routes/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ func (r *router) ApiInit() {

ffmpegGroup := api.Group("/ffmpeg")
ffmpegGroup.Post("/convert", r.h.FFmpegHander.Convert)
ffmpegGroup.Post("/add_fonts", r.h.FFmpegHander.AddFonts)
ffmpegGroup.Post("/add_subtitle", r.h.FFmpegHander.Convert)

javGroup := api.Group("/jav")
javGroup.Post("/code", r.h.JavHandler.CrawlJavByCode)
Expand Down
1 change: 1 addition & 0 deletions garage_server/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Task interface {
GetId() string
Run()
GetOB() rxgo.Observable
GetCmds() [][]string
}

type taskOb struct {
Expand Down
56 changes: 38 additions & 18 deletions garage_server/task/task_ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type ffmpegTask struct {
cmd string
batcher garage_ffmpeg.VideoBatcher
ob *taskOb
exec bool
cmds [][]string
}

func NewFFmpegTask(opt *garage_ffmpeg.VideoBatchOption, cmd string) (Task, error) {
Expand All @@ -21,12 +23,35 @@ func NewFFmpegTask(opt *garage_ffmpeg.VideoBatchOption, cmd string) (Task, error
return nil, err
}

return &ffmpegTask{
var task = &ffmpegTask{
id: uuid.New().String(),
cmd: cmd,
batcher: batcher,
exec: opt.Exec,
ob: newTaskOb(),
}, nil
cmds: make([][]string, 0),
}

switch cmd {
case "convert":
cmds, err := batcher.GetConvertBatch()
for _, c := range cmds {
fmt.Println("cmd: ", c)
}

if err != nil || len(cmds) == 0 {
return nil, err
}
case "add_fonts":
cmds, err := batcher.GetAddFontsBatch()
if err != nil {
return nil, err
}
task.cmds = cmds
default:
}

return task, nil
}

func (t *ffmpegTask) GetId() string {
Expand All @@ -37,23 +62,18 @@ func (t *ffmpegTask) GetOB() rxgo.Observable {
return t.ob.ob
}

func (t *ffmpegTask) GetCmds() [][]string {
return t.cmds
}

func (t *ffmpegTask) Run() {
switch t.cmd {
case "convert":
cmds, err := t.batcher.GetConvertBatch()
for _, c := range cmds {
fmt.Println("cmd: ", c)
}
if !t.exec {
return
}

if err != nil || len(cmds) == 0 {
return
}
t.ob.ch <- rxgo.Of(cmds)
if err := t.batcher.ExecuteBatch(t.ob, t.ob, cmds); err != nil {
t.ob.ch <- rxgo.Of("error executing batch")
close(t.ob.ch)
return
}
default:
if err := t.batcher.ExecuteBatch(t.ob, t.ob, t.cmds); err != nil {
t.ob.ch <- rxgo.Of("error executing batch")
close(t.ob.ch)
return
}
}
4 changes: 4 additions & 0 deletions garage_server/task/task_javbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ func (t *javbusTask) GetId() string {
func (t *javbusTask) GetOB() rxgo.Observable {
return nil
}

func (t *javbusTask) GetCmds() [][]string {
return nil
}

0 comments on commit 4abaf58

Please sign in to comment.