-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
180 lines (156 loc) · 5.11 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
"github.com/go-pkgz/lgr"
"github.com/umputun/go-flags"
)
var opts struct {
CheckURL string `long:"check-url" env:"CHECK_URL" default:"http://icecast:8000/status-json.xsl" description:"URL to check the stream status"`
CheckInterval time.Duration `long:"check-interval" env:"CHECK_INTERVAL" default:"60s" description:"Interval for status checks"`
CheckTimeout time.Duration `long:"check-timeout" env:"CHECK_TIMEOUT" default:"5s" description:"Timeout for status check"`
StreamURL string `long:"stream-url" env:"STREAM_URL" default:"https://stream.radio-t.com" description:"Source stream URL"`
FfmpegPath string `long:"ffmpeg-path" env:"FFMPEG_PATH" default:"/usr/bin/ffmpeg" description:"Path to ffmpeg binary"`
SkipCheck bool `long:"skip-check" env:"SKIP_CHECK" description:"Disable status check"`
TGServer string `long:"tg-server" env:"TG_SERVER" default:"dc4-1.rtmp.t.me" description:"Telegram server"`
TGKey string `long:"tg-key" env:"TG_KEY" required:"true" description:"Telegram stream key"`
Debug bool `long:"debug" env:"DEBUG" description:"Enable debug mode"`
}
var revision = "unknown"
func main() {
fmt.Printf("tg-retrans, %s\n", revision)
if _, err := flags.Parse(&opts); err != nil {
log.Printf("[ERROR] failed to parse flags: %v", err)
os.Exit(2)
}
setupLog(opts.Debug)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if err := run(ctx); err != nil {
log.Fatalf("[ERROR] run failed: %v", err)
}
log.Printf("[INFO] completed")
}
// run is the main function that starts the retranslation process
// it is one-shot if SkipCheck is set, otherwise it runs in a loop with a check interval
func run(ctx context.Context) error {
if opts.SkipCheck {
if err := startRetrans(ctx); err != nil {
return fmt.Errorf("failed to start retranslation: %w", err)
}
if !checkStreamStatus(ctx) {
return fmt.Errorf("stream is not available")
}
return nil
}
for {
// cancel the context if the parent is done
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if checkStreamStatus(ctx) {
log.Print("[INFO] Stream is available, start retranslation")
if err := startRetrans(ctx); err != nil {
log.Printf("[WARN] failed to start retranslation: %v", err)
}
} else {
log.Printf("[DEBUG] Not streaming, next check in %v", opts.CheckInterval)
}
time.Sleep(opts.CheckInterval)
}
}
// checkStreamStatus checks if the stream is available
func checkStreamStatus(ctx context.Context) bool {
log.Printf("[DEBUG] Checking stream with %s", opts.CheckURL)
client := http.Client{Timeout: opts.CheckTimeout}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, opts.CheckURL, http.NoBody)
if err != nil {
log.Printf("[WARN] Can't make request to %s: %v", opts.CheckURL, err)
return false
}
resp, err := client.Do(req)
if err != nil {
log.Printf("[WARN] Can't get response from %s: %v", opts.CheckURL, err)
return false
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("[DEBUG] Invalid status code for %s: %d", opts.CheckURL, resp.StatusCode)
return false
}
data := map[string]any{}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
log.Printf("[WARN] Failed to decode response: %v", err)
return false
}
icestats, ok := data["icestats"].(map[string]any)
if !ok {
log.Printf("[WARN] Missing icestats key in response")
return false
}
if sources, ok := icestats["source"]; !ok || sources == nil {
log.Printf("[WARN] Missing or empty source in icestats response")
return false
}
log.Printf("[DEBUG] Status check passed")
return true
}
func startRetrans(ctx context.Context) error {
// spawnFFmpeg creates and runs ffmpeg process
spawnFFmpeg := func(destURL string) error {
runOpts := []string{
"-v", "verbose",
"-nostdin",
"-nostats",
"-hide_banner",
"-loop", "1",
"-i", "logo-dark.png",
"-i", opts.StreamURL,
"-c:v", "libx264",
"-tune", "stillimage",
"-pix_fmt", "yuv420p",
"-c:a", "aac",
"-b:a", "128k",
"-ac", "1",
"-ar", "44100",
"-f", "flv",
"-rtmp_live", "-1",
destURL,
}
log.Printf("[DEBUG] Run options: %v", runOpts)
cmd := exec.CommandContext(ctx, opts.FfmpegPath, runOpts...)
if opts.Debug {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
}
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to run ffmpeg: %w", err)
}
return nil
}
destURL := fmt.Sprintf("rtmps://%s/s/%s", opts.TGServer, opts.TGKey)
log.Printf("[INFO] Start retranslation from %s to %s", opts.StreamURL, destURL)
start := time.Now()
if err := spawnFFmpeg(destURL); err != nil {
return fmt.Errorf("failed to start retranslation: %w", err)
}
log.Printf("[INFO] End retranslation in %v", time.Since(start))
return nil
}
func setupLog(dbg bool) {
logOpts := []lgr.Option{lgr.Msec, lgr.LevelBraces, lgr.StackTraceOnError}
if dbg {
logOpts = []lgr.Option{lgr.Debug, lgr.CallerFile, lgr.CallerFunc, lgr.Msec, lgr.LevelBraces, lgr.StackTraceOnError}
}
lgr.SetupStdLogger(logOpts...)
}