-
Notifications
You must be signed in to change notification settings - Fork 84
/
playlist.go
54 lines (46 loc) · 1.13 KB
/
playlist.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
package hls
import (
"bytes"
"fmt"
"io"
"text/template"
)
func WritePlaylist(urlTemplate string, file string, w io.Writer) error {
t := template.Must(template.New("urlTemplate").Parse(urlTemplate))
vinfo, err := GetVideoInformation(file)
if err != nil {
return err
}
duration := vinfo.Duration
getUrl := func(segmentIndex int) string {
buf := new(bytes.Buffer)
t.Execute(buf, struct {
Resolution int64
Segment int
}{
720,
segmentIndex,
})
return buf.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, "#EXT-X-TARGETDURATION:"+fmt.Sprintf("%.f", hlsSegmentLength)+"\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, getUrl(segmentIndex)+"\n")
segmentIndex++
leftover = leftover - hlsSegmentLength
}
fmt.Fprint(w, "#EXT-X-ENDLIST\n")
return nil
}