Skip to content

Commit

Permalink
fix advance error
Browse files Browse the repository at this point in the history
  • Loading branch information
gsxhnd committed Apr 26, 2024
1 parent a6c639c commit 42a42cd
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 42 deletions.
4 changes: 3 additions & 1 deletion garage_cmd/ffmpeg.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"os"

"github.com/gsxhnd/garage/garage_ffmpeg"
Expand Down Expand Up @@ -233,7 +234,8 @@ var ffmpegBatchAddFontCmd = &cli.Command{

if !opt.Exec {
for _, cmd := range cmds {
logger.Infof(cmd)
// logger.Infow(cmd)
fmt.Println(cmd)
}
return nil
}
Expand Down
48 changes: 28 additions & 20 deletions garage_ffmpeg/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ type VideoBatchOption struct {
type VideoBatcher interface {
GetVideosList() ([]string, error) // 获取视频列表s
GetFontsList() ([]string, error) // 获取字体列表
GetFontsParams() (string, error) // 获取字体列表
GetFontsParams() ([]string, error) // 获取字体列表
GetConvertBatch() ([][]string, error) // 获取转换视频命令
GetAddFontsBatch() ([]string, error) // 获取添加字体命令
GetAddFontsBatch() ([][]string, error) // 获取添加字体命令
GetAddSubtittleBatch() ([]string, error) // 获取添加字幕命令
ExecuteBatch(wOut, wError io.Writer, batchCmd [][]string) error
// GetExecBatch() rxgo.Observable
Expand All @@ -46,10 +46,7 @@ type videoBatch struct {

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

const CONVERT_TEMPLATE = `-i "%v" %v "%v"`
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"`
const ADD_FONT_TEMPLATE = `-i "%s" -c copy %s "%v"`
const FONT_TEMPLATE = `-attach "%s" -metadata:s:t:%v mimetype=application/x-truetype-font `

func NewVideoBatch(opt *VideoBatchOption) (VideoBatcher, error) {
if err := utils.MakeDir(opt.OutputPath); err != nil {
Expand Down Expand Up @@ -116,19 +113,23 @@ func (vb *videoBatch) GetFontsList() ([]string, error) {
}
}

func (vb *videoBatch) GetFontsParams() (string, error) {
var fontsParams = ""
func (vb *videoBatch) GetFontsParams() ([]string, error) {
var fontsCmdList = []string{}
fontsList, err := vb.GetFontsList()
if err != nil {
return "", err
return nil, err
}

for i, v := range fontsList {
fontPath := filepath.Join(vb.option.FontsPath, v)
fontsParams += fmt.Sprintf(FONT_TEMPLATE, fontPath, i)
fontsCmdList = append(fontsCmdList,
"-attach",
filepath.Join(vb.option.FontsPath, v),
fmt.Sprintf("-metadata:s:t:%v", i),
"mimetype=application/x-truetype-font",
)
}

return fontsParams, nil
return fontsCmdList, nil
}

func (vb *videoBatch) GetConvertBatch() ([][]string, error) {
Expand All @@ -141,31 +142,40 @@ func (vb *videoBatch) GetConvertBatch() ([][]string, error) {

for _, v := range videosList {
cmd := []string{"-i"}
cmd = append(cmd, v, vb.option.Advance, outputVideosMap[v])
cmd = append(cmd, v)
if vb.option.Advance != "" {
cmd = append(cmd, vb.option.Advance)
}
cmd = append(cmd, outputVideosMap[v])
vb.cmdBatchs = append(vb.cmdBatchs, cmd)
}

return vb.cmdBatchs, nil
}

func (vb *videoBatch) GetAddFontsBatch() ([]string, error) {
func (vb *videoBatch) GetAddFontsBatch() ([][]string, error) {
videosList, err := vb.GetVideosList()
if err != nil {
return nil, err
}

fontsParams, err := vb.GetFontsParams()
fontCmd, err := vb.GetFontsParams()
if err != nil {
return nil, err
}

outputVideosMap := vb.filterOutput(videosList)
for _, v := range videosList {
outputVideo := filepath.Join(vb.option.OutputPath, filepath.Base(v))
s := fmt.Sprintf(ADD_FONT_TEMPLATE, v, fontsParams, outputVideo)
vb.cmdBatch = append(vb.cmdBatch, s)
var batchCmd = []string{
"-i", v,
"-c", "copy",
}
batchCmd = append(batchCmd, fontCmd...)
batchCmd = append(batchCmd, outputVideosMap[v])
vb.cmdBatchs = append(vb.cmdBatchs, batchCmd)
}

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

func (vb *videoBatch) GetAddSubtittleBatch() ([]string, error) {
Expand Down Expand Up @@ -194,7 +204,6 @@ func (vb *videoBatch) GetAddSubtittleBatch() ([]string, error) {
}

func (vb *videoBatch) ExecuteBatch(wOut, wError io.Writer, cmdBatch [][]string) error {
// TODO: cmd need list
if !vb.option.Exec {
return nil
}
Expand All @@ -206,7 +215,6 @@ func (vb *videoBatch) ExecuteBatch(wOut, wError io.Writer, cmdBatch [][]string)
cmd = exec.Command("ffmpeg", c...)
}

fmt.Println(c)
cmd.Stdout = wOut
cmd.Stderr = wError
err := cmd.Run()
Expand Down
81 changes: 61 additions & 20 deletions garage_ffmpeg/ffmpeg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ var correctMkvVideos = []string{
"../testdata/2/2.mkv",
}

var correctFontsParams = `-attach "../testdata/1/1.ttf" -metadata:s:t:0 mimetype=application/x-truetype-font -attach "../testdata/1/2.ttf" -metadata:s:t:1 mimetype=application/x-truetype-font -attach "../testdata/2/1.ttf" -metadata:s:t:2 mimetype=application/x-truetype-font -attach "../testdata/2/2.ttf" -metadata:s:t:3 mimetype=application/x-truetype-font `

func TestNewVideoBatch(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -125,18 +123,26 @@ func Test_videoBatch_GetFontsList(t *testing.T) {
}
}

var correctFontsParams = `-attach "../testdata/1/1.ttf" -metadata:s:t:0 mimetype=application/x-truetype-font -attach "../testdata/1/2.ttf" -metadata:s:t:1 mimetype=application/x-truetype-font -attach "../testdata/2/1.ttf" -metadata:s:t:2 mimetype=application/x-truetype-font -attach "../testdata/2/2.ttf" -metadata:s:t:3 mimetype=application/x-truetype-font `
var correctFontsCmd = []string{
"-attach", "../testdata/1/1.ttf", "-metadata:s:t:0", "mimetype=application/x-truetype-font",
"-attach", "../testdata/1/2.ttf", "-metadata:s:t:1", "mimetype=application/x-truetype-font",
"-attach", "../testdata/2/1.ttf", "-metadata:s:t:2", "mimetype=application/x-truetype-font",
"-attach", "../testdata/2/2.ttf", "-metadata:s:t:3", "mimetype=application/x-truetype-font",
}

func Test_videoBatch_GetFontsParams(t *testing.T) {
type args struct {
FontsPath string
}
tests := []struct {
name string
args args
want string
want []string
wantErr bool
}{
{"test_succ", args{FontsPath: "../testdata"}, correctFontsParams, false},
{"test_err", args{FontsPath: "../111"}, "", true},
{"test_succ", args{FontsPath: "../testdata"}, correctFontsCmd, false},
{"test_err", args{FontsPath: "../111"}, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -157,10 +163,10 @@ func Test_videoBatch_GetFontsParams(t *testing.T) {
}

var correctConvertBatch = [][]string{
{"-i", "../testdata/1/1.mp4", "", "../testdata/output/1.mkv"},
{"-i", "../testdata/1/2.mp4", "", "../testdata/output/2.mkv"},
{"-i", "../testdata/2/1.mp4", "", "../testdata/output/1-1.mkv"},
{"-i", "../testdata/2/2.mp4", "", "../testdata/output/2-1.mkv"},
{"-i", "../testdata/1/1.mp4", "../testdata/output/1.mkv"},
{"-i", "../testdata/1/2.mp4", "../testdata/output/2.mkv"},
{"-i", "../testdata/2/1.mp4", "../testdata/output/1-1.mkv"},
{"-i", "../testdata/2/2.mp4", "../testdata/output/2-1.mkv"},
}

func Test_videoBatch_GetConvertBatch(t *testing.T) {
Expand Down Expand Up @@ -203,11 +209,27 @@ func Test_videoBatch_GetConvertBatch(t *testing.T) {
}
}

var correctAddFontsBatch = []string{
`-i "../testdata/1/1.mkv" -c copy ` + correctFontsParams + ` "../testdata/output/1.mkv"`,
`-i "../testdata/1/2.mkv" -c copy ` + correctFontsParams + ` "../testdata/output/2.mkv"`,
`-i "../testdata/2/1.mkv" -c copy ` + correctFontsParams + ` "../testdata/output/1.mkv"`,
`-i "../testdata/2/2.mkv" -c copy ` + correctFontsParams + ` "../testdata/output/2.mkv"`,
var correctAddFontsCmdBatch = func() [][]string {
var a = [][]string{
{"-i", "../testdata/1/1.mkv", "-c", "copy"},
{"-i", "../testdata/1/2.mkv", "-c", "copy"},
{"-i", "../testdata/2/1.mkv", "-c", "copy"},
{"-i", "../testdata/2/2.mkv", "-c", "copy"},
}

var b = []string{
"../testdata/output/1.mkv",
"../testdata/output/2.mkv",
"../testdata/output/1-1.mkv",
"../testdata/output/2-1.mkv",
}
var c = [][]string{}
for i, v := range a {
v = append(v, correctFontsCmd...)
v = append(v, b[i])
c = append(c, v)
}
return c
}

func Test_videoBatch_GetAddFontsBatch(t *testing.T) {
Expand All @@ -221,20 +243,21 @@ func Test_videoBatch_GetAddFontsBatch(t *testing.T) {
tests := []struct {
name string
args args
want []string
want [][]string
wantErr bool
}{
{"test_succ", args{InputPath: "../testdata", InputFormat: "mkv", FontsPath: "../testdata", OutputPath: "../testdata/output"}, correctAddFontsBatch, false},
{"test_succ", args{InputPath: "../testdata", InputFormat: "mkv", FontsPath: "../testdata", OutputPath: "../testdata/output"}, correctAddFontsCmdBatch(), false},
{"test_fail", args{InputPath: "../111", InputFormat: "mkv", OutputPath: "../testdata/output", OutputFormat: "mkv"}, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
vb := &videoBatch{
option: &VideoBatchOption{
InputPath: tt.args.InputPath,
InputFormat: tt.args.InputFormat,
OutputPath: tt.args.OutputPath,
FontsPath: tt.args.FontsPath,
InputPath: tt.args.InputPath,
InputFormat: tt.args.InputFormat,
OutputPath: tt.args.OutputPath,
OutputFormat: tt.args.InputFormat,
FontsPath: tt.args.FontsPath,
},
}

Expand Down Expand Up @@ -285,3 +308,21 @@ func Test_videoBatch_filterOutput(t *testing.T) {
})
}
}

func Test_videoBatch_ExecuteBatch(t *testing.T) {
tests := []struct {
name string
}{
{"test"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// var a = []string{"-i", "../testdata/1/1.mp4", "", "../data/1.mkv"}

// cmd := exec.Command("ffmpeg", a...)
// cmd.Stdout = os.Stdout
// cmd.Stderr = os.Stdout
// cmd.Run()
})
}
}
5 changes: 4 additions & 1 deletion garage_server/task/task_ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ func (t *ffmpegTask) Run() {
switch t.cmd {
case "convert":
cmds, err := t.batcher.GetConvertBatch()
fmt.Println(cmds)
for _, c := range cmds {
fmt.Println("cmd: ", c)
}

if err != nil || len(cmds) == 0 {
return
}
Expand Down

0 comments on commit 42a42cd

Please sign in to comment.