-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
161 lines (143 loc) · 3.83 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"context"
ffmpeg_go "github.com/u2takey/ffmpeg-go"
"github.com/wailsapp/wails/v2/pkg/runtime"
"log"
"strings"
)
// App struct
type App struct {
ctx context.Context
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved,
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
type MediaConvertOptions struct {
TargetFormat string `json:"target_format"`
TargetBitrate struct {
Video string `json:"video"`
Audio string `json:"audio"`
} `json:"target_bitrate"`
}
// MediaConvert converts the given media file to the given format
func (a *App) MediaConvert(options MediaConvertOptions) error {
fileSource, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
Title: "Select a file",
Filters: []runtime.FileFilter{
{
DisplayName: "Videos",
Pattern: "*.mp4;*.mkv;*.avi;*.mov;*.wmv;*.flv;*.webm",
},
{
DisplayName: "Audio",
Pattern: "*.mp3;*.wav;*.aac;*.flac;*.ogg;*.wma;*.m4a;*.opus",
},
},
})
if err != nil {
runtime.EventsEmit(a.ctx, "failed", err.Error())
return err
}
if fileSource == "" {
runtime.EventsEmit(a.ctx, "failed", "No file selected")
return nil
}
targetDir, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
Title: "Save file...",
Filters: []runtime.FileFilter{
{
DisplayName: options.TargetFormat,
Pattern: "*." + options.TargetFormat,
},
},
DefaultFilename: strings.Split(strings.Split(fileSource, "\\")[len(strings.Split(fileSource, "\\"))-1], ".")[0],
})
if err != nil {
runtime.EventsEmit(a.ctx, "failed", err.Error())
return err
}
if targetDir == "" {
runtime.EventsEmit(a.ctx, "failed", "No target directory selected")
return nil
}
targetDir = targetDir + "." + options.TargetFormat
err = ffmpeg_go.Input(fileSource).
Output(targetDir, ffmpeg_go.KwArgs{
"b:v": options.TargetBitrate.Video,
"b:a": options.TargetBitrate.Audio,
}).
OverWriteOutput().
ErrorToStdOut().
Run()
if err != nil {
runtime.EventsEmit(a.ctx, "failed", err.Error())
return err
}
log.Println("File converted to: " + targetDir)
runtime.EventsEmit(a.ctx, "success", "File converted to: "+targetDir)
return nil
}
type ImageConvertOptions struct {
TargetFormat string `json:"target_format"`
}
func (a *App) ImageConvert(options ImageConvertOptions) error {
fileSource, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
Title: "Select a file",
Filters: []runtime.FileFilter{
{
DisplayName: "Images",
Pattern: "*.jpg;*.jpeg;*.png;*.gif;*.bmp;*.webp",
},
{
DisplayName: "Videos",
Pattern: "*.mp4;*.mkv;*.avi;*.mov;*.wmv;*.flv;*.webm",
},
},
})
if err != nil {
runtime.EventsEmit(a.ctx, "failed", err.Error())
return err
}
if fileSource == "" {
runtime.EventsEmit(a.ctx, "failed", "No file selected")
return nil
}
targetDir, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
Title: "Save file...",
Filters: []runtime.FileFilter{
{
DisplayName: options.TargetFormat,
Pattern: "*." + options.TargetFormat,
},
},
DefaultFilename: strings.Split(strings.Split(fileSource, "\\")[len(strings.Split(fileSource, "\\"))-1], ".")[0],
})
if err != nil {
runtime.EventsEmit(a.ctx, "failed", err.Error())
return err
}
if targetDir == "" {
runtime.EventsEmit(a.ctx, "failed", "No target directory selected")
return nil
}
targetDir = targetDir + "." + options.TargetFormat
err = ffmpeg_go.Input(fileSource).
Output(targetDir).
OverWriteOutput().
ErrorToStdOut().
Run()
if err != nil {
runtime.EventsEmit(a.ctx, "failed", err.Error())
return err
}
log.Println("File converted to: " + targetDir)
runtime.EventsEmit(a.ctx, "success", "File converted to: "+targetDir)
return nil
}