-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
video.go
465 lines (384 loc) · 12.1 KB
/
video.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
package main
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/gorilla/mux"
"io"
"net/http"
"os"
"os/exec"
"pomu/hls"
"pomu/qualities"
"pomu/s3"
"pomu/video"
"strconv"
"strings"
"time"
"github.com/lib/pq"
log "github.com/sirupsen/logrus"
"github.com/getsentry/sentry-go"
)
type ytdlRemotePlaylist struct {
request VideoRequest
}
// ErrorLivestreamNotStarted indicates that the livestream has not started
var ErrorLivestreamNotStarted = errors.New("livestream has not started")
func (p *ytdlRemotePlaylist) Get() (string, error) {
log.Println("Getting playlist url for", p.request.VideoUrl)
span := sentry.StartSpan(
context.Background(),
"youtube-dl get playlist",
sentry.TransactionName(
fmt.Sprintf("youtube-dl get playlist %s", p.request.VideoUrl)))
defer span.Finish()
// Check that we are trying to record a valid quality
if p.request.Quality <= 0 {
// Stream was queued ahead of time, select best quality
qualities, _, err := qualities.GetVideoQualities(p.request.VideoUrl, true)
if err != nil {
log.Println("Whilst trying to get playlist url, was unable to get qualities for video")
return "", err
}
for _, quality := range qualities {
if quality.Best {
log.Println("Whilst trying to get playlist url, automatically chose quality", quality.Code)
p.request.Quality = quality.Code
}
}
}
output := new(strings.Builder)
cmd := exec.Command(os.Getenv("YT_DLP"), "--force-ipv4", "-f", strconv.Itoa(int(p.request.Quality)), "-g", p.request.VideoUrl)
cmd.Stdout = output
cmd.Stderr = output
err := cmd.Run()
// NOTE(emily): If the livestream has not started yet, ytdl will return 1
// We want to check first whether the live event WILL begin, and return the correct
// error
if strings.Contains(output.String(), "This live event will begin in") ||
strings.Contains(output.String(), "Premieres in") ||
strings.Contains(output.String(), "Premiere will begin") {
return "", ErrorLivestreamNotStarted
}
if err != nil {
sentry.AddBreadcrumb(&sentry.Breadcrumb{Level: sentry.LevelDebug, Message: fmt.Sprintf("ffmpeg output was %s", output)})
sentry.CaptureException(err)
log.Printf("cannot run youtube-dl: %s (output was %s)\n", err, output)
return "", err
}
span.Finish()
stringOutput := strings.TrimSpace(output.String())
if !strings.HasSuffix(stringOutput, ".m3u8") {
log.Printf("Expected m3u8 output, received %s\n", stringOutput)
return "", errors.New("expected m3u8")
}
return stringOutput, nil
}
var _ hls.RemotePlaylist = (*ytdlRemotePlaylist)(nil)
var ffmpegLogs = make(map[string]*strings.Builder)
func hasLivestreamStarted(request VideoRequest) (bool, error) {
_, err := (&ytdlRemotePlaylist{request}).Get()
if err == ErrorLivestreamNotStarted {
return false, nil
} else if err != nil {
return false, err
}
return true, nil
}
func videoLengthFromLog(id string) time.Duration {
// NOTE(emily): Here we can get the video length by looking at the ffmpeg log
logString := strings.TrimSpace(ffmpegLogs[id].String())
timeStart := strings.LastIndex(logString, "time=") + len("time=")
timeEnd := strings.Index(logString[timeStart:], " ")
timeStr := logString[timeStart : timeStart+timeEnd]
// replace punctuation with their respective time units
timeStr = strings.Replace(timeStr, ":", "h", 1)
timeStr = strings.Replace(timeStr, ":", "m", 1)
timeStr = strings.Replace(timeStr, ".", "s", 1)
timeStr = timeStr + "ms"
duration, err := time.ParseDuration(timeStr)
if err != nil {
log.Println("Failed to parse duration from ffmpeg log")
}
return duration
}
func (app *Application) recordFinished(db *sql.DB, id string, size int64) error {
tx, err := db.Begin()
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("failed to begin transaction")
return err
}
defer tx.Rollback()
length := videoLengthFromLog(id)
log.WithFields(log.Fields{
"id": id,
"size": size,
"length": length,
}).Info("finishing video")
var video Video
statement, err := tx.Prepare("update videos set finished = true, file_size = $1, video_length = $2 where id = $3 returning *")
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("failed to prepare statement")
return err
}
row := statement.QueryRow(size, int(length.Seconds()), id)
if err := row.Err(); err != nil {
log.WithFields(log.Fields{"error": err}).Error("failed to execute statement")
return err
}
if err = row.Scan(&video.Id,
pq.Array(&video.Submitters),
&video.Start,
&video.Finished,
&video.Title,
&video.ChannelName,
&video.ChannelId,
&video.Thumbnail,
&video.FileSize,
&video.Length,
&video.Downloads); err != nil {
log.WithFields(log.Fields{"error": err}).Error("failed to serialize row into video")
return err
}
if err := tx.Commit(); err != nil {
log.WithFields(log.Fields{"error": err}).Error("failed to commit transaction")
return err
}
go app.UpsertVideo(video)
return nil
}
func recordFailed(db *sql.DB, id string) error {
log.Println("Record for", id, "failed, deleting from database")
// TODO(emily): Probably want to make sure that s3 is cleaned up as well
tx, err := db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
_, err = tx.Exec("delete from videos where id = $1", id)
if err != nil {
return err
}
if tx.Commit() != nil {
return err
}
return nil
}
func record(request VideoRequest) (size int64, err error) {
log.Println("Starting recording of ", request.VideoUrl)
span := sentry.StartSpan(
context.Background(),
"record",
sentry.TransactionName(
fmt.Sprintf("record %s", request.VideoUrl)))
defer span.Finish()
id, err := request.Id()
if err != nil {
log.Println("failed to get video id: ", err)
sentry.CaptureException(err)
return
}
// Start getting segments
hlsClient := hls.New(id)
defer hlsClient.Stop()
go func() {
hlsClientPlaylistSpan := span.StartChild("hls-client playlist")
defer hlsClientPlaylistSpan.Finish()
logVideo(request, nil).Info("Starting HLS Client")
defer logVideo(request, nil).Info("HLS Client stopped")
hlsClient.Playlist(&ytdlRemotePlaylist{request})
}()
// Start the video muxer
muxer := &video.Muxer{}
ffmpegLogs[id] = new(strings.Builder)
muxer.Stderr = ffmpegLogs[id]
err = muxer.Start()
if err != nil {
log.Println(id, "Failed to start ffmpeg:", err)
sentry.CaptureException(err)
return 0, errors.New("failed to start ffmpeg")
}
finished := make(chan struct{})
defer close(finished)
s3, err := s3.New(os.Getenv("S3_BUCKET"))
if err != nil {
sentry.CaptureException(err)
return 0, errors.New("failed to contact s3 bucket")
}
sizeWritten := make(chan int64)
defer close(sizeWritten)
go func() {
muxerSpan := span.StartChild("muxer-uploader loop")
defer muxerSpan.Finish()
reader, writer := io.Pipe()
go func() {
defer func() { finished <- struct{}{} }()
err := s3.Upload(fmt.Sprintf("%s.mp4", id), reader, "video/mp4")
if err != nil {
log.Println(id, "s3.Upload2():", err)
sentry.CaptureException(err)
return
}
log.Println(id, "s3 Upload successfully finished")
}()
logVideo(request, nil).Info("Begin copying")
// sentry.AddBreadcrumb(&sentry.Breadcrumb{Message: "copy from muxer to s3"})
size, err := io.Copy(writer, muxer)
if err != nil {
logVideo(request, err).Error("copy muxer to s3:", err)
sentry.CaptureException(err)
}
logVideo(request, nil).Info(id, "Finished reading from ffmpeg: ", size)
// NOTE(emily): Must close first before writing.
// sizeWritten <- size will block until its read
// but it wont be read until s3 finishes, which is after the writer
// has closed.
_ = writer.CloseWithError(io.EOF)
sizeWritten <- size
}()
go func() {
downloaderSpan := span.StartChild("downloader")
defer downloaderSpan.Finish()
logVideo(request, nil).Info("Starting segment downloader")
defer logVideo(request, nil).Info("Segment downloader stopped")
video.Download(id, hlsClient.Segments, muxer)
}()
<-finished
log.Println(id, "record finished")
go uploadLog(s3, id)
return <-sizeWritten, nil
}
func uploadLog(s3 *s3.Client, id string) {
ffmpegLog := ffmpegLogs[id].String()
lines := strings.Split(ffmpegLog, "\n")
if len(lines) > 3 {
lines = lines[3:]
}
err := s3.Upload(fmt.Sprintf("%s.log", id), strings.NewReader(strings.Join(lines, "\n")), "text/plain")
if err != nil {
log.Println(id, "uploadLog: s3.Upload2():", err)
sentry.CaptureException(err)
return
}
}
func logVideo(request VideoRequest, err error) (entry *log.Entry) {
if err == nil {
entry = log.WithFields(log.Fields{"video_url": request.VideoUrl})
} else {
entry = log.WithFields(log.Fields{"video_url": request.VideoUrl, "error": err})
}
return
}
func StartRecording(app *Application, request VideoRequest) {
logVideo(request, nil).Info("Start recording")
id, err := request.Id()
if err != nil {
logVideo(request, err).Error("Failed to get video id")
return
}
// See if this video has been re-scheduled into the future...
metadata, err := GetVideoMetadata(id)
if err != nil {
logVideo(request, err).Error("Failed to get metadata for scheduled video")
return
}
newStartTime, err := GetVideoStartTime(metadata)
if err != nil {
logVideo(request, err).Error("Failed to parse new start time from metadata for video")
return
}
const RETRY_INTERVAL = 1 * time.Minute
const MAX_RETRIES = 120
const MAX_DURATION = RETRY_INTERVAL * MAX_RETRIES
if time.Until(newStartTime) > (RETRY_INTERVAL * MAX_RETRIES) {
logVideo(request, nil).Info("video has been moved to more than", MAX_DURATION.String(), "into the future, rescheduling")
// Schedule a new cronjob that will re-queue the video
if _, err := Scheduler.
SingletonMode().
LimitRunsTo(1).
StartAt(time.Now().Add(RETRY_INTERVAL)).
Tag("Reschedule"+request.VideoUrl).
Do(app.scheduleVideo, metadata, id, request); err != nil {
logVideo(request, err).Error("Failed to reschedule video")
}
return
}
for try := 0; try < MAX_RETRIES; try += 1 {
if started, err := hasLivestreamStarted(request); err == nil && started {
size, err := record(request)
if err != nil {
log.Println("record failed:", err)
return
}
err = app.recordFinished(app.db, id, size)
if err != nil {
logVideo(request, err).Error("Failed record finish")
}
return
} else if err == ErrorLivestreamNotStarted {
logVideo(request, nil).Info("Livestream has not started yet")
} else if err != nil {
logVideo(request, err).Error("Failed checking livestream started")
err = recordFailed(app.db, id)
if err != nil {
logVideo(request, err).Error("Failed recordFailed")
}
return
}
logVideo(request, nil).Info("Waiting for video, try=", try)
time.Sleep(RETRY_INTERVAL)
}
}
func (app *Application) Log(w http.ResponseWriter, r *http.Request) {
ytUrl := r.URL.Query().Get("url")
var id string
if len(ytUrl) == 0 {
id = r.URL.Query().Get("id")
} else {
id = qualities.ParseVideoID(ytUrl)
}
if log, ok := ffmpegLogs[id]; ok {
_, err := w.Write([]byte(log.String()))
if err != nil {
http.Error(w, "failed to write output bytes", http.StatusInternalServerError)
}
return
}
w.WriteHeader(http.StatusNotFound)
}
func (app *Application) DownloadCount(w http.ResponseWriter, r *http.Request) {
user, err := app.ResolveUserFromRequest(r)
if user == nil || err != nil {
http.Error(w, "please login first", http.StatusUnauthorized)
return
}
tx, err := app.db.Begin()
if err != nil {
sentry.CaptureException(err)
http.Error(w, "failed to start transaction", http.StatusInternalServerError)
return
}
defer tx.Rollback()
variables := mux.Vars(r)
videoId := variables["id"]
statement, err := tx.Prepare("select downloads from videos where id = $1")
if err != nil {
sentry.CaptureException(err)
http.Error(w, "failed to prepare statement", http.StatusInternalServerError)
return
}
var count int32
if err = statement.QueryRow(videoId).Scan(&count); err != nil {
if err == sql.ErrNoRows {
http.Error(w, "video not found", http.StatusNotFound)
} else {
sentry.CaptureException(err)
http.Error(w, "failed to execute query", http.StatusInternalServerError)
}
return
}
SerializeJson(w, map[string]int32{
"downloads": count,
})
}