-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (90 loc) · 2.5 KB
/
main.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
package main
import (
"github.com/codercms/ffthumbs/examples"
"log"
"os"
"path/filepath"
"sync"
"time"
"github.com/codercms/ffthumbs"
)
// On successful run you will see something like this on command output:
// 2023/08/23 16:58:34 Request 2 processed, time spent: 49.4448109s
// 2023/08/23 16:58:35 Request 1 processed, time spent: 49.9029628s
// 2023/08/23 16:58:35 Background goroutine exit
// 2023/08/23 16:58:35 Done in 49.9029628s
func main() {
thumbsGen, err := ffthumbs.NewGenerator(&ffthumbs.Config{
// Concurrency default value is 2, feel free to set concurrency for your needs
Concurrency: 2,
Outputs: []*ffthumbs.OutputConfig{
{
Scale: ffthumbs.ScaleConfig{
Width: 320,
Height: 180,
Behavior: ffthumbs.ScaleBehaviorFillToKeepAspectRatio,
},
SnapshotInterval: time.Millisecond * 6500,
Type: ffthumbs.OutputTypeThumbs,
},
},
// Feel free do disable ffmpeg progress logs
//DisableProgressLogs: true,
})
if err != nil {
log.Fatal(err)
}
doneChan := make(chan *ffthumbs.GenerateResult, 1)
reqs := []ffthumbs.GenerateRequest{
{
MediaURL: examples.StreamURL,
// Here we override DstPath for output 0 (index of OutputConfig in an outputs array)
OutputDst: map[int]string{
0: "tears_of_steel_1/%04d.jpg",
},
DoneChan: doneChan,
},
{
MediaURL: examples.StreamURL,
OutputDst: map[int]string{
0: "tears_of_steel_2/%04d.jpg",
},
DoneChan: doneChan,
},
}
start := time.Now()
var wg sync.WaitGroup
for _, req := range reqs {
for _, dst := range req.OutputDst {
if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
log.Fatalf("cannot create req dst dir: %v", err)
}
}
// See https://github.com/golang/go/wiki/CommonMistakes
reqCopy := req
if err := thumbsGen.GenerateAsync(&reqCopy); err != nil {
log.Fatalf("Unable to send generate thumbnails request: %v", err)
}
wg.Add(1)
}
bgDoneChan := make(chan struct{})
go func() {
for reqDone := range doneChan {
handleProcessedRequest(&wg, reqDone)
}
log.Println("Background goroutine exit")
bgDoneChan <- struct{}{}
}()
wg.Wait()
close(doneChan)
<-bgDoneChan
log.Printf("Done in %s", time.Since(start))
}
func handleProcessedRequest(wg *sync.WaitGroup, res *ffthumbs.GenerateResult) {
defer wg.Done()
if res.Err != nil {
log.Printf("Request %d failed, time spent: %s, err: %v", res.Req.GetId(), res.Duration, res.Err)
} else {
log.Printf("Request %d processed, time spent: %s", res.Req.GetId(), res.Duration)
}
}