forked from jasonparekh/slingdvr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecorder.go
147 lines (118 loc) · 3.69 KB
/
recorder.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
package main
import (
"fmt"
"math"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
)
var curRecording *Showing
func Recorder(recordC chan Showing) (err error) {
curRecordingSetter := make(chan *Showing)
for {
select {
case showing := <-recordC:
if curRecording != nil {
fmt.Printf("Already recording another program %s [ends at %s], waiting until it is finished to start %s [began at %s].. currently %s\n", curRecording.Title, curRecording.End, showing.Title, showing.Start, timeNow())
go func(curRecording, showing Showing) {
time.Sleep(time.Duration(math.Max(float64(time.Second), float64(curRecording.End.Sub(timeNow())))))
recordC <- showing
}(*curRecording, showing)
continue
}
curRecording = &showing
go record(showing, curRecordingSetter, showing.End)
case curRecording = <-curRecordingSetter:
}
}
return
}
func record(showing Showing, curRecordingSetter chan<- *Showing, endTime time.Time) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Error: %s", r)
}
}()
fmt.Printf("Recording %s (started at %s) is now recording (%s), ends at %s\n", showing.Title, showing.Start.Local(), timeNow().Local(), showing.End.Local())
if curRecordingSetter != nil {
defer func() {
curRecordingSetter <- nil
}()
}
args := getSlingArgs()
filename, finalFilename := genFilename(showing, 0)
// secsLeft := showing.End.Sub(timeNow()) / time.Second
// if secsLeft < 0 {
// secsLeft = 1
// }
args = append(args, "-output", filename)
bin := fmt.Sprintf("%s/rec2a.pl", filepath.Dir(os.Args[0]))
if _, err := os.Stat(bin); os.IsNotExist(err) {
bin, err = filepath.Abs("rec2a.pl")
if err != nil {
panic(err)
}
}
fmt.Println(" Running: ", bin, strings.Join(args, " "))
cmd := exec.Command(bin, args...)
if err := cmd.Start(); err != nil {
fmt.Println("Could not start recorder:", err)
return
}
wg := sync.WaitGroup{}
wg.Add(1)
// NOTE: $dur param to perl script was not working properly (quick glance shows they are counting based on packets, which Sling adapter may screw up)
go func() {
defer wg.Done()
time.Sleep(endTime.Sub(timeNow()))
if err := cmd.Process.Kill(); err != nil {
fmt.Println("Could not kill process: ", err.Error())
}
if err := StartJobs(filename, finalFilename); err != nil {
fmt.Println("Could not finish jobs: " + err.Error())
}
}()
if err := cmd.Wait(); err != nil && !strings.Contains(err.Error(), "signal: killed") {
fmt.Println("Error running: ", err)
}
if endTime.Sub(timeNow()) > 30*time.Second {
time.Sleep(time.Second)
fmt.Println("Exited early, trying again")
record(showing, curRecordingSetter, endTime)
}
wg.Wait()
}
func getSlingArgs() (args []string) {
for k, v := range rawConfig {
if !strings.HasPrefix(k, "sling") {
continue
}
args = append(args, "-"+strings.ToLower(k[5:6])+k[6:], fmt.Sprintf("%s", v))
}
return
}
func genFilename(showing Showing, ver int) (string, string) {
finalFilename := fmt.Sprintf("%s/%s - %s", config.RecordingDir, showing.Title, showing.Start.Local().Format("2006-01-02 3:04PM"))
filename := fmt.Sprintf("%s/Raw - %s - %s", config.RecordingDir, showing.Title, showing.Start.Local().Format("2006-01-02 3:04PM"))
if showing.Title != showing.Subtitle {
filename += ": " + showing.Subtitle
finalFilename += ": " + showing.Subtitle
}
if ver > 100 {
panic(fmt.Sprintf("Too many attempts to record showing: %#v", showing))
}
if ver > 0 {
filename += fmt.Sprintf(" (part %d)", ver+1)
finalFilename += fmt.Sprintf(" (part %d)", ver+1)
}
filename += ".asf"
finalFilename += ".mp4"
if _, err := os.Stat(filename); os.IsNotExist(err) {
return filename, finalFilename
} else {
return genFilename(showing, ver+1)
}
}