forked from shimberger/gohls
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hls.go
156 lines (142 loc) · 3.36 KB
/
hls.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
package hls
import (
"database/sql"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/admpub/errors"
)
var (
HomeDir = ".gohls"
FFProbePath = "ffprobe"
FFMPEGPath = "ffmpeg"
//ComSkipINI comskip's ini file path
ComSkipINI = ""
ComSkipPath = "comskip"
supportedFFProbe sql.NullBool
supportedFFMPEG sql.NullBool
supportedComSkip sql.NullBool
)
func Reset() {
supportedFFProbe.Valid = false
supportedFFMPEG.Valid = false
supportedComSkip.Valid = false
}
const (
cacheDirName = "cache"
hlsSegmentLength = 5.0 // Seconds
)
func ClearCache() error {
var cacheDir = filepath.Join(HomeDir, cacheDirName)
return os.RemoveAll(cacheDir)
}
func ApplyEnv() {
envFFProbePath := os.Getenv(`FFPROBE_PATH`)
if len(envFFProbePath) > 0 {
FFProbePath = envFFProbePath
}
envFFMPEGPath := os.Getenv(`FFMPEG_PATH`)
if len(envFFMPEGPath) > 0 {
FFMPEGPath = envFFMPEGPath
if len(envFFProbePath) == 0 {
envFFProbePath = filepath.Join(filepath.Dir(envFFMPEGPath), `ffprobe`)
if fi, err := os.Stat(envFFProbePath); err == nil && !fi.IsDir() {
FFProbePath = envFFProbePath
}
}
}
}
func IsUnsupported(err error) bool {
return errors.Cause(err) == ErrUnsupported
}
func IsSupportedFFProbe() bool {
if supportedFFProbe.Valid {
return supportedFFProbe.Bool
}
supportedFFProbe.Valid = true
supportedFFProbe.Bool = false
if _, err := exec.LookPath(FFProbePath); err != nil {
return false
}
supportedFFProbe.Bool = true
return true
}
func IsSupportedFFMPEG() bool {
if supportedFFMPEG.Valid {
return supportedFFMPEG.Bool
}
supportedFFMPEG.Valid = true
supportedFFMPEG.Bool = false
if _, err := exec.LookPath(FFMPEGPath); err != nil {
return false
}
supportedFFMPEG.Bool = true
return true
}
func IsSupportedComSkip() bool {
if supportedComSkip.Valid {
return supportedComSkip.Bool
}
supportedComSkip.Valid = true
supportedComSkip.Bool = false
if len(ComSkipINI) == 0 {
return false
}
if _, err := os.Stat(ComSkipINI); err != nil {
log.Println(err)
return false
}
if _, err := exec.LookPath(ComSkipPath); err != nil {
return false
}
supportedComSkip.Bool = true
return true
}
func ConvertToMP4(videoFile string, outputFile string) error {
if !IsSupportedFFMPEG() {
return errors.WithMessage(ErrUnsupported, "Cannot find "+FFMPEGPath+" executable in path")
}
size := 1
if IsSupportedComSkip() {
size++
} else {
log.Println("Cannot find " + ComSkipPath + " executable in path")
}
ch := make(chan error, size)
go func() {
args := []string{"-i", videoFile, "-acodec", "copy", "-vcodec", "copy", "-y", outputFile}
//ffmpeg -i index.ts -acodec copy -vcodec copy -y index.mp4
log.Println(FFMPEGPath, strings.Join(args, " "))
res, err := execute(FFMPEGPath, args)
if len(res) > 0 {
log.Println(string(res))
}
ch <- err
}()
if size > 1 {
go func() {
args := []string{"-d", "255", "--ini=" + ComSkipINI, "--threads=" + strconv.Itoa(runtime.NumCPU()), "--hwassist", "-t", outputFile}
log.Println(ComSkipPath, strings.Join(args, " "))
res, err := execute(ComSkipPath, args)
if len(res) > 0 {
log.Println(string(res))
}
ch <- err
}()
}
var err error
for i := 0; i < size; i++ {
recvErr := <-ch
if recvErr != nil {
err = recvErr
}
}
if err != nil {
return errors.WithMessage(err, "Some error occurred during encoding or detecting commercials")
}
return err
}