-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
134 lines (116 loc) · 2.57 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
package main
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
var (
logPath = os.Getenv("LOG_PATH")
)
var rtmpFileFlag = &cli.StringFlag{
Name: "rtmp",
Aliases: []string{"r"},
Usage: "Load configuration from `FILE`",
}
var videoFileFlag = &cli.StringFlag{
Name: "video",
Aliases: []string{"v"},
Usage: "Load configuration from `FILE`",
}
func main() {
setupLogger()
var rtmpStreams int
app := &cli.App{
Flags: []cli.Flag{rtmpFileFlag, videoFileFlag},
Action: func(c *cli.Context) error {
rtmpFilePath := c.String(rtmpFileFlag.Name)
videoFilePath := c.String(videoFileFlag.Name)
if rtmpFilePath == "" || videoFilePath == "" {
return fmt.Errorf("invalid arg")
}
rangeFileLine(rtmpFilePath, func(rtmpAddr string) {
if rtmpAddr == "" {
return
}
go pushStream(context.TODO(), rtmpAddr, videoFilePath)
rtmpStreams++
time.Sleep(time.Second)
})
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.WithError(err).Fatalf("run error")
}
if rtmpStreams <= 0 {
log.Infof("no rtmp stream to push")
return
}
// 监听系统退出信号
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
s := <-signalChan
log.Infof("quit with %s", s.String())
}
func pushStream(ctx context.Context, pushAddr string, pushFile string) error {
log.Infof("start pushing %s: %s", pushFile, pushAddr)
cmd := exec.CommandContext(ctx,
"ffmpeg",
"-re",
"-stream_loop", "-1",
"-i", pushFile,
"-vcodec", "copy",
"-acodec", "aac",
"-ar", "44100",
"-f", "flv",
pushAddr,
)
errOut := bytes.Buffer{}
cmd.Stdout = &errOut
cmd.Stderr = &errOut
err := cmd.Run()
if err != nil {
log.Errorf("push %s failed: %s, %s", pushAddr, err.Error(), errOut.String())
return err
}
return nil
}
func rangeFileLine(filePath string, f func(line string)) error {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
buf := bufio.NewReader(file)
for {
line, err := buf.ReadString('\n')
line = strings.TrimSpace(line)
f(line)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
}
}
func setupLogger() {
logFilePath := filepath.Join(logPath, "main.log")
file, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
log.WithError(err).Fatalf("open file error: %s")
}
writer := io.MultiWriter(file, os.Stderr)
log.SetOutput(writer)
}