-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathencode.go
More file actions
603 lines (483 loc) · 15.2 KB
/
encode.go
File metadata and controls
603 lines (483 loc) · 15.2 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
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
// Package ffmpeg captures video from RTSP streams, like IP cameras.
//
// Provides a simple interface to set FFMPEG options and capture video from an RTSP source.
package ffmpeg
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/url"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
// Default, Maximum and Minimum Values for encoder configuration. Change these if your needs differ.
//
//nolint:gochecknoglobals,mnd // these are constants, not variables, but configurable by a consumer.
var (
DefaultFrameRate = 5
MinimumFrameRate = 1
MaximumFrameRate = 60
DefaultFrameHeight = 720
DefaultFrameWidth = 1280
MinimumFrameSize = 100
MaximumFrameSize = 5000
DefaultEncodeCRF = 21
MinimumEncodeCRF = 16
MaximumEncodeCRF = 30
DefaultCaptureTime = 15
MaximumCaptureTime = 1200 // 10 minute max.
DefaultCaptureSize = int64(2500000) // 2.5MB default (roughly 5-10 seconds)
MaximumCaptureSize = int64(104857600) // 100MB max.
DefaultFFmpegPath = "/usr/local/bin/ffmpeg"
DefaultProfile = "main"
DefaultLevel = "3.0"
)
// Custom errors that this library outputs. The library also outputs errors created elsewhere.
var (
ErrInvalidOutput = errors.New("output path is not valid")
ErrInvalidInput = errors.New("input path is not valid")
)
const (
bits64 = 64
base10 = 10
// Keep the last bytes of ffmpeg stderr to improve diagnostics.
defaultStderrTail = 8192
// Account for slow live streams: processing may take longer than clip length.
minCommandTimeout = 30 * time.Second
)
// Config defines how ffmpeg shall transcode a stream.
// If Copy is true, these options are ignored: profile, level, width, height, crf and frame rate.
type Config struct {
Copy bool // Copy original stream, rather than transcode.
Audio bool // include audio?
Width int // 1920
Height int // 1080
CRF int // 24
Time int // 15 (seconds)
Rate int // framerate (5-20)
Size int64 // max file size (always goes over). use 2000000 for 2.5MB
FFMPEG string // "/usr/local/bin/ffmpeg"
Level string // 3.0, 3.1 ..
Prof string // main, high, baseline
}
// Encoder is the struct returned by this library.
// Contains all the bound methods.
type Encoder struct {
config *Config
}
// Get an encoder interface.
func Get(config *Config) *Encoder {
cfg := &Config{}
if config != nil {
*cfg = *config
}
encode := &Encoder{config: cfg}
if encode.config.FFMPEG == "" {
encode.config.FFMPEG = DefaultFFmpegPath
}
encode.SetLevel(encode.config.Level)
encode.SetProfile(encode.config.Prof)
encode.fixValues()
return encode
}
// Config returns the current values in the encoder.
func (e *Encoder) Config() Config {
return *e.config
}
// SetAudio turns audio on or off based on a string value.
// This can also be passed into Get() as a boolean.
func (e *Encoder) SetAudio(audio string) bool {
e.config.Audio, _ = strconv.ParseBool(audio)
return e.config.Audio
}
// SetLevel sets the h264 transcode level.
// This can also be passed into Get().
func (e *Encoder) SetLevel(level string) string {
if e.config.Level = level; level != "3.0" && level != "3.1" && level != "4.0" && level != "4.1" && level != "4.2" {
e.config.Level = DefaultLevel
}
return e.config.Level
}
// SetProfile sets the h264 transcode profile.
// This can also be passed into Get().
func (e *Encoder) SetProfile(profile string) string {
if e.config.Prof = profile; e.config.Prof != "main" && e.config.Prof != "baseline" && e.config.Prof != "high" {
e.config.Prof = DefaultProfile
}
return e.config.Prof
}
// SetWidth sets the transcode frame width from a string.
// This can also be passed into Get() as an int.
func (e *Encoder) SetWidth(width string) int {
e.config.Width, _ = strconv.Atoi(width)
e.fixValues()
return e.config.Width
}
// SetHeight sets the transcode frame width from a string.
// This can also be passed into Get() as an int.
func (e *Encoder) SetHeight(height string) int {
e.config.Height, _ = strconv.Atoi(height)
e.fixValues()
return e.config.Height
}
// SetCRF sets the h264 transcode CRF value from a string.
// This can also be passed into Get() as an int.
func (e *Encoder) SetCRF(crf string) int {
e.config.CRF, _ = strconv.Atoi(crf)
e.fixValues()
return e.config.CRF
}
// SetTime sets the maximum transcode duration from a string representing seconds.
// This can also be passed into Get() as an int.
func (e *Encoder) SetTime(seconds string) int {
e.config.Time, _ = strconv.Atoi(seconds)
e.fixValues()
return e.config.Time
}
// SetRate sets the transcode framerate from a string.
// This can also be passed into Get() as an int.
func (e *Encoder) SetRate(rate string) int {
e.config.Rate, _ = strconv.Atoi(rate)
e.fixValues()
return e.config.Rate
}
// SetSize sets the maximum transcode file size as a string.
// This can also be passed into Get() as an int64.
func (e *Encoder) SetSize(size string) int64 {
e.config.Size, _ = strconv.ParseInt(size, base10, bits64)
e.fixValues()
return e.config.Size
}
// GetVideo retreives video from an input and returns an io.ReadCloser to consume the output.
// Input must be an RTSP URL. Title is encoded into the video as the "movie title."
// Returns command used for diagnostics, io.ReadCloser and error or nil.
// This will automatically create a context timeout based on the requested capture length.
// For full timeout control, use GetVideoContext().
// If you want to control the context, use GetVideoContext().
func (e *Encoder) GetVideo(input, title string) (string, io.ReadCloser, error) {
ctx := context.Background()
var cancel context.CancelFunc
if e.config.Time > 0 {
ctx, cancel = context.WithTimeout(ctx, captureTimeout(e.config.Time))
}
cmdStr, stream, err := e.GetVideoContext(ctx, input, title)
if err != nil {
if cancel != nil {
cancel()
}
return cmdStr, nil, err
}
if cancel == nil {
return cmdStr, stream, nil
}
return cmdStr, &cancelReadCloser{ReadCloser: stream, cancel: cancel}, nil
}
// GetVideoContext retreives video from an input and returns an io.ReadCloser to consume the output.
// Input must be an RTSP URL. Title is encoded into the video as the "movie title."
// Returns command used for diagnostics, io.ReadCloser and error or nil.
// Use the context to add a timeout value (max run duration) to the ffmpeg command.
//
//nolint:contextcheck // caller-provided context is accepted and used for command execution.
func (e *Encoder) GetVideoContext(ctx context.Context, input, title string) (string, io.ReadCloser, error) {
if input == "" {
return "", nil, ErrInvalidInput
}
if ctx == nil {
ctx = context.Background()
}
cmdCtx, cmdCancel := context.WithCancel(ctx)
cmdStr, cmd := e.getVideoHandle(cmdCtx, input, "-", title)
stderr := newTailBuffer(defaultStderrTail)
cmd.Stderr = stderr
stdoutpipe, err := cmd.StdoutPipe()
if err != nil {
cmdCancel()
return cmdStr, nil, fmt.Errorf("subcommand failed: %w", err)
}
err = cmd.Start()
if err != nil {
_ = stdoutpipe.Close()
cmdCancel()
return cmdStr, nil, withStderr("run failed", err, stderr.String())
}
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
}()
return cmdStr, &streamResult{
out: stdoutpipe,
done: done,
cmdCancel: cmdCancel,
stderr: stderr,
}, nil
}
// SaveVideo saves a video snippet to a file.
// Input must be an RTSP URL and output must be a file path. It will be overwritten.
// Returns command used for diagnostics, command output and error or nil.
// This will automatically create a context timeout based on the requested capture length.
// For full timeout control, use SaveVideoContext().
// If you want to control the context, use SaveVideoContext().
//
//nolint:nonamedreturns // the names help readability.
func (e *Encoder) SaveVideo(input, output, title string) (cmdStr, outputStr string, err error) {
ctx := context.Background()
if e.config.Time > 0 {
var cancel func()
ctx, cancel = context.WithTimeout(ctx, captureTimeout(e.config.Time))
defer cancel()
}
return e.SaveVideoContext(ctx, input, output, title)
}
// SaveVideoContext saves a video snippet to a file using a provided context.
// Input must be an RTSP URL and output must be a file path. It will be overwritten.
// Returns command used for diagnostics, command output and error or nil.
// Use the context to add a timeout value (max run duration) to the ffmpeg command.
//
//nolint:nonamedreturns // the names help readability.
func (e *Encoder) SaveVideoContext(
ctx context.Context, input, output, title string,
) (cmdStr, outputStr string, err error) {
if input == "" {
return "", "", ErrInvalidInput
}
if output == "" || output == "-" {
return "", "", ErrInvalidOutput
}
cmdStr, cmd := e.getVideoHandle(ctx, input, output, title)
stderr := newTailBuffer(defaultStderrTail)
var stdout bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = stderr
err = cmd.Run()
if err != nil {
return cmdStr, stdout.String(), runError(ctx, "subcommand failed", err, stderr.String())
}
return cmdStr, stdout.String(), nil
}
// fixValues makes sure video request values are sane.
func (e *Encoder) fixValues() { //nolint:cyclop // it's a simple switch statement.
switch {
case e.config.Height == 0:
e.config.Height = DefaultFrameHeight
case e.config.Height > MaximumFrameSize:
e.config.Height = MaximumFrameSize
case e.config.Height < MinimumFrameSize:
e.config.Height = MinimumFrameSize
}
switch {
case e.config.Width == 0:
e.config.Width = DefaultFrameWidth
case e.config.Width > MaximumFrameSize:
e.config.Width = MaximumFrameSize
case e.config.Width < MinimumFrameSize:
e.config.Width = MinimumFrameSize
}
switch {
case e.config.CRF == 0:
e.config.CRF = DefaultEncodeCRF
case e.config.CRF < MinimumEncodeCRF:
e.config.CRF = MinimumEncodeCRF
case e.config.CRF > MaximumEncodeCRF:
e.config.CRF = MaximumEncodeCRF
}
switch {
case e.config.Rate == 0:
e.config.Rate = DefaultFrameRate
case e.config.Rate < MinimumFrameRate:
e.config.Rate = MinimumFrameRate
case e.config.Rate > MaximumFrameRate:
e.config.Rate = MaximumFrameRate
}
// No minimums.
if e.config.Time == 0 {
e.config.Time = DefaultCaptureTime
} else if e.config.Time > MaximumCaptureTime {
e.config.Time = MaximumCaptureTime
}
if e.config.Size == 0 {
e.config.Size = DefaultCaptureSize
} else if e.config.Size > MaximumCaptureSize {
e.config.Size = MaximumCaptureSize
}
}
// getVideoHandle is a helper function that creates and returns an ffmpeg command.
// This is used by higher level function to cobble together an input stream.
func (e *Encoder) getVideoHandle(ctx context.Context, input, output, title string) (string, *exec.Cmd) {
if title == "" {
title = filepath.Base(output)
}
// the order of these values is important.
arg := []string{
e.config.FFMPEG,
"-v", "16", // log level
"-i", input,
"-metadata", "title=" + title,
"-y", "-map", "0",
}
if isRTSP(input) {
arg = append(arg[:3], append([]string{"-rtsp_transport", "tcp"}, arg[3:]...)...)
}
if output == "-" {
arg = append(arg, "-f", "mp4", "-movflags", "frag_keyframe+empty_moov")
} else {
arg = append(arg, "-f", "mov")
}
if e.config.Size > 0 {
arg = append(arg, "-fs", strconv.FormatInt(e.config.Size, base10))
}
if e.config.Time > 0 {
arg = append(arg, "-t", strconv.Itoa(e.config.Time))
}
if !e.config.Copy {
arg = append(arg, "-vcodec", "libx264",
"-profile:v", e.config.Prof,
"-level", e.config.Level,
"-pix_fmt", "yuv420p",
"-s", strconv.Itoa(e.config.Width)+"x"+strconv.Itoa(e.config.Height),
"-preset", "superfast",
"-crf", strconv.Itoa(e.config.CRF),
"-r", strconv.Itoa(e.config.Rate),
)
if output != "-" {
arg = append(arg, "-movflags", "faststart")
}
} else {
arg = append(arg, "-c", "copy")
}
if !e.config.Audio {
arg = append(arg, "-an")
} else {
arg = append(arg, "-c:a", "copy")
}
arg = append(arg, output) // save file path goes last.
// This command string is for diagnostics only; it is not shell-escaped.
//nolint:gosec // it's ok, but maybe it's not.
return strings.Join(arg, " "), exec.CommandContext(ctx, arg[0], arg[1:]...)
}
// streamResult is our custom io.ReadCloser that also cleans up the command and context.
type streamResult struct {
out io.ReadCloser
done <-chan error
cmdCancel context.CancelFunc
stderr *tailBuffer
closeOnce sync.Once
closeErr error
}
func (s *streamResult) Read(data []byte) (int, error) {
bytesRead, err := s.out.Read(data)
if err == nil {
return bytesRead, nil
}
if errors.Is(err, io.EOF) {
return bytesRead, io.EOF
}
if err != nil {
return bytesRead, fmt.Errorf("read stream: %w", err)
}
return bytesRead, nil
}
func (s *streamResult) Close() error {
s.closeOnce.Do(func() {
_ = s.out.Close()
select {
case waitErr := <-s.done:
if waitErr != nil && !isIgnorableWaitErr(waitErr) {
s.closeErr = withStderr("run failed", waitErr, s.stderr.String())
}
return
default:
}
s.cmdCancel()
waitErr := <-s.done
if waitErr != nil && !isIgnorableWaitErr(waitErr) {
s.closeErr = withStderr("run failed", waitErr, s.stderr.String())
}
})
return s.closeErr
}
type tailBuffer struct {
buf []byte
max int
}
func newTailBuffer(limit int) *tailBuffer {
return &tailBuffer{max: limit}
}
func (t *tailBuffer) Write(data []byte) (int, error) {
if t.max <= 0 {
return len(data), nil
}
if len(data) >= t.max {
t.buf = append(t.buf[:0], data[len(data)-t.max:]...)
return len(data), nil
}
need := len(t.buf) + len(data) - t.max
if need > 0 {
t.buf = append(t.buf[:0], t.buf[need:]...)
}
t.buf = append(t.buf, data...)
return len(data), nil
}
func (t *tailBuffer) String() string {
return strings.TrimSpace(string(t.buf))
}
func withStderr(prefix string, err error, stderr string) error {
if stderr == "" {
return fmt.Errorf("%s: %w", prefix, err)
}
return fmt.Errorf("%s: %w: %s", prefix, err, stderr)
}
func runError(ctx context.Context, prefix string, err error, stderr string) error {
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return withStderr(prefix+": ffmpeg command timed out", err, stderr)
}
if errors.Is(ctx.Err(), context.Canceled) {
return withStderr(prefix+": ffmpeg command canceled", err, stderr)
}
return withStderr(prefix, err, stderr)
}
func isIgnorableWaitErr(err error) bool {
if err == nil || errors.Is(err, context.Canceled) {
return true
}
// Windows can report this when canceling a command that already exited.
return strings.Contains(err.Error(), "TerminateProcess: Access is denied")
}
func isRTSP(input string) bool {
parsedURL, err := url.Parse(input)
if err != nil {
return strings.HasPrefix(strings.ToLower(input), "rtsp://") ||
strings.HasPrefix(strings.ToLower(input), "rtsps://")
}
return parsedURL.Scheme == "rtsp" || parsedURL.Scheme == "rtsps"
}
func captureTimeout(seconds int) time.Duration {
if seconds <= 0 {
return minCommandTimeout
}
const timeoutMultiplier = 6
timeout := time.Duration(seconds*timeoutMultiplier) * time.Second
timeout = max(timeout, minCommandTimeout)
return timeout
}
type cancelReadCloser struct {
io.ReadCloser
cancel context.CancelFunc
closeOnce sync.Once
}
func (c *cancelReadCloser) Close() error {
var err error
c.closeOnce.Do(func() {
err = c.ReadCloser.Close()
c.cancel()
})
if err != nil {
return fmt.Errorf("close stream: %w", err)
}
return nil
}