-
Notifications
You must be signed in to change notification settings - Fork 1
/
video.go
726 lines (621 loc) · 20.5 KB
/
video.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/google/uuid"
"github.com/k1nho/gahara/ffmpegbuilder"
"github.com/k1nho/gahara/internal/audio"
"github.com/k1nho/gahara/internal/placeholder"
"github.com/k1nho/gahara/internal/timeline"
"github.com/k1nho/gahara/internal/video"
wruntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
type rawTimeline struct {
Nodes [][]json.RawMessage `json:"timeline"`
}
type Video struct {
// ID: the unique identifier of the video
ID string `json:"id"`
// Name: the name of the video file (includes extension)
Name string `json:"name"`
// Extension: the container type of the video (mp4, avi, etc)
Extension string `json:"extension"`
// FilePath: the absolute path of the video
FilePath string `json:"filepath"`
// Duration: the duration of the video in seconds
Duration float64 `json:"duration"`
}
type Interval struct {
Start float64 `json:"start"`
End float64 `json:"end"`
}
type VideoProcessingResult struct {
ID string `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
Message string `json:"message"`
}
type MonitoringOpts struct {
terms map[string]bool
}
func NewVideo(name string, extension string, filepath string, duration float64) *Video {
return &Video{
ID: strings.Replace(uuid.New().String(), "-", "", -1),
Name: name,
Extension: extension,
FilePath: filepath,
Duration: duration,
}
}
func NewMonitoringOpts(observeParams ...string) *MonitoringOpts {
terms := make(map[string]bool)
for _, word := range observeParams {
terms[word] = true
}
return &MonitoringOpts{
terms: terms,
}
}
func NewVideoProcessingResult(id string, name string, status string, msg string) *VideoProcessingResult {
if id == "" {
id = strings.Replace(uuid.New().String(), "-", "", -1)
}
return &VideoProcessingResult{
ID: id,
Name: name,
Status: status,
Message: msg,
}
}
// createProxyFile: creates the proxy file to be used for editing (preserve original media)
func (a *App) createProxyFile(inputFilePath string) {
if inputFilePath == "" {
wruntime.EventsEmit(a.ctx, video.EVT_PROXY_ERROR_MSG, "no file selected")
return
}
fileName := video.GetFilename(inputFilePath)
name, ext, err := video.GetNameAndExtension(fileName)
if err != nil {
wruntime.LogError(a.ctx, "invalid file format")
wruntime.EventsEmit(a.ctx, video.EVT_PROXY_ERROR_MSG, "invalid file format")
return
}
if !video.IsValidExtension("." + ext) {
wruntime.LogError(a.ctx, "invalid file extension")
wruntime.EventsEmit(a.ctx, video.EVT_PROXY_ERROR_MSG, "invalid file extension")
return
}
proxyFile := fmt.Sprintf("%s.mov", name)
pathProxyFile := path.Join(a.config.ProjectDir, proxyFile)
// check that a proxy has not already been created for the file
_, err = os.Stat(pathProxyFile)
if os.IsNotExist(err) {
pfile := NewVideo(name, filepath.Ext(proxyFile), a.config.ProjectDir, 0)
cancelDurationListener := wruntime.EventsOnce(a.ctx, video.EVT_DURATION_EXTRACTED, func(duration ...interface{}) {
pfile.Duration = duration[0].(float64)
wruntime.EventsEmit(a.ctx, video.EVT_PROXY_FILE_CREATED, pfile)
})
defer cancelDurationListener()
err := a.FFmpegQuery(video.QUERY_CREATE_PROXY_FILE, video.ProcessingOpts{
Filename: name,
VideoFormat: fmt.Sprintf(".%s", ext),
InputPath: filepath.Dir(inputFilePath),
OutputPath: a.config.ProjectDir,
})
if err != nil {
wruntime.LogError(a.ctx, fmt.Sprintf("could not create the proxy file for %s: %s", inputFilePath, err.Error()))
wruntime.EventsEmit(a.ctx, video.EVT_PROXY_ERROR_MSG, fmt.Sprintf("failed to import %s", fileName))
return
}
wruntime.LogInfo(a.ctx, fmt.Sprintf("proxy file created: %s", fileName))
return
} else if err != nil {
wruntime.LogError(a.ctx, fmt.Sprintf("file finding error: %s", err.Error()))
wruntime.EventsEmit(a.ctx, video.EVT_PROXY_ERROR_MSG, fmt.Sprintf("failed to import %s", fileName))
return
}
wruntime.LogInfo(a.ctx, fmt.Sprintf("proxy file found: %s", fileName))
wruntime.EventsEmit(a.ctx, video.EVT_PROXY_ERROR_MSG, fmt.Sprintf("file is already in project %s", fileName))
}
// GenerateThumbnail: given an input file, generates a single frame that can be used as thumbnail
func (a *App) GenerateThumbnail(inputFilePath string) error {
inputFile, err := os.Stat(inputFilePath)
if err != nil {
return fmt.Errorf("could not find proxy file: %s", err.Error())
}
filename, _, err := video.GetNameAndExtension(inputFile.Name())
if err != nil {
return fmt.Errorf("could not extract name and extension")
}
// check that the thumbnail exists
thumbnailPath := fmt.Sprintf("%s/%s.png", a.config.ProjectDir, filename)
_, err = os.Stat(thumbnailPath)
if err == nil {
wruntime.LogInfo(a.ctx, fmt.Sprintf("thumbnail of video %s already exists", filename))
return nil
}
cmd := video.GenerateEditThumb(inputFilePath, thumbnailPath, video.ThumbnailOpts{})
err = cmd.Run()
if err != nil {
errMsg := fmt.Sprintf("could not generate the thumbnail for file %s: %s", filename, err.Error())
wruntime.LogError(a.ctx, errMsg)
return fmt.Errorf(errMsg)
}
wruntime.LogInfo(a.ctx, fmt.Sprintf("thumbnail for video: %s has been created", filename))
return nil
}
// GetThumbnail: retrieve thumbnail for a given input file
func (a *App) GetThumbnail(inputFilePath string) error {
filename := filepath.Base(inputFilePath)
if filename == "." {
return fmt.Errorf("file %s does not exists", filename)
}
filename = filename + ".png"
file, err := os.Open(filename)
if err != nil {
return fmt.Errorf("could not find thumbnail %s", filename)
}
defer file.Close()
return nil
}
func (a *App) GetProjectThumbnail(projectName string) (string, error) {
thumbnailDir := path.Join(a.config.GaharaDir, projectName)
projectDir, err := os.Open(thumbnailDir)
if err != nil {
wruntime.LogError(a.ctx, "directory does not exists for the project")
return "", err
}
defer projectDir.Close()
files, err := projectDir.ReadDir(0)
if err != nil {
wruntime.LogError(a.ctx, "could not read the files of the project")
return "", err
}
var thumbnailPath string
for _, project := range files {
if !project.IsDir() && filepath.Ext(project.Name()) == ".png" {
thumbnailPath = path.Join(thumbnailDir, project.Name())
break
}
}
if thumbnailPath == "" {
return thumbnailPath, fmt.Errorf("no thumbnail found")
}
return thumbnailPath, nil
}
func (a *App) SaveProjectFiles(projectFiles []Video) error {
data, err := json.MarshalIndent(projectFiles, "", " ")
if err != nil {
return err
}
err = os.WriteFile(path.Join(a.config.ProjectDir, "metadata.json"), data, 0644)
if err != nil {
return err
}
wruntime.LogInfo(a.ctx, "project files have been saved")
return nil
}
// SaveTimeline: save project timeline into the project filesystem
func (a *App) SaveTimeline() error {
if a.Timeline.Nodes == nil && len(a.Timeline.Nodes) <= 0 {
return fmt.Errorf("timeline is empty, could not save timeline")
}
data, err := json.MarshalIndent(a.Timeline, "", " ")
if err != nil {
return err
}
err = os.WriteFile(path.Join(a.config.ProjectDir, "timeline.json"), data, 0644)
if err != nil {
return err
}
wruntime.LogInfo(a.ctx, fmt.Sprintf("%s: timeline has been saved", time.Now().String()))
return nil
}
// LoadTimeline: retrieve saved project timeline, if any, from filesystem
func (a *App) LoadTimeline() (timeline.Timeline, error) {
var timeline timeline.Timeline
timelinePath := path.Join(a.config.ProjectDir, "timeline.json")
if _, err := os.Stat(timelinePath); err != nil {
return timeline, fmt.Errorf("no timeline found for this project")
}
bytes, err := os.ReadFile(timelinePath)
if err != nil {
wruntime.LogError(a.ctx, "could not read the timeline file")
return timeline, fmt.Errorf("could not read timeline file")
}
rawTimeline := rawTimeline{}
err = json.Unmarshal(bytes, &rawTimeline)
if err != nil {
wruntime.LogError(a.ctx, "could not unmarshal the timeline")
return timeline, err
}
var nodeType struct {
Type string `json:"type"`
}
for i, rawTrack := range rawTimeline.Nodes {
a.Timeline.AddTrack()
for j, rawNode := range rawTrack {
if err := json.Unmarshal(rawNode, &nodeType); err != nil {
wruntime.LogError(a.ctx, "could not unmarshal node type")
continue
}
switch nodeType.Type {
case video.NODE_VIDEO:
var videoNode video.VideoNode
if err := json.Unmarshal(rawNode, &videoNode); err != nil {
wruntime.LogError(a.ctx, "could not unmarshal into video node")
continue
}
if _, err := a.Timeline.Insert(i, j, &videoNode); err != nil {
wruntime.LogError(a.ctx, err.Error())
continue
}
case audio.NODE_AUDIO:
var audioNode audio.AudioNode
if err := json.Unmarshal(rawNode, &audioNode); err != nil {
wruntime.LogError(a.ctx, "could not unmarshal into audio node")
continue
}
if _, err := a.Timeline.Insert(i, j, &audioNode); err != nil {
wruntime.LogError(a.ctx, err.Error())
continue
}
default:
var placeholderNode placeholder.PlaceholderNode
if err := json.Unmarshal(rawNode, &placeholderNode); err != nil {
wruntime.LogError(a.ctx, "could not unmarshal into placeholder node")
continue
}
if _, err := a.Timeline.Insert(i, j, &placeholderNode); err != nil {
wruntime.LogError(a.ctx, err.Error())
continue
}
}
}
}
if len(a.Timeline.Nodes) == 0 {
wruntime.LogInfo(a.ctx, "empty timeline")
return timeline, fmt.Errorf("empty timeline")
}
wruntime.LogInfo(a.ctx, "timeline has been loaded!")
return a.GetTimeline(), nil
}
// LoadTimeline: retrieves saved project files, if any, from filesystem
func (a *App) LoadProjectFiles() ([]Video, error) {
var videoFiles []Video
metadataPath := path.Join(a.config.ProjectDir, "metadata.json")
if _, err := os.Stat(metadataPath); err != nil {
return videoFiles, fmt.Errorf("No video files found for this project")
}
bytes, err := os.ReadFile(metadataPath)
if err != nil {
wruntime.LogError(a.ctx, "could not read the video files metadata file")
return videoFiles, fmt.Errorf("could not read timeline file")
}
err = json.Unmarshal(bytes, &videoFiles)
if err != nil {
wruntime.LogError(a.ctx, "could not unmarshal the files")
return videoFiles, err
}
if len(videoFiles) == 0 {
wruntime.LogInfo(a.ctx, "empty video files")
return videoFiles, fmt.Errorf("empty video files")
}
wruntime.LogInfo(a.ctx, "video files loaded")
return videoFiles, nil
}
func (a *App) ExporterVideoNode() video.VideoNode {
return video.VideoNode{}
}
func (a *App) ExporterAudioNode() audio.AudioNode {
return audio.AudioNode{}
}
func (a *App) ExporterPlaceholderNode() placeholder.PlaceholderNode {
return placeholder.PlaceholderNode{}
}
// GetTimeline: returns the video timeline which is composed of video nodes
func (a *App) GetTimeline() timeline.Timeline {
return a.Timeline
}
// InsertInterval: inserts a video node with some interval [a,b]
func (a *App) InsertInterval(tid int, pos int, nodeType string, rid string, name string, start, end float64) (timeline.TimelineNode, error) {
return a.Timeline.Insert(tid, pos, timeline.CreateNode(nodeType, rid, name, start, end))
}
// RemoveInterval: removes a video node with some interval [a,b]
func (a *App) RemoveInterval(tid int, pos int) error {
return a.Timeline.Delete(tid, pos)
}
// SplitInterval: splits a video node with some interval [a,b].
func (a *App) SplitInterval(tid int, pos int, eventType string, start, end float64) ([]timeline.TimelineNode, error) {
return a.Timeline.Split(tid, pos, eventType, start, end)
}
// DeleteRIDReferences: removes all timeline references of a root id
func (a *App) DeleteRIDReferences(rid string) error {
return a.Timeline.DeleteRIDReferences(rid)
}
func (a *App) RenameVideoNode(tid int, pos int, name string) error {
return a.Timeline.RenameVideoNode(tid, pos, name)
}
func (a *App) ToggleLossless(tid, pos int) error {
return a.Timeline.ToggleLossless(tid, pos)
}
func (a *App) MarkAllLossless(tid int) error {
return a.Timeline.MarkAllLossless(tid)
}
func (a *App) UnmarkAllLossless(tid int) error {
return a.Timeline.UnmarkAllLossless(tid)
}
// ResetTimeline: cleanup timeline state in memory
func (a *App) ResetTimeline() {
a.Timeline = timeline.NewTimeline()
}
// GetTrackDuration: retrieves the total video duration of a track
func (a *App) GetTrackDuration(tid int) (float64, error) {
if a.Timeline.Nodes == nil || tid >= len(a.Timeline.Nodes) {
return 0, fmt.Errorf("no timeline exists")
}
duration := 0.0
for _, node := range a.Timeline.Nodes[tid] {
duration += node.End() - node.Start()
}
return duration, nil
}
// GetOutputFileSavePath: retrieves the output path where the resulting video should be saved
func (a *App) GetOutputFileSavePath() (string, error) {
hd, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("could not get the user home directory")
}
saveFilepath, err := wruntime.OpenDirectoryDialog(a.ctx, wruntime.OpenDialogOptions{
DefaultDirectory: path.Join(hd),
Title: "Export video",
ShowHiddenFiles: false,
CanCreateDirectories: true,
TreatPackagesAsDirectories: true,
})
if err != nil {
return "", fmt.Errorf(err.Error())
}
return saveFilepath, nil
}
// FFmpegQuery: produces an asset (video, image) with FFmpeg given a query type, and processing opts
func (a *App) FFmpegQuery(queryType string, userOpts video.ProcessingOpts) error {
defer wruntime.EventsEmit(a.ctx, video.EVT_FFMPEG_EXEC_ENDED)
if userOpts.InputPath == "" {
userOpts.InputPath = a.config.ProjectDir
}
if err := userOpts.ValidateRequiredFields(queryType); err != nil {
return err
}
switch queryType {
case video.QUERY_FILTERGRAPH:
if err := a.queryFiltergraph(userOpts); err != nil {
return err
}
case video.QUERY_LOSSLESS_CUT:
if err := a.queryLosslessCut(userOpts); err != nil {
return err
}
case video.QUERY_CREATE_PROXY_FILE:
if err := a.queryCreateProxyFile(userOpts); err != nil {
return err
}
case video.QUERY_CREATE_THUMBNAIL:
if err := a.queryCreateThumbnail(userOpts); err != nil {
return err
}
default:
return fmt.Errorf("invalid query type (q_filtergraph, q_create_proxy_file, q_create_thumbnail, q_lossless_cut)")
}
return nil
}
// queryFiltergraph: executes a filtergraph query, currently merge clips
func (a *App) queryFiltergraph(userOpts video.ProcessingOpts) error {
query, err := ffmpegbuilder.MergeClipsQuery(a.FFmpegPath, ffmpegbuilder.ExtractVideoNodes(a.Timeline.Nodes[0]), userOpts)
if err != nil {
return err
}
err = a.executeFFmpegQuery(query, NewMonitoringOpts(video.OBV_OUT_TIME_US))
if err != nil {
return err
}
wruntime.EventsEmit(a.ctx, video.EVT_FFMPEG_RESULT, NewVideoProcessingResult("", userOpts.Filename, Success, ffmpegbuilder.GetFullOutputPath(userOpts)))
wruntime.EventsEmit(a.ctx, video.EVT_EXPORT_MSG, fmt.Sprintf("Finished exporting %s%s", userOpts.Filename, userOpts.VideoFormat))
return nil
}
// queryLosslessCut: executes LosslessCut for a batch of video nodes
func (a *App) queryLosslessCut(userOpts video.ProcessingOpts) error {
var (
wg = new(sync.WaitGroup)
msgChannel = make(chan VideoProcessingResult)
)
go func() {
defer close(msgChannel)
for msg := range msgChannel {
wruntime.EventsEmit(a.ctx, video.EVT_FFMPEG_RESULT, msg)
}
}()
videoNodes := ffmpegbuilder.ExtractVideoNodes(a.Timeline.Nodes[0])
for _, videoNode := range videoNodes {
if !videoNode.LosslessExport {
continue
}
wg.Add(1)
go func(vNode video.VideoNode) {
defer wg.Done()
userOpts.Filename = vNode.VideoName
query, err := ffmpegbuilder.LosslessCutQuery(a.FFmpegPath, vNode, userOpts)
if err != nil {
msgChannel <- VideoProcessingResult{ID: vNode.VideoID, Status: Failed, Message: err.Error()}
return
}
err = a.executeFFmpegQuery(query, nil)
if err != nil {
msgChannel <- VideoProcessingResult{ID: vNode.VideoID, Name: vNode.VideoName, Status: Failed, Message: err.Error()}
return
}
msgChannel <- VideoProcessingResult{ID: vNode.VideoID, Name: vNode.VideoName, Status: Success, Message: ffmpegbuilder.GetFullOutputPath(userOpts)}
}(videoNode)
}
wg.Wait()
return nil
}
// queryCreateProxyFile: executes a conversion query for the given video
func (a *App) queryCreateProxyFile(userOpts video.ProcessingOpts) error {
query, err := ffmpegbuilder.CreateProxyFileQuery(a.FFmpegPath, userOpts, ".mov")
if err != nil {
return err
}
err = a.executeFFmpegQuery(query, NewMonitoringOpts(video.OBV_OUT_TIME))
if err != nil {
return err
}
return nil
}
// queryCreateThumbnail: executes a query to generate a thumbnail for a video (picks 1st frame)
func (a *App) queryCreateThumbnail(userOpts video.ProcessingOpts) error {
query, err := ffmpegbuilder.CreateThumbnailQuery(a.FFmpegPath, userOpts, ".png")
if err != nil {
return err
}
err = a.executeFFmpegQuery(query, nil)
if err != nil {
return err
}
return nil
}
// executeFFmpegQuery: executes an ffmpeg query
func (a *App) executeFFmpegQuery(query string, monitoringOpts *MonitoringOpts) error {
// TODO: implement windows
cmd := exec.Command("bash", "-c", query)
stderrPipe, err := cmd.StderrPipe()
if err != nil {
return fmt.Errorf("could not initialize ffmpeg monitoring")
}
err = cmd.Start()
if err != nil {
return fmt.Errorf("could not initialize video export")
}
if monitoringOpts != nil {
go a.monitorFFmpegOuput(stderrPipe, monitoringOpts)
}
err = cmd.Wait()
if err != nil {
return fmt.Errorf("could not export the video")
}
return nil
}
// monitorFFmpegOuput: monitors ffmpeg query progress
func (a *App) monitorFFmpegOuput(FFmpegOut io.ReadCloser, monitoringOpts *MonitoringOpts) {
wruntime.LogInfo(a.ctx, "monitoring FFmpeg query")
a.TrackDuration = 0
scanner := bufio.NewScanner(FFmpegOut)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, video.OBV_OUT_TIME_US) && monitoringOpts.terms[video.OBV_OUT_TIME_US] {
args := strings.Split(line, "=")
timeMicro, err := strconv.Atoi(args[1])
if err != nil {
continue
}
timeSeconds := (timeMicro / 1000000)
if timeSeconds < 0 {
continue
}
// optimize track duration retrieval
if a.TrackDuration == 0 {
total, err := a.GetTrackDuration(0)
if err != nil {
return
}
a.TrackDuration = int(total)
}
wruntime.EventsEmit(a.ctx, video.EVT_ENCODING_PROGRESS, (timeSeconds*100)/a.TrackDuration)
}
if strings.Contains(line, video.OBV_OUT_TIME) && monitoringOpts.terms[video.OBV_OUT_TIME] {
args := strings.Split(line, "=")
if args[0] != video.OBV_OUT_TIME {
continue
}
duration, err := convertHMStoSeconds(args[1])
if err != nil {
// TODO: handle conversion error
continue
}
if duration < video.Epsilon {
continue
}
wruntime.EventsEmit(a.ctx, video.EVT_DURATION_EXTRACTED, duration)
}
}
}
func convertHMStoSeconds(hms string) (float64, error) {
parts := strings.Split(hms, ".")
if len(parts) != 2 {
return 0.0, fmt.Errorf("could not parse decimal part")
}
timeParts := strings.Split(parts[0], ":")
if len(timeParts) != 3 {
return 0.0, fmt.Errorf("could not parse time part")
}
hours, err := strconv.Atoi(timeParts[0])
if err != nil {
return 0.0, fmt.Errorf("could not convert hours to int")
}
minutes, err := strconv.Atoi(timeParts[1])
if err != nil {
return 0.0, fmt.Errorf("could not convert minutes to int")
}
seconds, err := strconv.Atoi(timeParts[2])
if err != nil {
return 0.0, fmt.Errorf("could not convert seconds to int")
}
microseconds, err := strconv.Atoi(parts[1])
if err != nil {
return 0.0, fmt.Errorf("could not convert microseconds to int")
}
totalSeconds := float64(hours*3600+minutes*60+seconds) + float64(microseconds)/1000000
return totalSeconds, nil
}
func getVideoDuration(FFmpegPath string, userOpts video.ProcessingOpts) (float64, error) {
query, err := ffmpegbuilder.CheckVideoDuration(FFmpegPath, userOpts)
if err != nil {
return 0, err
}
cmd := exec.Command("bash", "-c", query)
stderrPipe, err := cmd.StderrPipe()
if err != nil {
return 0, fmt.Errorf("could not initialize ffmpeg monitoring")
}
scanner := bufio.NewScanner(stderrPipe)
err = cmd.Start()
if err != nil {
return 0, fmt.Errorf("could not initialize video duration extraction")
}
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, video.OBV_DURATION) {
hms := strings.Split(strings.TrimSpace(strings.Split(line, ",")[0]), "Duration: ")[1]
duration, err := convertHMStoSeconds(hms)
if err != nil {
continue
}
return duration, nil
}
}
err = cmd.Wait()
if err != nil {
return 0, fmt.Errorf("could not extract duration of the video")
}
return 0, nil
}