-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamdisplay.go
More file actions
335 lines (327 loc) · 9.56 KB
/
streamdisplay.go
File metadata and controls
335 lines (327 loc) · 9.56 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main
import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/go-toast/toast"
"github.com/go-vgo/robotgo"
"golang.design/x/hotkey"
)
const (
YOUTUBE_RTMP = "rtmp://x.rtmp.youtube.com/live2"
TWITCH_RTMP = "rtmp://ingest.global-contribute.live-video.net/app"
)
func StreamDisp() {
active_displays := robotgo.DisplaysNum()
displays := []string{"Display 1 (Primary)"}
for i := 2; i < active_displays; i++ {
displays = append(displays, fmt.Sprintf("Display %d", i))
}
var display int
err := survey.AskOne(&survey.Select{
Message: "Select Display",
Options: displays,
}, &display, survey.WithValidator(survey.Required))
if err != nil {
fmt.Print("Some error occurred")
return
}
// _, _, w, h := robotgo.GetDisplayBounds(display)
var service string
err = survey.AskOne(&survey.Select{
Message: "Select Service",
Options: []string{"Youtube", "Twitch", "Twitch (Test Stream)", "Both"},
}, &service, survey.WithValidator(survey.Required))
if err != nil {
fmt.Println("Some error occurred")
return
}
var modkeys []hotkey.Modifier
pressedKeys := map[string]bool{}
for _, key := range config.HotkeyConfig.Modkeys {
pressedKeys[key] = true
}
for _, mod := range []string{"ctrl", "alt", "shift"} {
if pressedKeys[mod] {
switch mod {
case "ctrl":
modkeys = append(modkeys, hotkey.ModCtrl)
case "alt":
modkeys = append(modkeys, hotkey.ModAlt)
case "shift":
modkeys = append(modkeys, hotkey.ModShift)
}
}
}
switch service {
case "Youtube":
key := config.StreamConfig.YoutubeStreamKey
if key == "" {
fmt.Println("Stream key not set.")
err = survey.AskOne(&survey.Password{
Message: "Enter your stream key for YouTube:",
}, &key, survey.WithValidator(survey.Required))
if err != nil {
fmt.Println("Error asking for prompt")
return
}
stream_config := config.StreamConfig
stream_config.YoutubeStreamKey = key
setConfig("stream_config", stream_config)
}
args := []string{
"-filter_complex", fmt.Sprintf("ddagrab=output_idx=%d:framerate=%d:draw_mouse=%d,hwdownload,format=bgra", display, config.RecordingOpts.FPS, ternary(config.RecordingOpts.CaptureMouse, 1, 0)),
"-f", "dshow",
"-i", fmt.Sprintf("audio=%s", config.RecordingOpts.AudioDevice),
"-c:v", "libx264",
"-preset", "veryfast",
"-b:v", "3000k",
"-c:a", "aac",
"-f", "flv",
fmt.Sprintf("%s/%s", YOUTUBE_RTMP, key),
}
cmd := exec.Command(getFfmpegPath(), args...)
stdin, _ := cmd.StdinPipe()
var start time.Time
if err, start = cmd.Start(), time.Now(); err != nil {
fmt.Println("Cannot start ffmpeg")
os.Exit(0)
}
fmt.Printf("Streaming started on youtube. Press %s to stop\n", strings.Join(append(config.HotkeyConfig.Modkeys, config.HotkeyConfig.Finalkey), "+"))
tickStop := make(chan struct{})
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
i := 0
last := []string{"🔴", "⚫"}
go func() {
for {
select {
case <-ticker.C:
fmt.Printf("\r%s Streaming time elapsed: %02d:%02d", last[i], int(time.Since(start).Minutes()), int(time.Since(start).Seconds())%60)
i = (i + 1) % 2
case <-tickStop:
}
}
}()
defer func() {
if r := recover(); r != nil {
fmt.Println("\nUnexpected error:", r)
}
exec.Command("taskkill", "/T", "/F", "/PID", strconv.Itoa(cmd.Process.Pid)).Run()
}()
go func() {
err = cmd.Wait()
if err != nil {
fmt.Println("Error waiting for ffmpeg to exit:", err)
os.Exit(1)
}
}()
hk := hotkey.New(modkeys, keys[config.HotkeyConfig.Finalkey])
err = hk.Register()
if err != nil {
fmt.Println("Error registering hotkey:", err)
return
}
defer hk.Unregister()
keyChan := hk.Keydown()
for range keyChan {
tickStop <- struct{}{}
stdin.Write([]byte("q"))
stdin.Close()
fmt.Println("\nStopping streaming...")
notif := toast.Notification{
AppID: "Captr",
Title: "Streaming stopped",
}
notif.Push()
break
}
fmt.Println("\nStreaming stopped")
case "Twitch", "Twitch (Test Stream)":
key := config.StreamConfig.TwitchStreamKey
if key == "" {
fmt.Println("Stream key not set")
err := survey.AskOne(&survey.Password{
Message: "Enter your stream key for Twitch:",
}, &key, survey.WithValidator(survey.Required))
if err != nil {
fmt.Println("Error asking for prompt")
return
}
stream_config := config.StreamConfig
stream_config.TwitchStreamKey = key
setConfig("stream_config", stream_config)
}
args := []string{
"-filter_complex", fmt.Sprintf("ddagrab=output_idx=%d:framerate=%d:draw_mouse=%d,hwdownload,format=bgra,format=yuv420p", display, config.RecordingOpts.FPS, ternary(config.RecordingOpts.CaptureMouse, 1, 0)),
"-f", "dshow",
"-i", fmt.Sprintf("audio=%s", config.RecordingOpts.AudioDevice),
"-c:v", "libx264",
"-preset", "veryfast",
"-b:v", "3000k",
"-c:a", "aac",
"-pix_fmt", "yuv420p",
"-f", "flv",
fmt.Sprintf("%s/%s", TWITCH_RTMP, ternary(service == "Twitch (Test Stream)", fmt.Sprintf("%s?bandwidthtest=true", key), key)),
}
cmd := exec.Command(getFfmpegPath(), args...)
stdin, _ := cmd.StdinPipe()
var start time.Time
if err, start = cmd.Start(), time.Now(); err != nil {
fmt.Println("Cannot start ffmpeg")
os.Exit(0)
}
fmt.Printf("Streaming started on twitch. Press %s to stop\n", strings.Join(append(config.HotkeyConfig.Modkeys, config.HotkeyConfig.Finalkey), "+"))
tickStop := make(chan struct{})
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
i := 0
last := []string{"🔴", "⚫"}
go func() {
for {
select {
case <-ticker.C:
fmt.Printf("\r%s Streaming time elapsed: %02d:%02d", last[i], int(time.Since(start).Minutes()), int(time.Since(start).Seconds())%60)
i = (i + 1) % 2
case <-tickStop:
}
}
}()
defer func() {
if r := recover(); r != nil {
fmt.Println("\nUnexpected error:", r)
}
exec.Command("taskkill", "/T", "/F", "/PID", strconv.Itoa(cmd.Process.Pid)).Run()
}()
go func() {
err = cmd.Wait()
if err != nil {
fmt.Println("Error waiting for ffmpeg to exit:", err)
os.Exit(1)
}
}()
hk := hotkey.New(modkeys, keys[config.HotkeyConfig.Finalkey])
err = hk.Register()
if err != nil {
fmt.Println("Error registering hotkey:", err)
return
}
defer hk.Unregister()
keyChan := hk.Keydown()
for range keyChan {
tickStop <- struct{}{}
stdin.Write([]byte("q"))
stdin.Close()
fmt.Println("\nStopping streaming...")
notif := toast.Notification{
AppID: "Captr",
Title: "Streaming stopped",
}
notif.Push()
break
}
fmt.Println("\nStreaming stopped")
case "Both":
twitchKey := config.StreamConfig.TwitchStreamKey
ytKey := config.StreamConfig.YoutubeStreamKey
if twitchKey == "" {
fmt.Println("Twitch stream key not set")
err := survey.AskOne(&survey.Password{
Message: "Enter your stream key for Twitch:",
}, &twitchKey, survey.WithValidator(survey.Required))
if err != nil {
fmt.Println("Error asking for prompt")
return
}
stream_config := config.StreamConfig
stream_config.TwitchStreamKey = twitchKey
setConfig("stream_config", stream_config)
}
if ytKey == "" {
fmt.Println("YouTube stream key not set")
err := survey.AskOne(&survey.Password{
Message: "Enter your stream key for YouTube: ",
}, &ytKey, survey.WithValidator(survey.Required))
if err != nil {
fmt.Println("Error asking for prompt")
return
}
stream_config := config.StreamConfig
stream_config.YoutubeStreamKey = ytKey
setConfig("stream_config", stream_config)
}
args := []string{
"-filter_complex", fmt.Sprintf("ddagrab=output_idx=%d:framerate=%d:draw_mouse=%d,hwdownload,format=bgra", display, config.RecordingOpts.FPS, ternary(config.RecordingOpts.CaptureMouse, 1, 0)),
"-f", "dshow",
"-i", fmt.Sprintf("audio=%s", config.RecordingOpts.AudioDevice),
"-c:v", "libx264",
"-preset", "veryfast",
"-b:v", "3000k",
"-c:a", "aac",
"-pix_fmt", "yuv420p",
"-f", "flv",
fmt.Sprintf("tee:%s/%s|%s/%s?bandwidthtest=true", YOUTUBE_RTMP, ytKey, TWITCH_RTMP, twitchKey),
}
cmd := exec.Command(getFfmpegPath(), args...)
stdin, _ := cmd.StdinPipe()
var start time.Time
if err, start = cmd.Start(), time.Now(); err != nil {
fmt.Println("Cannot start ffmpeg")
os.Exit(0)
}
fmt.Printf("Streaming started. Press %s to stop\n", strings.Join(append(config.HotkeyConfig.Modkeys, config.HotkeyConfig.Finalkey), "+"))
tickStop := make(chan struct{})
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
i := 0
last := []string{"🔴", "⚫"}
go func() {
for {
select {
case <-ticker.C:
fmt.Printf("\r%s Streaming time elapsed: %02d:%02d", last[i], int(time.Since(start).Minutes()), int(time.Since(start).Seconds())%60)
i = (i + 1) % 2
case <-tickStop:
}
}
}()
defer func() {
if r := recover(); r != nil {
fmt.Println("\nUnexpected error:", r)
}
exec.Command("taskkill", "/T", "/F", "/PID", strconv.Itoa(cmd.Process.Pid)).Run()
}()
go func() {
err = cmd.Wait()
if err != nil {
fmt.Println("Error waiting for ffmpeg to exit:", err)
os.Exit(1)
}
}()
hk := hotkey.New(modkeys, keys[config.HotkeyConfig.Finalkey])
err = hk.Register()
if err != nil {
fmt.Println("Error registering hotkey:", err)
return
}
defer hk.Unregister()
keyChan := hk.Keydown()
for range keyChan {
tickStop <- struct{}{}
stdin.Write([]byte("q"))
stdin.Close()
fmt.Println("\nStopping streaming...")
notif := toast.Notification{
AppID: "Captr",
Title: "Streaming stopped",
}
notif.Push()
break
}
fmt.Println("\nStreaming stopped")
}
}