-
Notifications
You must be signed in to change notification settings - Fork 130
/
config.go
67 lines (54 loc) · 1.3 KB
/
config.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
package goffmpeg
import (
"context"
"errors"
"runtime"
"strings"
"github.com/xfrr/goffmpeg/pkg/cmd"
)
const (
ffmpegCommand = "ffmpeg"
ffprobeCommand = "ffprobe"
)
type Configuration struct {
ffprobeBinPath string
ffmpegBinPath string
}
func (cfg Configuration) FFmpegBinPath() string {
return cfg.ffmpegBinPath
}
func (cfg Configuration) FFprobeBinPath() string {
return cfg.ffprobeBinPath
}
func Configure(ctx context.Context) (Configuration, error) {
ffmpegBin, err := cmd.FindBinPath(ctx, ffmpegCommand)
if err != nil {
return Configuration{}, err
}
if ffmpegBin == "" {
return Configuration{}, errors.New("ffmpeg not found, please install it before using goffmpeg")
}
ffprobeBin, err := cmd.FindBinPath(ctx, ffprobeCommand)
if err != nil {
return Configuration{}, err
}
if ffprobeBin == "" {
return Configuration{}, errors.New("ffprobe not found, please install it before using goffmpeg")
}
return Configuration{
ffmpegBinPath: normalizeBinPath(ffmpegBin),
ffprobeBinPath: normalizeBinPath(ffprobeBin),
}, nil
}
func normalizeBinPath(binPath string) string {
binPath = strings.ReplaceAll(binPath, lineSeparator(), " ")
return strings.TrimSpace(binPath)
}
func lineSeparator() string {
switch runtime.GOOS {
case "windows":
return "\r\n"
default:
return "\n"
}
}