-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
371 lines (323 loc) · 9.31 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
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
package main
import (
"bytes"
"crypto/sha1"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"syscall"
"time"
log "github.com/Sirupsen/logrus"
"github.com/julienschmidt/httprouter"
)
const (
FFMPEGPath = "ffmpeg"
root = "/data/"
HomeDir = ".agentVideo"
cacheDirName = "cache"
hlsSegmentLength = 10.0 // Seconds
)
// hhmmssmsToSeconds converts timecode (HH:MM:SS.MS) to seconds (SS.MS).
func hhmmssmsToSeconds(hhmmssms string) float64 {
var hh, mm, ss, ms float64
var buffer string
length := len(hhmmssms)
timecode := []string{}
for i := length - 1; i >= 0; i-- {
if hhmmssms[i] == '.' {
ms, _ = strconv.ParseFloat(buffer, 64)
buffer = ""
} else if hhmmssms[i] == ':' {
timecode = append(timecode, buffer)
buffer = ""
} else if i == 0 {
if buffer != "" {
timecode = append(timecode, string(hhmmssms[i])+buffer)
} else {
timecode = append(timecode, string(hhmmssms[i]))
}
} else {
buffer = string(hhmmssms[i]) + buffer
}
}
length = len(timecode)
if length == 1 {
ss, _ = strconv.ParseFloat(timecode[0], 64)
} else if length == 2 {
ss, _ = strconv.ParseFloat(timecode[0], 64)
mm, _ = strconv.ParseFloat(timecode[1], 64)
} else if length == 3 {
ss, _ = strconv.ParseFloat(timecode[0], 64)
mm, _ = strconv.ParseFloat(timecode[1], 64)
hh, _ = strconv.ParseFloat(timecode[2], 64)
}
return hh*3600 + mm*60 + ss + ms/100
}
func getVideoDuration(path string) (float64, error) {
con, _ := exec.Command(FFMPEGPath, "-hide_banner", "-i", path).CombinedOutput()
// if err != nil {
// return 0.0, fmt.Errorf("Error starting command: %v", err)
// }
durationRegex, err := regexp.Compile(`.*Duration: (\d{2}\:\d{2}\:\d{2}\.\d{2}),`)
if err != nil {
return 0.0, fmt.Errorf("Get video duration error:%v", err)
}
durationStr := strings.Replace(durationRegex.FindString(string(con)), "Duration:", "", 1)
durationStr = strings.Replace(durationStr, " ", "", -1)
durationStr = strings.Replace(durationStr, ",", "", -1)
return hhmmssmsToSeconds(durationStr), nil
}
func urlEncoded(str string) (string, error) {
u, err := url.Parse(str)
if err != nil {
return "", err
}
return u.String(), nil
}
func execute(cmdPath string, args []string) (data []byte, err error) {
cmd := exec.Command(cmdPath, args...)
stdout, err := cmd.StdoutPipe()
defer stdout.Close()
if err != nil {
err = fmt.Errorf("Error opening stdout of command: %v", err)
return
}
log.Debugf("Executing: %v %v", cmdPath, args)
err = cmd.Start()
if err != nil {
err = fmt.Errorf("Error starting command: %v", err)
return
}
var buffer bytes.Buffer
_, err = io.Copy(&buffer, stdout)
if err != nil {
cmd.Process.Signal(syscall.SIGKILL)
cmd.Process.Wait()
err = fmt.Errorf("Error copying stdout to buffer: %v", err)
return
}
err = cmd.Wait()
if err != nil {
err = fmt.Errorf("Command failed %v", err)
return
}
data = buffer.Bytes()
return
}
type EncodingRequest struct {
file string
segment int64
res int64
data chan *[]byte
err chan error
}
func NewEncodingRequest(file string, segment int64, res int64) *EncodingRequest {
return &EncodingRequest{file, segment, res, make(chan *[]byte, 1), make(chan error, 1)}
}
func NewWarmupEncodingRequest(file string, segment int64, res int64) *EncodingRequest {
return &EncodingRequest{file, segment, res, nil, nil}
}
func (r *EncodingRequest) sendError(err error) {
if r.err != nil {
r.err <- err
}
}
func (r *EncodingRequest) sendData(data *[]byte) {
if r.data != nil {
r.data <- data
}
}
func (r *EncodingRequest) getCacheKey() string {
h := sha1.New()
h.Write([]byte(r.file))
return fmt.Sprintf("%x.%v.%v", h.Sum(nil), r.res, r.segment)
}
type Encoder struct {
cacheDir string
reqChan chan EncodingRequest
}
func NewEncoder(cacheDir string, workerCount int) *Encoder {
rc := make(chan EncodingRequest, 100)
encoder := &Encoder{cacheDir, rc}
go func() {
for {
r := <-rc
cache, err := encoder.GetFromCache(r)
if err != nil {
r.sendError(err)
continue
}
if cache != nil {
r.sendData(&cache)
continue
}
log.Debugf("Encoding %v:%v", r.file, r.segment)
data, err := execute(FFMPEGPath, EncodingArgs(r.file, r.segment, r.res))
if err != nil {
r.err <- err
continue
}
r.sendData(&data)
tmp := encoder.GetCacheFile(r) + ".tmp"
mkerr := os.MkdirAll(filepath.Join(root, HomeDir, encoder.cacheDir), 0777)
if mkerr != nil {
log.Errorf("Could not create cache dir")
continue
}
if err2 := ioutil.WriteFile(tmp, data, 0777); err2 == nil {
os.Rename(tmp, encoder.GetCacheFile(r))
}
}
}()
return encoder
}
func (e *Encoder) GetFromCache(r EncodingRequest) ([]byte, error) {
cachePath := e.GetCacheFile(r)
if _, err := os.Stat(cachePath); err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, fmt.Errorf("Encoder cache file %v could not be opened because: %v", cachePath, err)
}
dat, err := ioutil.ReadFile(cachePath)
if err != nil {
return nil, fmt.Errorf("Encoder could not read cache file %v because: %v", cachePath, err)
}
return dat, nil
}
func (e *Encoder) GetCacheFile(r EncodingRequest) string {
return filepath.Join(root, HomeDir, e.cacheDir, r.getCacheKey())
}
func (e *Encoder) Encode(r EncodingRequest) {
go func() {
log.Debugf("Encoding requested %v:%v", r.file, r.segment)
data, err := e.GetFromCache(r)
if err != nil {
r.sendError(err)
return
}
if data != nil {
r.sendData(&data)
return
}
e.reqChan <- r
e.reqChan <- *NewWarmupEncodingRequest(r.file, r.segment+1, r.res)
e.reqChan <- *NewWarmupEncodingRequest(r.file, r.segment+2, r.res)
}()
}
func EncodingArgs(videoFile string, segment int64, res int64) []string {
startTime := segment * hlsSegmentLength
var (
pressTime int64 = 0
postssTime int64 = 0
//offsetTime int64 = 0
)
if startTime > 0 {
pressTime = startTime - 5
postssTime = 5
//offsetTime = startTime + hlsSegmentLength + 2
}
return []string{
"-y",
"-timelimit", "45",
"-ss", fmt.Sprintf("%v.00", pressTime),
"-i", videoFile,
"-ss", fmt.Sprintf("%v.00", postssTime),
"-t", fmt.Sprintf("%v.00", hlsSegmentLength),
"-vf", fmt.Sprintf("scale=-2:%v", res),
"-vcodec", "libx264",
"-preset", "veryfast",
"-acodec", "libfdk_aac", //"libvo_aacenc",
"-pix_fmt", "yuv420p",
//"-r", "25", // fixed framerate
//"-vsync", "cfr",
"-force_key_frames", fmt.Sprintf("expr:gte(t,n_forced*%v.00)", hlsSegmentLength),
//"-x264opts", "keyint=25:min-keyint=25:scenecut=-1",
"-f", "ssegment",
"-segment_time", fmt.Sprintf("%v.00", hlsSegmentLength),
"-initial_offset", fmt.Sprintf("%v.00", startTime),
"pipe:out%03d.ts",
}
}
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome!\n")
}
func playlist(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
filename := strings.TrimLeft(params.ByName("filename"), "/filename")
log.Debugf("Playlist request: %v,%s", r.URL.Path, filename)
file := path.Join(root, filename)
duration, err := getVideoDuration(file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
id, err := urlEncoded(filename)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header()["Content-Type"] = []string{"application/vnd.apple.mpegurl"}
w.Header()["Access-Control-Allow-Origin"] = []string{"*"}
fmt.Fprint(w, "#EXTM3U\n")
fmt.Fprint(w, "#EXT-X-VERSION:3\n")
fmt.Fprint(w, "#EXT-X-MEDIA-SEQUENCE:0\n")
fmt.Fprint(w, "#EXT-X-ALLOW-CACHE:YES\n")
fmt.Fprint(w, fmt.Sprintf("#EXT-X-TARGETDURATION:%.f\n", hlsSegmentLength))
fmt.Fprint(w, "#EXT-X-DISCONTINUITY\n")
fmt.Fprint(w, "#EXT-X-PLAYLIST-TYPE:VOD\n")
leftover := duration
segmentIndex := 0
for leftover > 0 {
if leftover > hlsSegmentLength {
fmt.Fprintf(w, "#EXTINF:%f,\n", hlsSegmentLength)
} else {
fmt.Fprintf(w, "#EXTINF:%f,\n", leftover)
}
fmt.Fprintf(w, "http://%v/api/hls/segments/%v/%v.ts\n", r.Host, id, segmentIndex)
segmentIndex++
leftover = leftover - hlsSegmentLength
}
fmt.Fprint(w, "#EXT-X-ENDLIST\n")
}
func hls(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
filename := strings.TrimLeft(params.ByName("segments"), "/segments")
log.Debugf("Stream request: %v,%v", r.URL.Path, filename)
var streamRegexp = regexp.MustCompile(`^(.*)/([0-9]+)\.ts$`)
matches := streamRegexp.FindStringSubmatch(filename)
segment, _ := strconv.ParseInt(matches[2], 0, 64)
file := path.Join(root, matches[1])
log.Debugf("Stream request: %v,%v", file, segment)
er := NewEncodingRequest(file, segment, 480)
NewEncoder("segments", 2).Encode(*er)
w.Header()["Access-Control-Allow-Origin"] = []string{"*"}
select {
case data := <-er.data:
w.Write(*data)
case err := <-er.err:
log.Errorf("Error encoding %v", err)
case <-time.After(60 * time.Second):
log.Errorf("Timeout encoding")
}
}
//获得预览图,待开发
func pic(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//filename := strings.Replace(params.ByName("cover"), "/cover/", "", 1)
log.Debugf("Cover request: %v", r.URL.Path)
}
func main() {
router := httprouter.New()
router.GET("/", Index)
router.GET("/api/playlist/*filename", playlist)
router.GET("/api/hls/*segments", hls)
router.GET("/api/pic/*cover", pic)
log.Fatal(http.ListenAndServe(":8001", router))
}