/
transcode.go
692 lines (590 loc) · 18.6 KB
/
transcode.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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
package server
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io"
"log"
"os"
"os/exec"
"runtime"
"strings"
"sync"
"time"
"github.com/AudiusProject/audius-protocol/mediorum/cidutil"
"github.com/AudiusProject/audius-protocol/mediorum/crudr"
"github.com/disintegration/imaging"
"github.com/spf13/cast"
"golang.org/x/exp/slices"
"golang.org/x/exp/slog"
)
var (
audioPreviewDuration = "30" // seconds
)
func (ss *MediorumServer) startTranscoder() {
myHost := ss.Config.Self.Host
work := make(chan *Upload)
// use most cpus for transcode
numWorkers := runtime.NumCPU() - 2
if numWorkers < 2 {
numWorkers = 2
}
// on boot... reset any of my wip jobs
for _, statuses := range [][]string{{JobStatusBusy, JobStatusNew}, {JobStatusBusyRetranscode, JobStatusRetranscode}} {
busyStatus := statuses[0]
resetStatus := statuses[1]
tx := ss.crud.DB.Model(Upload{}).
Where(Upload{
TranscodedBy: myHost,
Status: busyStatus,
}).
Updates(Upload{Status: resetStatus})
if tx.Error != nil {
ss.logger.Warn("reset stuck uploads error" + tx.Error.Error())
} else if tx.RowsAffected > 0 {
ss.logger.Info("reset stuck uploads", "count", tx.RowsAffected)
}
}
// add a callback to crudr that so we can consider new uploads
ss.crud.AddOpCallback(func(op *crudr.Op, records interface{}) {
if op.Table != "uploads" || op.Action != crudr.ActionCreate {
return
}
uploads, ok := records.(*[]*Upload)
if !ok {
log.Printf("unexpected type in transcode callback %T", records)
return
}
for _, upload := range *uploads {
// only the first mirror transcodes
if upload.Status == JobStatusNew && slices.Index(upload.Mirrors, myHost) == 0 {
ss.logger.Info("got transcode job", "id", upload.ID)
work <- upload
}
}
})
// add a callback to crudr that so we can consider audio preview retranscodes
ss.crud.AddOpCallback(func(op *crudr.Op, records interface{}) {
if op.Table != "uploads" || op.Action != crudr.ActionUpdate {
return
}
uploads, ok := records.(*[]*Upload)
if !ok {
log.Printf("unexpected type in transcode callback %T", records)
return
}
for _, upload := range *uploads {
if upload.Status == JobStatusRetranscode {
if upload.TranscodedMirrors == nil {
ss.logger.Warn("missing full transcoded mp3 data in retranscode job. skipping", "id", upload.ID)
continue
}
// only the first mirror transcodes
if slices.Index(upload.TranscodedMirrors, myHost) == 0 {
ss.logger.Info("got retranscode job", "id", upload.ID)
work <- upload
}
}
}
})
// start workers
for i := 0; i < numWorkers; i++ {
go ss.startTranscodeWorker(i, work)
}
// finally... poll periodically for uploads that slipped thru the cracks
for {
time.Sleep(time.Minute)
// find missed transcode jobs
ss.findMissedJobs(work, myHost, false)
// find missed retranscode jobs
ss.findMissedJobs(work, myHost, true)
}
}
func (ss *MediorumServer) findMissedJobs(work chan *Upload, myHost string, retranscode bool) {
newStatus := JobStatusNew
busyStatus := JobStatusBusy
errorStatus := JobStatusError
if retranscode {
newStatus = JobStatusRetranscode
busyStatus = JobStatusBusyRetranscode
errorStatus = JobStatusErrorRetranscode
}
uploads := []*Upload{}
ss.crud.DB.Where("status in ?", []string{newStatus, busyStatus, errorStatus}).Find(&uploads)
for _, upload := range uploads {
if upload.ErrorCount > 5 {
continue
}
mirrors := upload.Mirrors
if retranscode {
if upload.TranscodedMirrors == nil {
ss.logger.Warn("missing full transcoded mp3 mirrors in retranscode job. skipping", "id", upload.ID)
continue
}
mirrors = upload.TranscodedMirrors
}
myIdx := slices.Index(mirrors, myHost)
if myIdx == -1 {
continue
}
myRank := myIdx + 1
logger := ss.logger.With("upload_id", upload.ID, "upload_status", upload.Status, "my_rank", myRank)
// normally this job of mine would start via callback
// but just in case we could enqueue it here.
// there's a chance it would be processed twice if the callback and this loop
// ran at the same instant...
// but I'm not too worried about that race condition
if myRank == 1 && upload.Status == newStatus {
logger.Info("my upload not started")
work <- upload
continue
}
// determine if #1 rank worker dropped ball
timedOut := false
neverStarted := false
// for #2 rank worker:
if myRank == 2 {
// no recent update?
timedOut = upload.Status == busyStatus &&
time.Since(upload.TranscodedAt) > time.Minute*3
// never started?
neverStarted = upload.Status == newStatus &&
time.Since(upload.CreatedAt) > time.Minute*6
}
// for #3 rank worker:
if myRank == 3 {
// no recent update?
timedOut = upload.Status == busyStatus &&
time.Since(upload.TranscodedAt) > time.Minute*7
// never started?
neverStarted = upload.Status == newStatus &&
time.Since(upload.CreatedAt) > time.Minute*14
}
if timedOut {
logger.Info("upload timed out... starting")
work <- upload
} else if neverStarted {
logger.Info("upload never started")
work <- upload
}
// take turns retrying errors
if upload.Status == errorStatus && upload.ErrorCount%len(mirrors) == myIdx {
logger.Info("retrying transcode error", "error_count", upload.ErrorCount)
work <- upload
}
}
}
func (ss *MediorumServer) startTranscodeWorker(n int, work chan *Upload) {
for upload := range work {
ss.logger.Debug("transcoding", "upload", upload.ID)
err := ss.transcode(upload)
if err != nil {
ss.logger.Warn("transcode failed", "upload", upload, "err", err)
}
}
}
type JobTemplate string
const (
JobTemplateAudio JobTemplate = "audio"
JobTemplateImgSquare JobTemplate = "img_square"
JobTemplateImgBackdrop JobTemplate = "img_backdrop"
)
const (
JobStatusNew = "new"
JobStatusError = "error"
JobStatusBusy = "busy"
JobStatusRetranscode = "retranscode_preview"
JobStatusBusyRetranscode = "busy_retranscode_preview"
JobStatusErrorRetranscode = "error_retranscode_preview"
JobStatusDone = "done"
)
func (ss *MediorumServer) getKeyToTempFile(fileHash string) (*os.File, error) {
temp, err := os.CreateTemp("", "mediorumTemp")
if err != nil {
return nil, err
}
key := cidutil.ShardCID(fileHash)
blob, err := ss.bucket.NewReader(context.Background(), key, nil)
if err != nil {
return nil, err
}
defer blob.Close()
_, err = io.Copy(temp, blob)
if err != nil {
return nil, err
}
temp.Sync()
return temp, nil
}
type errorCallback func(err error, uploadStatus string, info ...string) error
func (ss *MediorumServer) transcodeAudio(upload *Upload, destPath string, cmd *exec.Cmd, logger *slog.Logger, onError errorCallback) error {
stdout, err := cmd.StdoutPipe()
if err != nil {
return onError(err, upload.Status, "cmd.StdoutPipe")
}
stderr, err := cmd.StderrPipe()
if err != nil {
return onError(err, upload.Status, "cmd.StderrPipe")
}
err = cmd.Start()
if err != nil {
return onError(err, upload.Status, "cmd.Start")
}
// WaitGroup to make sure all stdout/stderr processing is done before cmd.Wait() is called
var wg sync.WaitGroup
wg.Add(2)
var stderrBuf bytes.Buffer
var stdoutBuf bytes.Buffer
// Log stdout
go func() {
defer wg.Done()
stdoutLines := bufio.NewScanner(stdout)
for stdoutLines.Scan() {
stdoutBuf.WriteString(stdoutLines.Text())
stdoutBuf.WriteString("\n")
}
if err := stdoutLines.Err(); err != nil {
logger.Error("stdoutLines.Scan", "err", err)
}
logger.Info("stdout lines: " + stdoutBuf.String())
}()
// Log stderr and parse it to update transcode progress
go func() {
defer wg.Done()
stderrLines := bufio.NewScanner(stderr)
durationUs := float64(0)
if upload.FFProbe != nil {
durationSeconds := cast.ToFloat64(upload.FFProbe.Format.Duration)
durationUs = durationSeconds * 1000 * 1000
}
for stderrLines.Scan() {
line := stderrLines.Text()
stderrBuf.WriteString(line)
stderrBuf.WriteString("\n")
if upload.FFProbe != nil {
var u float64
fmt.Sscanf(line, "out_time_us=%f", &u)
if u > 0 && durationUs > 0 {
percent := u / durationUs
if percent-upload.TranscodeProgress > 0.1 {
upload.TranscodeProgress = percent
upload.TranscodedAt = time.Now().UTC()
ss.crud.Patch(upload)
}
}
}
}
if err := stderrLines.Err(); err != nil {
logger.Error("stderrLines.Scan", "err", err)
}
// logger.Error("stderr lines: " + stderrBuf.String())
}()
wg.Wait()
err = cmd.Wait()
if err != nil {
return onError(err, upload.Status, "ffmpeg", "stdout="+stdoutBuf.String(), "stderr="+stderrBuf.String())
}
return nil
}
func (ss *MediorumServer) transcodeFullAudio(upload *Upload, temp *os.File, logger *slog.Logger, onError errorCallback) error {
srcPath := temp.Name()
destPath := srcPath + "_320.mp3"
defer os.Remove(destPath)
cmd := exec.Command("ffmpeg",
"-y",
"-i", srcPath,
"-b:a", "320k", // set bitrate to 320k
"-ar", "48000", // set sample rate to 48000 Hz
"-f", "mp3", // force output to mp3
"-metadata", fmt.Sprintf(`fileName="%s"`, upload.OrigFileName),
"-metadata", fmt.Sprintf(`uuid="%s"`, upload.ID), // make each upload unique so artists can re-upload same file with different CID if it gets delisted
"-vn", // no video
"-threads", "2", // limit to 2 threads per worker to avoid CPU spikes
"-progress", "pipe:2",
destPath)
err := ss.transcodeAudio(upload, destPath, cmd, logger, onError)
if err != nil {
return err
}
dest, err := os.Open(destPath)
if err != nil {
return onError(err, upload.Status, "os.Open")
}
defer dest.Close()
// replicate to peers
// attempt to forward to an assigned node
resultHash, err := cidutil.ComputeFileCID(dest)
if err != nil {
return onError(err, upload.Status, "computeFileCID")
}
resultKey := resultHash
upload.TranscodedMirrors, err = ss.replicateFileParallel(resultHash, destPath, upload.PlacementHosts)
if err != nil {
return onError(err, upload.Status, "replicateFile")
}
upload.TranscodeResults["320"] = resultKey
logger.Info("audio transcode done", "mirrors", upload.TranscodedMirrors)
// if a start time is set, also transcode an audio preview from the full 320kbps downsample
if upload.SelectedPreview.Valid {
// update upload to reflect the start of the retranscoding preview step
upload.TranscodeProgress = 0
upload.TranscodedAt = time.Now().UTC()
upload.Status = JobStatusBusyRetranscode
ss.crud.Update(upload)
return ss.transcodeAudioPreview(upload, dest, logger, onError)
}
return nil
}
func (ss *MediorumServer) transcodeAudioPreview(upload *Upload, temp *os.File, logger *slog.Logger, onError errorCallback) error {
if !upload.SelectedPreview.Valid {
logger.Info("no audio preview start time set. skipping preview transcode", "id", upload.ID)
return nil
}
srcPath := temp.Name()
destPath := strings.TrimSuffix(srcPath, "_320.mp3") + "_320_preview.mp3"
defer os.Remove(destPath)
splitPreview := strings.Split(upload.SelectedPreview.String, "|")
previewStart := splitPreview[1]
cmd := exec.Command("ffmpeg",
"-y",
"-i", srcPath,
"-ss", previewStart, // set preview start time
"-t", audioPreviewDuration, // set preview duration
"-b:a", "320k", // set bitrate to 320k
"-ar", "48000", // set sample rate to 48000 Hz
"-f", "mp3", // force output to mp3
"-metadata", fmt.Sprintf(`fileName="%s"`, upload.OrigFileName),
"-metadata", fmt.Sprintf(`uuid="%s"`, upload.ID), // make each upload unique so artists can re-upload same file with different CID if it gets delisted
"-vn", // no video
"-progress", "pipe:2",
destPath)
err := ss.transcodeAudio(upload, destPath, cmd, logger, onError)
if err != nil {
return err
}
dest, err := os.Open(destPath)
if err != nil {
return onError(err, upload.Status, "os.Open")
}
defer dest.Close()
// replicate to peers
// attempt to forward to an assigned node
resultHash, err := cidutil.ComputeFileCID(dest)
if err != nil {
return onError(err, upload.Status, "computeFileCID")
}
resultKey := resultHash
mirrors, err := ss.replicateFileParallel(resultHash, destPath, upload.PlacementHosts)
if err != nil {
return onError(err, upload.Status, "replicating file")
}
upload.TranscodeResults[upload.SelectedPreview.String] = resultKey
logger.Info("audio preview transcode done", "mirrors", mirrors)
return nil
}
func filterErrorLines(input string, errorTypes []string, maxCount int) string {
lines := strings.Split(input, "\\n")
var builder strings.Builder
errorCounts := make(map[string]int)
outerLoop:
for _, line := range lines {
for _, errorType := range errorTypes {
if strings.Contains(line, errorType) {
if errorCounts[errorType] < maxCount {
errorCounts[errorType]++
builder.WriteString(line + "\\n")
}
continue outerLoop
}
}
builder.WriteString(line + "\\n")
}
return builder.String()
}
func (ss *MediorumServer) transcode(upload *Upload) error {
upload.TranscodedBy = ss.Config.Self.Host
upload.TranscodedAt = time.Now().UTC()
// Differentiate between normal transcode vs retranscode jobs
if upload.Status == JobStatusRetranscode || upload.Status == JobStatusBusyRetranscode || upload.Status == JobStatusErrorRetranscode {
upload.Status = JobStatusBusyRetranscode
} else {
upload.Status = JobStatusBusy
}
ss.crud.Update(upload)
fileHash := upload.OrigFileCID
if upload.Status == JobStatusBusyRetranscode {
// Re-transcode previews off the 320kbps downsample
fileHash = upload.TranscodeResults["320"]
}
logger := ss.logger.With("template", upload.Template, "cid", fileHash)
onError := func(err error, uploadStatus string, info ...string) error {
// limit repetitive lines
errorTypes := []string{
"Header missing",
"Error while decoding",
"Invalid data",
"Application provided invalid",
"out_time_ms=",
"out_time_us",
"bitrate=",
"progress=",
}
filteredError := filterErrorLines(err.Error(), errorTypes, 10)
errMsg := fmt.Errorf("%s %s", filteredError, strings.Join(info, " "))
upload.Error = errMsg.Error()
if uploadStatus == JobStatusRetranscode || uploadStatus == JobStatusBusyRetranscode {
upload.Status = JobStatusErrorRetranscode
} else {
upload.Status = JobStatusError
}
upload.ErrorCount = upload.ErrorCount + 1
ss.crud.Update(upload)
return errMsg
}
temp, err := ss.getKeyToTempFile(fileHash)
if err != nil {
return onError(err, upload.Status, "getting file")
}
defer temp.Close()
defer os.Remove(temp.Name())
switch JobTemplate(upload.Template) {
case JobTemplateImgSquare:
// 150x150, 480x480, 1000x1000
squares := []int{150, 480, 1000}
for _, targetBox := range squares {
temp.Seek(0, 0)
out, w, h := Resized(".jpg", temp, targetBox, targetBox, "fill")
resultHash, err := cidutil.ComputeFileCID(out)
if err != nil {
return onError(err, upload.Status, "computeFileCID")
}
mirrors, err := ss.replicateFile(resultHash, out)
if err != nil {
return onError(err, upload.Status, "replicate")
}
logger.Debug("did square", "w", w, "h", h, "key", resultHash, "mirrors", mirrors)
variantName := fmt.Sprintf("%dx%[1]d.jpg", targetBox)
upload.TranscodeResults[variantName] = resultHash
}
case JobTemplateImgBackdrop:
// 640x, 2000x
widths := []int{640, 2000}
for _, targetWidth := range widths {
temp.Seek(0, 0)
out, w, h := Resized(".jpg", temp, targetWidth, AUTO, "fill")
resultHash, err := cidutil.ComputeFileCID(out)
if err != nil {
return onError(err, upload.Status, "computeFileCID")
}
mirrors, err := ss.replicateFile(resultHash, out)
if err != nil {
return onError(err, upload.Status, "replicate")
}
logger.Debug("did backdrop", "w", w, "h", h, "key", resultHash, "mirrors", mirrors)
variantName := fmt.Sprintf("%dx.jpg", targetWidth)
upload.TranscodeResults[variantName] = resultHash
}
case JobTemplateAudio, "":
if upload.Template == "" {
logger.Warn("empty template (shouldn't happen), falling back to audio")
}
if upload.Status == JobStatusBusyRetranscode {
// Re-transcode audio preview
err := ss.transcodeAudioPreview(upload, temp, logger, onError)
if err != nil {
return err
}
} else {
// Transcode audio and (optional) audio preview
err := ss.transcodeFullAudio(upload, temp, logger, onError)
if err != nil {
return err
}
}
}
upload.TranscodeProgress = 1
upload.TranscodedAt = time.Now().UTC()
upload.Status = "done"
upload.Error = ""
ss.crud.Update(upload)
return nil
}
type FFProbeResult struct {
Format struct {
Filename string `json:"filename"`
NbStreams int `json:"nb_streams"`
NbPrograms int `json:"nb_programs"`
FormatName string `json:"format_name"`
FormatLongName string `json:"format_long_name"`
Duration string `json:"duration,omitempty"`
Size string `json:"size"`
BitRate string `json:"bit_rate,omitempty"`
ProbeScore int `json:"probe_score"`
Tags map[string]string `json:"tags,omitempty"`
} `json:"format"`
}
func ffprobe(sourcePath string) (*FFProbeResult, error) {
probe, err := exec.Command("ffprobe",
"-v", "quiet",
"-print_format", "json",
"-show_format",
sourcePath).
Output()
if err != nil {
return nil, err
}
// fmt.Println(string(probe))
var probeResult *FFProbeResult
err = json.Unmarshal(probe, &probeResult)
return probeResult, err
}
const AUTO = -1
func Resized(ext string, read io.ReadSeeker, width, height int, mode string) (resized io.ReadSeeker, w int, h int) {
if width == 0 && height == 0 {
return read, 0, 0
}
srcImage, _, err := image.Decode(read)
if err == nil {
bounds := srcImage.Bounds()
var dstImage *image.NRGBA
// Maintain aspect ratio when auto-resizing height based on target width
if height == AUTO {
srcW := bounds.Dx()
srcH := bounds.Dy()
autoHeight := float64(srcH) * (float64(width) / float64(srcW))
height = int(autoHeight)
}
switch mode {
case "fit":
dstImage = imaging.Fit(srcImage, width, height, imaging.Lanczos)
case "fill":
dstImage = imaging.Fill(srcImage, width, height, imaging.Center, imaging.Lanczos)
default:
if width == height && bounds.Dx() != bounds.Dy() {
dstImage = imaging.Thumbnail(srcImage, width, height, imaging.Lanczos)
w, h = width, height
} else {
dstImage = imaging.Resize(srcImage, width, height, imaging.Lanczos)
}
}
var buf bytes.Buffer
switch ext {
case ".png":
png.Encode(&buf, dstImage)
case ".jpg", ".jpeg":
jpeg.Encode(&buf, dstImage, nil)
case ".gif":
gif.Encode(&buf, dstImage, nil)
}
return bytes.NewReader(buf.Bytes()), dstImage.Bounds().Dx(), dstImage.Bounds().Dy()
} else {
log.Println(err)
}
return read, 0, 0
}