-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
497 lines (434 loc) · 15.6 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"time"
"runtime"
"github.com/k1nho/gahara/internal/timeline"
"github.com/k1nho/gahara/internal/video"
"github.com/wailsapp/wails/v2/pkg/menu"
"github.com/wailsapp/wails/v2/pkg/menu/keys"
wruntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
const (
Success = "success"
Failed = "failed"
)
type Config struct {
// GaharaDir: the workspace directory for gahara
GaharaDir string `json:"gaharadir"`
//ProjectDir: the project directory for a video editing project
ProjectDir string `json:"projectdir,omitempty"`
}
// App struct
type App struct {
// ctx: app context
ctx context.Context
// config: gahara configuration
config Config
// Timeline: the timeline of the project (NODE_VIDEO0, NODE_AUDIO, NODE_PLACEHOLDER, etc)
Timeline timeline.Timeline
// FFmpegPath: the configured ffmpeg on build
FFmpegPath string
// TrackDuration: the duration of the track being encoded
TrackDuration int
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{Timeline: timeline.NewTimeline()}
}
// 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
err := a.gaharaSetup()
if err != nil {
wruntime.LogFatal(a.ctx, err.Error())
}
FFmpegPath, err := ExtractFFmpeg()
if err != nil {
wruntime.LogFatal(a.ctx, fmt.Sprintf("could not initialize FFmpeg: %s", err.Error()))
}
a.FFmpegPath = FFmpegPath
wruntime.LogInfo(a.ctx, fmt.Sprintf("initialized FFmpeg at %s", a.FFmpegPath))
}
func (a *App) cleanup(ctx context.Context) {
_, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
err := os.RemoveAll(filepath.Dir(a.FFmpegPath))
if err != nil {
wruntime.LogError(a.ctx, "could not cleanup FFmpeg")
}
wruntime.LogInfo(a.ctx, "FFmpeg was cleaned")
}
// FilePicker: opens the native file picker for the user
// this spawns a proxy file creation, if the file is valid
func (a *App) FilePicker() error {
fileFilter := wruntime.FileFilter{
DisplayName: "Video Files(*.mov, *.mp4, *.mkv)",
Pattern: "*.mov;*.mp4;*.mkv;*.avi;*.wmv;*.webm;*.avchd",
}
openDialogOpts := wruntime.OpenDialogOptions{
Title: "Select File",
Filters: []wruntime.FileFilter{fileFilter},
}
filepath, err := wruntime.OpenFileDialog(a.ctx, openDialogOpts)
if err != nil {
wruntime.LogError(a.ctx, err.Error())
return err
}
go a.createProxyFile(filepath)
return nil
}
func (a *App) AddTrack() {
a.Timeline.AddTrack()
}
func (a *App) RemoveTrack(tid int) error {
return a.Timeline.RemoveTrack(tid)
}
func (a *App) OpenFile(filepath string) error {
var cmd *exec.Cmd
switch runtime.GOOS {
case "windows":
cmd = exec.Command("explorer", "/select", filepath)
case "darwin":
cmd = exec.Command("open", "-R", filepath)
case "linux":
cmd = exec.Command("xdg-open", filepath)
default:
return fmt.Errorf("unsupported platform")
}
err := cmd.Run()
if err != nil {
return fmt.Errorf("failed to open file")
}
return nil
}
// createWorkspace: creates a workspace directory to store the video projects of a user locally
func (a *App) createWorkspace() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
wruntime.LogFatal(a.ctx, "user home directory does not exists, cannot create workspace for Gahara")
return "", fmt.Errorf("user home directory does not exists, cannot create workspace for Gahara")
}
gaharaDir := path.Join(homeDir, ".gahara")
_, err = os.Stat(gaharaDir)
// check if the gahara directory does not exists
if os.IsNotExist(err) {
if err := os.MkdirAll(gaharaDir, os.ModePerm); err != nil {
wruntime.LogError(a.ctx, err.Error())
return "", err
}
wruntime.LogInfo(a.ctx, "Gahara workspace has been created!")
} else if err != nil {
wruntime.LogError(a.ctx, "could not create gahara workspace")
return "", err
}
return gaharaDir, nil
}
// createProjectWorkspace: creates a project directory to store the videos related to a project locally
func (a *App) CreateProjectWorkspace(projectName string) (string, error) {
// create project workspace
projectDir := path.Join(a.config.GaharaDir, projectName)
file, err := os.Stat(projectDir)
if os.IsNotExist(err) {
if err := os.MkdirAll(projectDir, os.ModePerm); err != nil {
wruntime.LogError(a.ctx, err.Error())
return Failed, err
}
wruntime.LogInfo(a.ctx, fmt.Sprintf("project %s workspace has been created", projectName))
} else if err != nil {
wruntime.LogError(a.ctx, fmt.Sprintf("could not create the %s workspace\n", projectName))
return Failed, err
}
if file != nil {
wruntime.LogError(a.ctx, fmt.Sprintf("project name (%s) already exists in gahara workspace", projectName))
return Failed, fmt.Errorf("project name (%s) already exists in gahara workspace", projectName)
}
a.config.ProjectDir = projectDir
return Success, nil
}
// SetProjectDirectory: sets the project directory (used with loading projects)
func (a *App) SetProjectDirectory(projectDir string) {
a.config.ProjectDir = path.Join(a.config.GaharaDir, projectDir)
}
// ReadGaharaWorkspace: retrieve all the project workspaces
func (a *App) ReadGaharaWorkspace() ([]string, error) {
gaharaDir, err := os.Open(a.config.GaharaDir)
if err != nil {
wruntime.LogError(a.ctx, "could not read the gahara workspace")
return nil, err
}
defer gaharaDir.Close()
projects, err := gaharaDir.Readdir(0)
if err != nil {
wruntime.LogError(a.ctx, "could not retrieve projects in the gahara workspace")
return nil, err
}
projectsDirectories := []string{}
for _, project := range projects {
if project.IsDir() {
projectsDirectories = append(projectsDirectories, project.Name())
}
}
if len(projectsDirectories) <= 0 {
wruntime.LogError(a.ctx, "Gahara workspace exists, but no project workspace was found")
return nil, fmt.Errorf("gahara workspace exists, but no project workspace was found")
}
wruntime.LogInfo(a.ctx, "project directories loaded successfully")
return projectsDirectories, nil
}
// ReadProjectWorkspace: retrieve all the files in the project workspace
func (a *App) ReadProjectWorkspace() ([]Video, error) {
projectDir, err := os.Open(a.config.ProjectDir)
if err != nil {
wruntime.LogError(a.ctx, "could not read the gahara workspace")
return nil, err
}
defer projectDir.Close()
files, err := projectDir.Readdir(0)
if err != nil {
wruntime.LogError(a.ctx, "could not retrieve projects in the gahara workspace")
return nil, err
}
projectFiles := []Video{}
for _, project := range files {
if !project.IsDir() {
if !video.IsValidExtension(filepath.Ext(project.Name())) {
continue
}
duration, err := getVideoDuration(a.FFmpegPath, video.ProcessingOpts{
Filename: strings.Split(project.Name(), ".")[0],
VideoFormat: filepath.Ext(project.Name()),
InputPath: a.config.ProjectDir,
})
if err != nil {
wruntime.LogError(a.ctx, fmt.Sprintf("could not check video duration: %s", err.Error()))
continue
}
projectFiles = append(projectFiles, Video{Name: strings.Split(project.Name(), ".")[0], Extension: filepath.Ext(project.Name()), FilePath: a.config.ProjectDir, Duration: duration})
}
}
if len(projectFiles) <= 0 {
wruntime.LogError(a.ctx, "Project workspace exists, but no files were found")
return nil, fmt.Errorf("project workspace exists, but no files were found")
}
wruntime.LogInfo(a.ctx, "project files loaded successfully")
return projectFiles, nil
}
// DeleteProject: deletes a video project and all of its related files
func (a *App) DeleteProject(name string) error {
path := path.Join(a.config.GaharaDir, name)
err := os.RemoveAll(path)
if err != nil {
return fmt.Errorf("could not delete the project")
}
return nil
}
// DeleteProjectFile: delete a project file (root id form)
func (a *App) DeleteProjectFile(rid string) error {
_, err := os.Stat(rid)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("file %s does not exists", video.GetFilename(rid))
}
return err
}
err = os.Remove(rid)
if err != nil {
return err
}
return nil
}
func (a *App) AppMenu(menuItems ...menu.MenuItem) *menu.Menu {
return menu.NewMenuFromItems(menu.AppMenu(), menu.EditMenu(), menu.WindowMenu())
}
// SetDefaultAppMenu: it sets the default menu
func (a *App) SetDefaultAppMenu() {
wruntime.MenuSetApplicationMenu(a.ctx, a.AppMenu())
wruntime.MenuUpdateApplicationMenu(a.ctx)
}
// EnableExportMenus: it enables the menus for the export layout view
func (a *App) EnableExportMenus() {
exportMenu := menu.NewMenu()
exportMenu.AddText("Back to Project", keys.Shift("b"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_CHANGE_ROUTE, "video")
})
appMenu := a.AppMenu()
appMenu.Items = append(appMenu.Items, &menu.MenuItem{
Label: "Export",
SubMenu: exportMenu,
})
wruntime.MenuSetApplicationMenu(a.ctx, appMenu)
wruntime.MenuUpdateApplicationMenu(a.ctx)
}
// EnableVideoMenus: It enables the menus for the video layout view
func (a *App) EnableVideoMenus() {
timelineMenu := menu.NewMenu()
timelineMenu.AddText("Open File", keys.CmdOrCtrl("o"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_UPLOAD_FILE)
})
timelineMenu.AddText("Play Track", keys.Shift("space"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_PLAY_TRACK)
})
timelineMenu.AddText("Save Timeline", keys.CmdOrCtrl("s"), func(cd *menu.CallbackData) {
err := a.SaveTimeline()
if err != nil {
wruntime.LogError(a.ctx, "could not save video")
}
wruntime.EventsEmit(a.ctx, video.EVT_SAVED_TIMELINE, "-- SAVED --")
})
timelineMenu.AddText("Rename Clip", keys.CmdOrCtrl("r"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_OPEN_RENAME_CLIP_MODAL)
})
timelineMenu.AddText("Mark/Unmark Clip (Lossless Export)", keys.Key("m"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_TOGGLE_LOSSLESS)
})
timelineMenu.AddText("Mark All Clips (Lossless Export)", keys.Shift("m"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_MARK_ALL_LOSSLESS)
})
timelineMenu.AddText("Unmark All Clips (Lossless Export)", keys.Shift("u"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_UNMARK_ALL_LOSSLESS)
})
timelineMenu.AddText("Change Project", keys.Shift("b"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_CHANGE_ROUTE, "main")
})
timelineMenu.AddText("Export Project", keys.Shift("e"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_CHANGE_ROUTE, "export")
})
timelineMenu.AddText("Toggle Vim Mode", keys.CmdOrCtrl("i"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_TOGGLE_VIM_MODE)
wruntime.EventsEmit(a.ctx, video.EVT_CLIP_MOVE, 0)
})
vimCommandsMenu := timelineMenu.AddSubmenu("Vim Commands")
vimCommandsMenu.AddText("Normal Mode", keys.Key("i"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_CHANGE_VIM_MODE, "select")
})
vimCommandsMenu.AddText("Delete Mode", keys.Key("d"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_CHANGE_VIM_MODE, "remove")
})
vimCommandsMenu.AddText("Timeline Mode", keys.Key("t"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_CHANGE_VIM_MODE, "timeline")
})
vimCommandsMenu.AddText("Split clip", keys.Key("x"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_SPLITCLIP_EDIT)
})
vimCommandsMenu.AddText("Yank clip", keys.Key("y"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_YANK_CLIP)
})
vimCommandsMenu.AddText("Paste clip", keys.Key("p"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_INSERTCLIP_EDIT)
})
vimCommandsMenu.AddText("Execute Edit", keys.Key("enter"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_EXECUTE_EDIT)
})
vimCommandsMenu.AddText("Move Down Track", keys.Key("j"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_TRACK_MOVE, 1)
})
vimCommandsMenu.AddText("Move Up Track", keys.Key("k"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_TRACK_MOVE, -1)
})
vimCommandsMenu.AddText("Move Track Left", keys.Key("h"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_CLIP_MOVE, -1)
})
vimCommandsMenu.AddText("Move Track Right", keys.Key("l"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_CLIP_MOVE, 1)
})
vimCommandsMenu.AddText("Move to Beginning of Track", keys.Key("0"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_CLIP_MOVE, -len(a.Timeline.Nodes[0]))
})
vimCommandsMenu.AddText("Move to End of Track", keys.Key("$"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_CLIP_MOVE, len(a.Timeline.Nodes[0]))
})
vimCommandsMenu.AddText("Zoom In Timeline", keys.Shift("+"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_ZOOM_TIMELINE, "in")
})
vimCommandsMenu.AddText("Zoom Out Timeline", keys.Shift("-"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_ZOOM_TIMELINE, "out")
})
vimCommandsMenu.AddText("Add Timeline Track", keys.Shift("t"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_ADD_TRACK)
})
vimCommandsMenu.AddText("Remove Timeline Track", keys.Shift("d"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_REMOVE_TRACK)
})
vimCommandsMenu.AddText("Save Timeline", keys.Shift("w"), func(cd *menu.CallbackData) {
err := a.SaveTimeline()
if err != nil {
wruntime.LogError(a.ctx, "could not save video")
}
wruntime.EventsEmit(a.ctx, video.EVT_SAVED_TIMELINE, "-- SAVED --")
})
vimCommandsMenu.AddText("Open Search List", keys.Key("/"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_OPEN_SEARCH_LIST)
})
vimCommandsMenu.AddText("Search Timeline Clip", keys.Shift("f"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_SEARCH_TIMELINE_CLIP)
})
vimCommandsMenu.AddText("Insert Placeholder Clip", keys.Shift("p"), func(cd *menu.CallbackData) {
wruntime.EventsEmit(a.ctx, video.EVT_SEARCH_PLACEHOLDER)
})
appMenu := a.AppMenu()
appMenu.Items = append(appMenu.Items, &menu.MenuItem{
Label: "Timeline",
SubMenu: timelineMenu,
})
wruntime.MenuSetApplicationMenu(a.ctx, appMenu)
wruntime.MenuUpdateApplicationMenu(a.ctx)
}
// GaharaSetup: setup of gahara on startup (workspace and config.json)
func (a *App) gaharaSetup() error {
gaharaDir, err := a.createWorkspace()
if err != nil {
return err
}
wruntime.LogInfo(a.ctx, "Gahara workspace has been found!")
// config.json
configPath := path.Join(gaharaDir, "config.json")
// check that exists first
_, err = os.Stat(configPath)
if os.IsNotExist(err) {
// create config file
file, err := os.Create(configPath)
if err != nil {
return err
}
defer file.Close()
gaharaConfig := Config{
GaharaDir: gaharaDir,
}
bytes, err := json.MarshalIndent(gaharaConfig, "", "\t")
if err != nil {
wruntime.LogError(a.ctx, "could not marshal Config struct")
return err
}
_, err = file.Write(bytes)
if err != nil {
wruntime.LogError(a.ctx, "could not write bytes into json file")
return err
}
wruntime.LogInfo(a.ctx, "config.json file for gahara has been created!")
} else if err != nil {
wruntime.LogError(a.ctx, fmt.Sprintf("could not setup gahara: %s\n", err.Error()))
return err
}
// the file exists, read it into the struct
bytes, err := os.ReadFile(configPath)
if err != nil {
wruntime.LogError(a.ctx, "could not read the config file path")
return err
}
err = json.Unmarshal(bytes, &a.config)
if err != nil {
wruntime.LogError(a.ctx, "could not unmarshal the config")
return err
}
wruntime.LogInfo(a.ctx, "config.json file has been found!")
return nil
}