-
Notifications
You must be signed in to change notification settings - Fork 83
/
ytdlp.go
69 lines (59 loc) · 1.12 KB
/
ytdlp.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
package parser
import (
"io"
"os/exec"
"sync"
l "github.com/go-olive/olive/engine/log"
"github.com/sirupsen/logrus"
)
func init() {
SharedManager.Register(
new(ytdlp),
)
}
type ytdlp struct {
cmd *exec.Cmd
cmdStdIn io.WriteCloser
closeOnce sync.Once
stop chan struct{}
}
func (p *ytdlp) New() Parser {
return &ytdlp{
stop: make(chan struct{}),
}
}
func (p *ytdlp) Stop() {
p.closeOnce.Do(func() {
close(p.stop)
})
}
func (p *ytdlp) Type() string {
return "yt-dlp"
}
// yt-dlp -f "bv[height=1080]+ba/b" https://www.youtube.com/watch?v=f6PdkucL1hk
func (p *ytdlp) Parse(streamURL string, out string) (err error) {
l.Logger.WithFields(logrus.Fields{
// "streamURL": streamURL,
"out": out,
}).Debug("yt-dlp working")
p.cmd = exec.Command(
"yt-dlp",
"-o", out,
"-f", "bv[height=1080]+ba/b",
streamURL,
)
// s.cmd.Stderr = os.Stderr
if p.cmdStdIn, err = p.cmd.StdinPipe(); err != nil {
return err
}
if err = p.cmd.Start(); err != nil {
p.cmd.Process.Kill()
return err
}
go func() {
<-p.stop
// p.cmdStdIn.Write([]byte("\x03"))
p.cmd.Process.Kill()
}()
return p.cmd.Wait()
}