From ebc8832d4d54b351c77ab6f613b57d8cb54ea94e Mon Sep 17 00:00:00 2001 From: Leonid Nikitin Date: Thu, 18 Jan 2024 20:23:23 +0600 Subject: [PATCH] Made it possible to choose where to save. --- src/convertor/service.go | 18 +++++------ src/convertor/view.go | 65 ++++++++++++++++++++++++++++++++++++---- src/handler/convertor.go | 30 +++++++++++-------- src/helper/helper.go | 10 +++++++ src/main.go | 6 ++-- 5 files changed, 99 insertions(+), 30 deletions(-) create mode 100644 src/helper/helper.go diff --git a/src/convertor/service.go b/src/convertor/service.go index 8d82a12..e1828a2 100644 --- a/src/convertor/service.go +++ b/src/convertor/service.go @@ -33,8 +33,10 @@ type File struct { } type ConvertSetting struct { - VideoFileInput *File - SocketPath string + VideoFileInput *File + VideoFileOut *File + SocketPath string + OverwriteOutputFiles bool } type ConvertData struct { @@ -48,13 +50,11 @@ func NewService(ffPathUtilities FFPathUtilities) *Service { } func (s Service) RunConvert(setting ConvertSetting) error { - //args := strings.Split("-report -n -c:v libx264", " ") - //args := strings.Split("-n -c:v libx264", " ") - //args = append(args, "-progress", "unix://"+setting.SocketPath, "-i", setting.VideoFileInput.Path, "file-out.mp4") - //args := "-report -n -i " + setting.VideoFileInput.Path + " -c:v libx264 -progress unix://" + setting.SocketPath + " output-file.mp4" - //args := "-n -i " + setting.VideoFileInput.Path + " -c:v libx264 -progress unix://" + setting.SocketPath + " output-file.mp4" - //args := "-y -i " + setting.VideoFileInput.Path + " -c:v libx264 -progress unix://" + setting.SocketPath + " output-file.mp4" - args := []string{"-y", "-i", setting.VideoFileInput.Path, "-c:v", "libx264", "-progress", "unix://" + setting.SocketPath, "output-file.mp4"} + overwriteOutputFiles := "-n" + if setting.OverwriteOutputFiles == true { + overwriteOutputFiles = "-y" + } + args := []string{overwriteOutputFiles, "-i", setting.VideoFileInput.Path, "-c:v", "libx264", "-progress", "unix://" + setting.SocketPath, setting.VideoFileOut.Path} cmd := exec.Command(s.ffPathUtilities.FFmpeg, args...) out, err := cmd.CombinedOutput() diff --git a/src/convertor/view.go b/src/convertor/view.go index 1d0dd05..e5325dd 100644 --- a/src/convertor/view.go +++ b/src/convertor/view.go @@ -1,6 +1,7 @@ package convertor import ( + "errors" "fyne.io/fyne/v2" "fyne.io/fyne/v2/canvas" "fyne.io/fyne/v2/container" @@ -21,12 +22,15 @@ type View struct { } type HandleConvertSetting struct { - VideoFileInput *File - SocketPath string + VideoFileInput *File + DirectoryForSave string + SocketPath string + OverwriteOutputFiles bool } type enableFormConversionStruct struct { fileVideoForConversion *widget.Button + buttonForSelectedDir *widget.Button form *widget.Form } @@ -45,23 +49,40 @@ func (v View) Main( conversionMessage.TextStyle = fyne.TextStyle{Bold: true} progress := widget.NewProgressBar() - progress.Hide() fileVideoForConversion, fileVideoForConversionMessage, fileInput := v.getButtonFileVideoForConversion(form, progress, conversionMessage) + buttonForSelectedDir, buttonForSelectedDirMessage, pathToSaveDirectory := v.getButtonForSelectingDirectoryForSaving() + + isOverwriteOutputFiles := false + checkboxOverwriteOutputFiles := widget.NewCheck("Разрешить перезаписать файл", func(b bool) { + isOverwriteOutputFiles = b + }) form.Items = []*widget.FormItem{ {Text: "Файл для ковертации:", Widget: fileVideoForConversion}, {Widget: fileVideoForConversionMessage}, + {Text: "Папка куда будет сохранятся:", Widget: buttonForSelectedDir}, + {Widget: buttonForSelectedDirMessage}, + {Widget: checkboxOverwriteOutputFiles}, } form.SubmitText = "Конвертировать" enableFormConversionStruct := enableFormConversionStruct{ fileVideoForConversion: fileVideoForConversion, + buttonForSelectedDir: buttonForSelectedDir, form: form, } form.OnSubmit = func() { + if len(*pathToSaveDirectory) == 0 { + showConversionMessage(conversionMessage, errors.New("Не выбрали папку для сохранения!")) + enableFormConversion(enableFormConversionStruct) + return + } + conversionMessage.Text = "" + fileVideoForConversion.Disable() + buttonForSelectedDir.Disable() form.Disable() socketPath, err := getSocketPath(fileInput, progress) @@ -73,8 +94,10 @@ func (v View) Main( } setting := HandleConvertSetting{ - VideoFileInput: fileInput, - SocketPath: socketPath, + VideoFileInput: fileInput, + DirectoryForSave: *pathToSaveDirectory, + SocketPath: socketPath, + OverwriteOutputFiles: isOverwriteOutputFiles, } err = runConvert(setting) if err != nil { @@ -125,6 +148,37 @@ func (v View) getButtonFileVideoForConversion(form *widget.Form, progress *widge return button, fileVideoForConversionMessage, fileInput } +func (v View) getButtonForSelectingDirectoryForSaving() (button *widget.Button, buttonMessage *canvas.Text, dirPath *string) { + buttonMessage = canvas.NewText("", color.RGBA{255, 0, 0, 255}) + buttonMessage.TextSize = 16 + buttonMessage.TextStyle = fyne.TextStyle{Bold: true} + + path := "" + dirPath = &path + + button = widget.NewButton("выбрать", func() { + fileDialog := dialog.NewFolderOpen( + func(r fyne.ListableURI, err error) { + if err != nil { + buttonMessage.Text = err.Error() + setStringErrorStyle(buttonMessage) + return + } + if r == nil { + return + } + + path = r.Path() + + buttonMessage.Text = r.Path() + setStringSuccessStyle(buttonMessage) + }, v.w) + fileDialog.Show() + }) + + return +} + func setStringErrorStyle(text *canvas.Text) { text.Color = color.RGBA{255, 0, 0, 255} text.Refresh() @@ -142,5 +196,6 @@ func showConversionMessage(conversionMessage *canvas.Text, err error) { func enableFormConversion(enableFormConversionStruct enableFormConversionStruct) { enableFormConversionStruct.fileVideoForConversion.Enable() + enableFormConversionStruct.buttonForSelectedDir.Enable() enableFormConversionStruct.form.Enable() } diff --git a/src/handler/convertor.go b/src/handler/convertor.go index cd13541..3c3a8e0 100644 --- a/src/handler/convertor.go +++ b/src/handler/convertor.go @@ -3,6 +3,7 @@ package handler import ( "errors" "ffmpegGui/convertor" + "ffmpegGui/helper" "ffmpegGui/setting" "fmt" "fyne.io/fyne/v2/widget" @@ -48,7 +49,7 @@ func (h ConvertorHandler) GetConvertor() { } func (h ConvertorHandler) getSockPath(file *convertor.File, progressbar *widget.ProgressBar) (string, error) { - totalDuration, err := h.getTotalDuration(file) + totalDuration, err := h.convertorService.GetTotalDuration(file) if err != nil { return "", err @@ -102,18 +103,21 @@ func (h ConvertorHandler) getSockPath(file *convertor.File, progressbar *widget. } func (h ConvertorHandler) runConvert(setting convertor.HandleConvertSetting) error { + return h.convertorService.RunConvert( convertor.ConvertSetting{ VideoFileInput: setting.VideoFileInput, - SocketPath: setting.SocketPath, + VideoFileOut: &convertor.File{ + Path: setting.DirectoryForSave + helper.PathSeparator() + setting.VideoFileInput.Name + ".mp4", + Name: setting.VideoFileInput.Name, + Ext: ".mp4", + }, + SocketPath: setting.SocketPath, + OverwriteOutputFiles: setting.OverwriteOutputFiles, }, ) } -func (h ConvertorHandler) getTotalDuration(file *convertor.File) (float64, error) { - return h.convertorService.GetTotalDuration(file) -} - func (h ConvertorHandler) checkingFFPathUtilities() bool { if h.checkingFFPath() == true { return true @@ -121,7 +125,7 @@ func (h ConvertorHandler) checkingFFPathUtilities() bool { var pathsToFF []convertor.FFPathUtilities if runtime.GOOS == "windows" { - pathsToFF = []convertor.FFPathUtilities{{"ffmpeg/bin/ffmpeg.exe", "ffmpeg/bin/ffprobe.exe"}} + pathsToFF = []convertor.FFPathUtilities{{"ffmpeg\\bin\\ffmpeg.exe", "ffmpeg\\bin\\ffprobe.exe"}} } else { pathsToFF = []convertor.FFPathUtilities{{"ffmpeg/bin/ffmpeg", "ffmpeg/bin/ffprobe"}, {"ffmpeg", "ffprobe"}} } @@ -135,9 +139,9 @@ func (h ConvertorHandler) checkingFFPathUtilities() bool { continue } ffmpegEntity := setting.Setting{Code: "ffmpeg", Value: item.FFmpeg} - h.settingRepository.Create(ffmpegEntity) + _, _ = h.settingRepository.Create(ffmpegEntity) ffprobeEntity := setting.Setting{Code: "ffprobe", Value: item.FFprobe} - h.settingRepository.Create(ffprobeEntity) + _, _ = h.settingRepository.Create(ffprobeEntity) return true } @@ -147,18 +151,18 @@ func (h ConvertorHandler) checkingFFPathUtilities() bool { func (h ConvertorHandler) saveSettingFFPath(ffmpegPath string, ffprobePath string) error { ffmpegChecking, _ := h.convertorService.ChangeFFmpegPath(ffmpegPath) if ffmpegChecking == false { - return errors.New("Это не FFmpeg") + return errors.New("это не FFmpeg") } ffprobeChecking, _ := h.convertorService.ChangeFFprobePath(ffprobePath) if ffprobeChecking == false { - return errors.New("Это не FFprobe") + return errors.New("это не FFprobe") } ffmpegEntity := setting.Setting{Code: "ffmpeg", Value: ffmpegPath} - h.settingRepository.Create(ffmpegEntity) + _, _ = h.settingRepository.Create(ffmpegEntity) ffprobeEntity := setting.Setting{Code: "ffprobe", Value: ffprobePath} - h.settingRepository.Create(ffprobeEntity) + _, _ = h.settingRepository.Create(ffprobeEntity) h.GetConvertor() diff --git a/src/helper/helper.go b/src/helper/helper.go new file mode 100644 index 0000000..b98d666 --- /dev/null +++ b/src/helper/helper.go @@ -0,0 +1,10 @@ +package helper + +import "runtime" + +func PathSeparator() string { + if runtime.GOOS == "windows" { + return "\\" + } + return "/" +} diff --git a/src/main.go b/src/main.go index c74719d..d04a154 100644 --- a/src/main.go +++ b/src/main.go @@ -15,17 +15,17 @@ import ( "os" ) -const appVersion string = "0.1.0" +//const appVersion string = "0.1.0" func main() { a := app.New() w := a.NewWindow("GUI FFMpeg!") - w.Resize(fyne.Size{800, 600}) + w.Resize(fyne.Size{Width: 800, Height: 600}) errorView := myError.NewView(w) if canCreateFile("data/database") != true { - errorView.PanicError(errors.New("Не смогли создать файл 'database' в папке 'data'")) + errorView.PanicError(errors.New("не смогли создать файл 'database' в папке 'data'")) w.ShowAndRun() return }