This repository has been archived by the owner on Feb 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (67 loc) · 1.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
package main
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"github.com/otofune/hlsq"
"github.com/otofune/hlsq/ctxdebugfs"
"github.com/otofune/hlsq/ctxlogger"
"github.com/otofune/hlsq/examples/fshandler"
)
func chooseBestOne(va []*hlsq.MediaPlaylist) []*hlsq.MediaPlaylist {
var mp *hlsq.MediaPlaylist
maxBandwidth := uint32(0)
for _, v := range va {
if v != nil {
if v.Bandwidth > maxBandwidth {
maxBandwidth = v.Bandwidth
v := v // copy
mp = v
}
}
}
if mp == nil {
panic("no playlist available")
}
return []*hlsq.MediaPlaylist{mp}
}
func do(playlist *url.URL, dest string) error {
ctx := ctxlogger.WithLogger(context.Background(), ctxlogger.NewStdIOLogger())
debugDest := filepath.Join(dest, "debug")
ctx = ctxdebugfs.WithDebugFS(ctx, ctxdebugfs.NewOSDebugFS(debugDest))
if err := os.MkdirAll(debugDest, 0o755); err != nil {
return err
}
h, err := fshandler.New(http.DefaultClient, dest)
if err != nil {
return err
}
defer h.Close()
ses, err := hlsq.Play(ctx, http.DefaultClient, playlist, chooseBestOne, h)
if err != nil {
return err
}
defer ses.Close()
if err := ses.Wait(); err != nil {
return err
}
return nil
}
func main() {
fmt.Printf("hlsdump %s\n\n", version())
if len(os.Args) != 3 {
panic("You must specify 2 arguments: url, directory")
}
playlist := os.Args[1]
dest := os.Args[2]
playlistURL, err := url.Parse(playlist)
if err != nil {
panic(err)
}
if err := do(playlistURL, dest); err != nil {
panic(err)
}
}