-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
executable file
·68 lines (58 loc) · 1.47 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
package snap
import (
_ "embed"
"net/http"
"os/exec"
"strings"
"go.uber.org/zap"
. "m7s.live/engine/v4"
"m7s.live/engine/v4/config"
"m7s.live/engine/v4/util"
)
//go:embed default.yaml
var defaultYaml DefaultYaml
type SnapSubscriber struct {
Subscriber
}
type SnapConfig struct {
config.Subscribe
config.HTTP
FFmpeg string `desc:"ffmpeg的路径"` // ffmpeg的路径
Path string `desc:"存储路径"` //存储路径
}
func (snap *SnapConfig) OnEvent(event any) {
}
var conf = &SnapConfig{}
var plugin = InstallPlugin(conf, defaultYaml)
func (snap *SnapConfig) ServeHTTP(w http.ResponseWriter, r *http.Request) {
streamPath := strings.TrimPrefix(r.URL.Path, "/")
if r.URL.RawQuery != "" {
streamPath += "?" + r.URL.RawQuery
}
w.Header().Set("Content-Type", "image/jpeg")
sub := &SnapSubscriber{}
sub.ID = r.RemoteAddr
sub.SetParentCtx(r.Context())
sub.SetIO(w)
if err := plugin.SubscribeBlock(streamPath, sub, SUBTYPE_RAW); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
}
func (s *SnapSubscriber) OnEvent(event any) {
switch v := event.(type) {
case VideoFrame:
s.Stop(zap.String("reason", "snap"))
var errOut util.Buffer
firstFrame := v.GetAnnexB()
cmd := exec.Command(conf.FFmpeg, "-hide_banner", "-i", "pipe:0", "-vframes", "1", "-f", "mjpeg", "pipe:1")
cmd.Stdin = &firstFrame
cmd.Stderr = &errOut
cmd.Stdout = s
cmd.Run()
if errOut.CanRead() {
s.Info(string(errOut))
}
default:
s.Subscriber.OnEvent(event)
}
}