-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
523 lines (458 loc) · 13.5 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
package recordsite
import (
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"time"
)
// DEBUG Controls if the program is built just to test this or not
var DEBUG = false
// // Main - called as recordsite.exe test_youtube defranco.mp4 https://www.youtube.com/watch?v=ovpVWuuUgJg tv
// func main() {
// service := os.Args[1]
// name := os.Args[2]
// url := os.Args[3]
// isTv := os.Args[4]
// Test is for testing that this code is working.
func Test() {
service :="test_youtube"
name := "defranco %(title)s.mp4"
url := "https://www.youtube.com/watch?v=ovpVWuuUgJg"
isTv := "tv"
Setup()
Browser("test_youtube", "defranco %(title)s.mp4", "https://www.youtube.com/watch?v=ovpVWuuUgJg", isTv == "tv")
// Browser("test_youtube", "desync test %(title)s.mp4", "https://www.youtube.com/watch?v=ucZl6vQ_8Uo", true)
}
type pyInput struct {
firstPlaybackButton string
errorClass string
titleClass string
fullscreenButton string
seccondPlaybackButton string
profile string
pauseButton string
maxCheckLoop string
sendKeyTarget string
finishedClass string
}
func getPythonFile(service string, isTv bool, url string) pyInput {
input := pyInput{}
input.maxCheckLoop = "10" // default but overwridden by some services
switch service {
case "disney_plus-disney_plus-subscription":
if !fileExists("D:\\dvr\\disney_plus.profile") {
fmt.Println("Plese sign into " + service)
browserLogin("disney_plus", url)
}
input.firstPlaybackButton = ""
input.errorClass = ""
input.fullscreenButton = "fullscreen-icon"
input.seccondPlaybackButton = "play-icon"
input.profile = "D:\\dvr\\disney_plus.profile"
input.pauseButton = "pause-icon"
input.sendKeyTarget = "btm-media-player"
input.finishedClass = "headline"
if isTv {
input.titleClass = "subtitle-field"
} else {
input.titleClass = "title-field"
}
case "imdb_tv-imdb_tv-free":
fmt.Print(" Should be obtained through amazon_buy")
case "amazon_buy-amazon_buy-purchase":
fallthrough
case "hbo-amazon_prime-subscription":
fallthrough
case "amazon_prime-amazon_prime-subscription":
if !fileExists("D:\\dvr\\amazon_prime.profile") {
fmt.Println("Plese sign into " + service)
browserLogin("amazon_prime", url)
}
input.firstPlaybackButton = "dv-dp-node-playback"
input.errorClass = ""
input.fullscreenButton = "fullscreenButton"
input.seccondPlaybackButton = ""
input.profile = "D:\\dvr\\amazon_prime.profile"
input.pauseButton = ""
input.sendKeyTarget = "scalingVideoContainer"
input.finishedClass = ""
if isTv {
input.titleClass = "subtitle"
} else {
input.titleClass = "title"
}
case "hulu_plus-hulu_plus-subscription":
if !fileExists("D:\\dvr\\hulu_plus.profile") {
fmt.Println("Plese sign into " + service)
browserLogin("hulu_plus", url)
}
input.errorClass = "error__heading"
input.fullscreenButton = "controls__view-mode-button"
input.profile = "D:\\dvr\\hulu_plus.profile"
input.pauseButton = "controls__playback-button-paused-icon"
input.sendKeyTarget = ""
input.finishedClass = ""
input.firstPlaybackButton = ""
input.seccondPlaybackButton = "controls__playback-button-playing-icon"
if isTv {
input.titleClass = "metadata-area__third-line"
} else {
input.titleClass = "metadata-area__second-line"
}
case "netflix-netflix-subscription":
if !fileExists("D:\\dvr\\netflix.profile") {
fmt.Println("Plese sign into " + service)
browserLogin("netflix", url)
}
input.firstPlaybackButton = "nf-big-play-pause"
input.errorClass = ""
input.titleClass = "ellipsize-text"
input.fullscreenButton = "button-nfplayerFullscreen"
input.seccondPlaybackButton = "button-nfplayerPlay"
input.profile = "D:\\dvr\\netflix.profile"
input.pauseButton = "button-nfplayerPause"
input.sendKeyTarget = ""
input.finishedClass = "hide-credits"
}
return input
}
func browserLogin(service string, url string) bool {
local := "D:\\dvr"
// start the image
// 4444 is for selenium, and 5900 is for vnc
fmt.Print("[ ] Starting")
id := startDocker(local)
if id == "" {
return false
}
// find ffmpeg's pid
pid, err := executeCaptureOutput("docker", "exec", id, "pidof", "vlc")
if err != nil {
log.Println(err)
}
// kill that pid
err = executeUnhandled("docker", "exec", id, "kill", "-INT", pid)
if err != nil {
log.Println(err)
}
firefox := exec.Command("docker", "exec", id, "firefox", url)
err = firefox.Start()
if err != nil {
cleanup(id)
log.Println(err)
return false
}
fmt.Println("Sign into ", service)
err = executeUnhandled("C:\\Program Files\\RealVNC\\VNC Viewer\\vncviewer.exe", "localhost:5900")
if err != nil {
cleanup(id)
log.Println(err)
return false
}
if err != nil {
cleanup(id)
log.Println(err)
log.Println(service)
log.Println(url)
return false
}
f, err := executeCaptureOutput("docker", "exec", id, "find", "/home/seluser/.mozilla/firefox/", "-name", "*.default-release", "-maxdepth", "1")
if err != nil {
cleanup(id)
log.Println(err)
return false
}
err = executeUnhandled("docker", "exec", id, "cp", "-r", f, "/tmp/recordings/"+service+".profile")
if err != nil {
cleanup(id)
log.Println(err)
return false
}
cleanup(id)
os.Rename(local+"\\output.avi", local+"\\"+service+".mkv")
return true
}
var ffmpegPath = os.Getenv("GOPATH") + "\\src\\github.com\\coldwaterq\\recordsite\\ffmpeg\\bin\\ffmpeg.exe"
// Setup initializes the things that need initialization
func Setup() {
// Update a number of things for automation
execute("docker", "build", "-t", "videorecord", os.Getenv("GOPATH")+"\\src\\github.com\\coldwaterq\\recordsite\\image")
execute("pip", "install", "--upgrade", "selenium")
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
fmt.Println("This is not currently handled correctly, you are going to have to kill the docker container yourself.")
os.Exit(1)
}
}()
}
func startDocker(local string) string {
id, err := executeCaptureOutput("docker", "run", "-v", local+":/tmp/recordings", "-e", "SCREEN_WIDTH=1280", "-e", "SCREEN_HEIGHT=720", "--rm", "-d", "-p", "4444:4444", "-p", "5900:5900", "-v", "/dev/shm:/dev/shm", "videorecord:latest")
//handle the docker error if needed
if err != nil {
log.Println(err)
cleanup(id)
return ""
}
// wait for the conatiner to start
for status := ""; err != nil || status != "true"; status, err = executeCaptureOutput("docker", "inspect", "-f", "{{.State.Running}}", id) {
time.Sleep(time.Second * 5)
}
//start pulseadio
err = executeUnhandled("docker", "exec", id, "pulseaudio", "-D", "--exit-idle-time=-1")
if err != nil {
log.Println(err)
cleanup(id)
return ""
}
// start the virtula sink
err = executeUnhandled("docker", "exec", id, "pacmd", "load-module", "module-virtual-sink", "sink_name=v1")
if err != nil {
log.Println(err)
cleanup(id)
return ""
}
fmt.Print("\r[X\n[ ] Prepping")
vlc := exec.Command("docker", "exec", id, "vlc", "/tmp/watch.mp4", "--no-qt-privacy-ask", "-f", "-R")
err = vlc.Start()
if err != nil {
log.Println(err)
cleanup(id)
return ""
}
time.Sleep(5 * time.Second)
return id
}
func startFfmpeg(id string) *exec.Cmd {
// start ffmpeg
// executeUnhandled can be used to debug the ffmpeg command
// hardcoded delay
ffmpeg := exec.Command("docker", "exec", id, "ffmpeg", "-f", "x11grab", "-draw_mouse", "0", "-thread_queue_size", "256", "-probesize", "10000000", "-s", "1280x720", "-r", "30", "-i", ":99.0", "-f", "alsa", "-thread_queue_size", "512", "-i", "default", "-q:v", "0", "-acodec", "aac", "-ar", "48k", "-vcodec", "mpeg4", "-af", "aresample=async=1", "/tmp/recordings/output.avi")
// ffmpeg := exec.Command("docker", "exec", id, "ffmpeg", "-video_size", "1280x720", "-draw_mouse", "0", "-f", "x11grab", "-r", "60", "-i", ":99.0", "-f", "alsa", "-ac", "1", "-i", "default", "-threads", "0", "-acodec", "aac", "-vcodec", "mpeg4", "-preset", "ultrafast", "-qscale:v", "0", "-pix_fmt", "yuv444p", "/tmp/recordings/output.mkv")
err := ffmpeg.Start()
if err != nil {
log.Println(err)
return nil
}
return ffmpeg
}
func stopFfmpeg(id string, ffmpeg *exec.Cmd) {
// Stopping is now done in the driver, however we still have to wait for ffmpeg to stop.
// // find ffmpeg's pid
// pid, err := executeCaptureOutput("docker", "exec", id, "pidof", "ffmpeg")
// if err != nil {
// log.Println(err)
// }
// // kill that pid
// err = executeUnhandled("docker", "exec", id, "kill", "-INT", pid)
// if err != nil {
// log.Println(err)
// }
ffmpeg.Wait()
}
func fileExists(filename string) bool {
info, _ := os.Stat(filename)
if info != nil {
return info.IsDir()
}
return false
}
// Supported returns true if recording should work and false if it shouldn't
func Supported(service string, isTv bool, url string) bool {
return getPythonFile(service, isTv, url).profile != ""
}
func watchShow(url string, pythonInput pyInput, id string) string {
fmt.Print("\r[X\n[ ] Recording")
t := time.Now()
ch := make(chan string)
go func() {
out, err := executeCaptureOutput("python",
os.Getenv("GOPATH")+"\\src\\github.com\\coldwaterq\\recordSite\\drivers\\watch.py",
pythonInput.firstPlaybackButton,
pythonInput.errorClass,
pythonInput.titleClass,
pythonInput.fullscreenButton,
pythonInput.seccondPlaybackButton,
pythonInput.profile,
pythonInput.pauseButton,
url,
id,
pythonInput.maxCheckLoop,
pythonInput.sendKeyTarget,
pythonInput.finishedClass)
// the timeout helps, because going through shows/movies too quick caues the services to freak out.
if err != nil {
fmt.Print(" - ", out, " - ", err)
ch <- ""
} else {
ch <- out
}
}()
ffmpeg := startFfmpeg(id)
if ffmpeg == nil {
return ""
}
name := ""
for {
done := false
select {
case name = <-ch:
done = true
default:
time.Sleep(time.Second)
fmt.Print("\r[ ] Recording - " + time.Since(t).String())
if time.Since(t) > time.Minute*(3*60+30) {
done = true
}
}
if done {
break
}
}
if name == "" {
if DEBUG {
fmt.Println(url)
}
fmt.Print("\r[x\n[ ]\tRecording error")
return ""
}
stopFfmpeg(id, ffmpeg)
return name
}
func convertShow(local string, outfile string) bool {
fmt.Print("\r[X\n[ ] Converting")
t := time.Now()
ch := make(chan string)
go func() {
cmd := exec.Command(os.Getenv("USERPROFILE")+"\\go\\src\\github.com\\coldwaterq\\recordsite\\ffmpeg\\bin\\ffmpeg.exe", "-i", local+"\\output.avi", "-vcodec", "libx264", "-crf", "23", "-preset", "medium", "-vf", "format=yuv420p", outfile)
out, err := cmd.Output()
if err != nil {
fmt.Print(" - ", out, " - ", err)
ch <- ""
} else {
ch <- "WORKED: " + string(out)
}
}()
for {
select {
case output := <-ch:
if DEBUG {
fmt.Println(output)
}
if output == "" {
return false
}
return true
default:
time.Sleep(time.Second)
fmt.Print("\r[ ] Converting - " + time.Now().Sub(t).String())
}
}
}
// Browser will start a browser for that service, and pass the parameters to the selenium python script
func Browser(service string, showName string, url string, isTv bool) bool {
outfile := ""
if isTv {
outfile = "shows\\" + cleanName(showName) + " - %(title)s.mp4"
} else {
outfile = "movies\\" + cleanName(showName) + ".mp4"
}
pythonInput := getPythonFile(service, isTv, url)
if pythonInput.profile == "" {
log.Fatal("This show shouldn't have been started.")
}
local := "D:\\dvr"
// start the image
// 4444 is for selenium, and 5900 is for vnc
fmt.Print("[ ] Starting")
id := startDocker(local)
if id == "" {
return false
}
defer cleanup(id)
os.Remove(local + "\\output.avi")
name := watchShow(url, pythonInput, id)
defer wait(time.Now().Add(time.Minute))
executeUnhandled("cmd", "/c", "rmdir /s /q %temp% 2> %temp%/rmdir.txt")
if name == "" {
return false
}
name = cleanAndPickName(name)
outfile = strings.Replace(outfile, "%(title)s", name, -1)
if !convertShow(local, outfile) {
return false
}
os.Remove(local + "\\output.avi")
return true
}
func wait(till time.Time) {
fmt.Print("\r[X\n[ ] Waiting")
t := time.Now()
for till.After(time.Now()) {
time.Sleep(time.Second)
fmt.Print("\r[ ] Waiting - " + time.Now().Sub(t).String())
}
}
func cleanAndPickName(name string) string {
name = cleanName(name)
parts := strings.Split(name, "_")
length := 0
for _, part := range parts {
part = strings.TrimSpace(part)
if len(part) > length {
name = part
length = len(part)
}
}
return name
}
func cleanName(name string) string {
name = strings.Replace(name, ":", " ", -1)
name = strings.Replace(name, "'", "", -1)
name = strings.Replace(name, "\"", " ", -1)
validChars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- .,()"
tempName := ""
for _, character := range name {
c := string(character)
if strings.Contains(validChars, c) {
tempName += c
} else {
tempName += "_"
}
}
return tempName
}
func cleanup(id string) {
fmt.Print("\r[X\n[ ] Stopping")
output, err := executeCaptureOutput("docker", "stop", id)
if err != nil || output != id {
fmt.Println(output)
log.Println("You may have to manaully stop the container: " + id)
log.Fatal(err)
}
fmt.Println("\r[X")
}
func executeCaptureOutput(command ...string) (string, error) {
cmd := exec.Command(command[0], command[1:]...)
cmd.Stderr = os.Stderr
output, err := cmd.Output()
return strings.TrimSpace(string(output)), err
}
func execute(command ...string) {
err := executeUnhandled(command...)
if err != nil {
log.Fatal(err)
}
}
func executeUnhandled(command ...string) error {
cmd := exec.Command(command[0], command[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err := cmd.Run()
return err
}