diff --git a/garage_cmd/ffmpeg.go b/garage_cmd/ffmpeg.go index 403e9ab..313d803 100644 --- a/garage_cmd/ffmpeg.go +++ b/garage_cmd/ffmpeg.go @@ -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 @@ -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 @@ -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 diff --git a/garage_ffmpeg/ffmpeg.go b/garage_ffmpeg/ffmpeg.go index d78198a..2a6f1d1 100644 --- a/garage_ffmpeg/ffmpeg.go +++ b/garage_ffmpeg/ffmpeg.go @@ -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 { @@ -45,7 +41,6 @@ type videoBatch struct { fontsList []string fontsParams string cmdBatch []string - logger *zap.Logger } const CONVERT_TEMPLATE = `ffmpeg.exe -i "%v" %v "%v"` @@ -53,9 +48,8 @@ const ADD_SUB_TEMPLATE = `ffmpeg.exe -i "%s" -sub_charenc UTF-8 -i "%s" -map 0 - 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), @@ -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 { @@ -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 @@ -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) @@ -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) @@ -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 } @@ -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 @@ -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 } diff --git a/garage_ffmpeg/ffmpeg_test.go b/garage_ffmpeg/ffmpeg_test.go index 98f2963..217e98f 100644 --- a/garage_ffmpeg/ffmpeg_test.go +++ b/garage_ffmpeg/ffmpeg_test.go @@ -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) }) } } @@ -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)