Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
gsxhnd committed Feb 27, 2024
1 parent 0bbff3e commit cbd30fe
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 45 deletions.
7 changes: 4 additions & 3 deletions garage_cmd/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var ffmpegBatchConvertCmd = &cli.Command{
// advance = c.String("advance")
)

vb, err := garage_ffmpeg.NewVideoBatch(logger, nil)
vb, err := garage_ffmpeg.NewVideoBatch(nil)
if err != nil {
logger.Panic("Create dest path error", zap.Error(err))
return err
Expand Down Expand Up @@ -134,7 +134,7 @@ var ffmpegBatchAddSubCmd = &cli.Command{
// inputFontsPath = c.String("input-fonts-path")
// outputPath = c.String("output-path")
)
vb, err := garage_ffmpeg.NewVideoBatch(logger, nil)
vb, err := garage_ffmpeg.NewVideoBatch(nil)
if err != nil {
logger.Panic("Create dest path error", zap.Error(err))
return err
Expand Down Expand Up @@ -185,7 +185,8 @@ var ffmpegBatchAddFontCmd = &cli.Command{
// inputFontsPath = c.String("input-fonts-path")
// outputPath = c.String("output-path")
)
vb, err := garage_ffmpeg.NewVideoBatch(logger, nil)

vb, err := garage_ffmpeg.NewVideoBatch(nil)
if err != nil {
logger.Panic("Create dest path error", zap.Error(err))
return err
Expand Down
65 changes: 30 additions & 35 deletions garage_ffmpeg/ffmpeg.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package garage_ffmpeg

import (
"errors"
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"time"

"go.uber.org/zap"
)

type VideoBatchOption struct {
Expand Down Expand Up @@ -45,17 +41,15 @@ type videoBatch struct {
fontsList []string
fontsParams string
cmdBatch []string
logger *zap.Logger
}

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

func NewVideoBatch(l *zap.Logger, opt *VideoBatchOption) (VideoBatcher, error) {
func NewVideoBatch(opt *VideoBatchOption) (VideoBatcher, error) {
client := &videoBatch{
logger: l,
option: opt,
videosList: make([]string, 0),
fontsList: make([]string, 0),
Expand Down Expand Up @@ -94,22 +88,22 @@ func (vb *videoBatch) StartAddSubtittleBatch() error {
return err
}

vb.logger.Debug("Source videos directory: " + vb.option.InputPath)
vb.logger.Debug("Get matching video count: " + strconv.Itoa(len(vb.videosList)))
vb.logger.Debug("Target video's subtitle stream number: " + strconv.Itoa(vb.option.InputSubNo))
vb.logger.Debug("Target video's subtitle language: " + vb.option.InputSubLang)
vb.logger.Debug("Target video's subtitle title: " + vb.option.InputSubTitle)
// vb.logger.Debug("Source videos directory: " + vb.option.InputPath)
// vb.logger.Debug("Get matching video count: " + strconv.Itoa(len(vb.videosList)))
// vb.logger.Debug("Target video's subtitle stream number: " + strconv.Itoa(vb.option.InputSubNo))
// vb.logger.Debug("Target video's subtitle language: " + vb.option.InputSubLang)
// vb.logger.Debug("Target video's subtitle title: " + vb.option.InputSubTitle)

if vb.option.FontsPath != "" {
if err := vb.getFontsList(); err != nil {
return err
}
vb.logger.Info("Target video's font paths: " + vb.option.FontsPath)
vb.logger.Info(fmt.Sprintf("Attach fonts parameters: %v", vb.fontsParams))
// vb.logger.Info("Target video's font paths: " + vb.option.FontsPath)
// vb.logger.Info(fmt.Sprintf("Attach fonts parameters: %v", vb.fontsParams))
} else {
vb.logger.Info("Target video's font paths not set, skip.")
// vb.logger.Info("Target video's font paths not set, skip.")
}
vb.logger.Info("Dest video directory: " + vb.option.OutputPath)
// vb.logger.Info("Dest video directory: " + vb.option.OutputPath)

template := `ffmpeg.exe -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"`
for _, v := range vb.videosList {
Expand All @@ -122,7 +116,7 @@ func (vb *videoBatch) StartAddSubtittleBatch() error {
vb.fontsParams, destVideo)
vb.cmdBatch = append(vb.cmdBatch, s)
}
vb.logger.Info("Get all videos, starting convert")
// vb.logger.Info("Get all videos, starting convert")

if vb.option.Exec {
return nil
Expand All @@ -139,11 +133,11 @@ func (vb *videoBatch) StartAddFontsBatch() error {
return err
}

vb.logger.Info("Source videos directory: " + vb.option.InputPath)
vb.logger.Info("Get matching video count: " + strconv.Itoa(len(vb.videosList)))
vb.logger.Info("Target video's font paths: " + vb.option.FontsPath)
vb.logger.Info(fmt.Sprintf("Attach fonts parameters: %v", vb.fontsParams))
vb.logger.Info("Dest video directory: " + vb.option.OutputPath)
// vb.logger.Info("Source videos directory: " + vb.option.InputPath)
// vb.logger.Info("Get matching video count: " + strconv.Itoa(len(vb.videosList)))
// vb.logger.Info("Target video's font paths: " + vb.option.FontsPath)
// vb.logger.Info(fmt.Sprintf("Attach fonts parameters: %v", vb.fontsParams))
// vb.logger.Info("Dest video directory: " + vb.option.OutputPath)
template := `ffmpeg.exe -i "%s" -c copy %s "%v"`
for _, v := range vb.videosList {
sourceVideo := filepath.Join(vb.option.InputPath, v+vb.option.InputFormat)
Expand All @@ -160,7 +154,7 @@ func (vb *videoBatch) StartAddFontsBatch() error {

func (vb *videoBatch) createDestDir() error {
destDir := path.Join(vb.option.OutputPath)
vb.logger.Info("Start creating destination directory: " + destDir)
// vb.logger.Info("Start creating destination directory: " + destDir)
if fi, err := os.Stat(destDir); err != nil {
if os.IsNotExist(err) {
os.MkdirAll(destDir, os.ModePerm)
Expand All @@ -169,10 +163,11 @@ func (vb *videoBatch) createDestDir() error {
}
} else {
if fi.IsDir() {
vb.logger.Info("Destination directory already exists")
return errors.New("destination directory already exists")
// vb.logger.Info("Destination directory already exists")
}
}
vb.logger.Info("Destination directory created")
// vb.logger.Info("Destination directory created")
return nil
}

Expand All @@ -187,12 +182,12 @@ func (vb *videoBatch) getVideosList() error {
if fi.IsDir() {
return nil
}

filename := fi.Name()
vb.logger.Debug("get video filename: " + filename)
// vb.logger.Debug("get video filename: " + filename)
fileExt := filepath.Ext(filename)
if fileExt == vb.option.InputFormat {
fileName := strings.TrimSuffix(filename, fileExt)
vb.videosList = append(vb.videosList, fileName)
if fileExt == "."+vb.option.InputFormat {
vb.videosList = append(vb.videosList, path)
return nil
}
return nil
Expand Down Expand Up @@ -242,21 +237,21 @@ func (vb *videoBatch) getFontsParams() (string, error) {
func (vb *videoBatch) executeBatch() error {
for _, cmd := range vb.cmdBatch {
if vb.option.Exec {
vb.logger.Sugar().Infof("cmd: %v", cmd)
// vb.logger.Sugar().Infof("cmd: %v", cmd)
return nil
}

startTime := time.Now()
vb.logger.Sugar().Infof("Start convert video cmd: %v", cmd)
// startTime := time.Now()
// vb.logger.Sugar().Infof("Start convert video cmd: %v", cmd)
cmd := exec.Command("powershell", cmd)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
vb.logger.Sugar().Errorf("cmd error: %v", err)
// vb.logger.Sugar().Errorf("cmd error: %v", err)
return err
}
vb.logger.Sugar().Infof("Finished convert video, spent time: %v sec", time.Since(startTime).Seconds())
// vb.logger.Sugar().Infof("Finished convert video, spent time: %v sec", time.Since(startTime).Seconds())
}
return nil
}
33 changes: 26 additions & 7 deletions garage_ffmpeg/ffmpeg_test.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,60 @@
package garage_ffmpeg

import (
"strconv"
"testing"

"github.com/gsxhnd/garage/utils"
"github.com/stretchr/testify/assert"
)

var logger = utils.GetLogger()
func createCorrectVideos(inputPath, formart string) []string {
var a = make([]string, 0)
for i := 1; i < 5; i++ {
for j := 1; j < 6; j++ {
a = append(a, inputPath+"/"+strconv.Itoa(i)+"/"+strconv.Itoa(j)+"."+formart)
}
}
return a
}

func Test_videoBatch_getVideosList(t *testing.T) {
type args struct {
inputPath string
InputFormat string
}

tests := []struct {
name string
args args
want []string
wantErr bool
}{
{"test_mkv", args{inputPath: "../testdata", InputFormat: ".mkv"}, []string{"1", "2", "3", "4", "5"}, false},
{"test_mp4", args{inputPath: "../testdata", InputFormat: ".mp4"}, []string{"1", "2", "3", "4", "5"}, false},
{"test_mkv", args{inputPath: "../testdata", InputFormat: "mkv"}, []string{}, false},
{"test_mp4", args{inputPath: "../testdata", InputFormat: "mp4"}, []string{"1", "2", "3", "4", "5"}, false},
// {"test_err", args{sourceRootPath: "../../testdata", sourceVideoType: ".mp4"}, []string{}, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
vb := &videoBatch{
logger: logger,
option: &VideoBatchOption{
InputPath: tt.args.inputPath,
InputFormat: tt.args.InputFormat,
},
}

err := vb.getVideosList()
t.Log(vb.videosList)

if tt.wantErr {
assert.Error(t, err)
}

var cList = createCorrectVideos(tt.args.inputPath, tt.args.InputFormat)
t.Log(cList)

assert.Nil(t, err)
assert.Equal(t, vb.videosList, cList)
})
}
}
Expand All @@ -52,13 +71,13 @@ func Test_videoBatch_getFontsParams(t *testing.T) {
}{
// TODO: Add test cases.
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
vb := &videoBatch{
option: tt.fields.option,

logger: logger,
}

got, err := vb.getFontsParams()
if (err != nil) != tt.wantErr {
t.Errorf("videoBatch.getFontsParams() error = %v, wantErr %v", err, tt.wantErr)
Expand Down

0 comments on commit cbd30fe

Please sign in to comment.