From 1b1cdd5c22c57574cb98433469d012c57a7f7b35 Mon Sep 17 00:00:00 2001 From: Leonid Nikitin Date: Sun, 8 Jun 2025 21:47:28 +0500 Subject: [PATCH] Add `RunConvert` method to `FFmpegContract` and implementation Extend `FFmpegContract` with `RunConvert` for handling file conversion, including progress tracking and callback support before and after execution. --- internal/ffmpeg/ffmpeg.go | 52 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/internal/ffmpeg/ffmpeg.go b/internal/ffmpeg/ffmpeg.go index db5a30d..b303182 100644 --- a/internal/ffmpeg/ffmpeg.go +++ b/internal/ffmpeg/ffmpeg.go @@ -5,13 +5,20 @@ import ( "errors" "fyne.io/fyne/v2/lang" "git.kor-elf.net/kor-elf/gui-for-ffmpeg/internal/utils" + "io" "os/exec" "strings" ) +type ProgressContract interface { + GetProtocole() string + Run(stdOut io.ReadCloser, stdErr io.ReadCloser) error +} + type FFmpegContract interface { GetPath() string GetEncoders(scanner func(scanner *bufio.Reader)) error + RunConvert(setting ConvertSetting, progress ProgressContract, beforeWait func(cmd *exec.Cmd), afterWait func(cmd *exec.Cmd)) error } type ffmpeg struct { @@ -40,6 +47,51 @@ func (f *ffmpeg) GetPath() string { return f.path } +func (f *ffmpeg) RunConvert(setting ConvertSetting, progress ProgressContract, beforeWait func(cmd *exec.Cmd), afterWait func(cmd *exec.Cmd)) error { + overwriteOutputFiles := "-n" + if setting.OverwriteOutputFiles == true { + overwriteOutputFiles = "-y" + } + args := []string{overwriteOutputFiles, "-i", setting.FileInput.Path} + args = append(args, setting.Encoder.GetParams()...) + args = append(args, "-progress", progress.GetProtocole(), setting.FileOut.Path) + cmd := exec.Command(f.path, args...) + utils.PrepareBackgroundCommand(cmd) + + stdOut, err := cmd.StdoutPipe() + if err != nil { + return err + } + stdErr, err := cmd.StderrPipe() + if err != nil { + return err + } + + err = cmd.Start() + if err != nil { + return err + } + + if beforeWait != nil { + beforeWait(cmd) + } + + errProgress := progress.Run(stdOut, stdErr) + + err = cmd.Wait() + if afterWait != nil { + afterWait(cmd) + } + if errProgress != nil { + return errProgress + } + if err != nil { + return err + } + + return nil +} + func (f *ffmpeg) GetEncoders(scanner func(scanner *bufio.Reader)) error { cmd := exec.Command(f.path, "-encoders") utils.PrepareBackgroundCommand(cmd)