This repository has been archived by the owner on Aug 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions_main.go
117 lines (106 loc) · 4.91 KB
/
options_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
package ffmpeg
import (
"fmt"
"regexp"
"strconv"
"time"
)
// FLAG SPEC ARGS AFFECTS IMPL
// stream_loop false [number] [input] [X]
// itsoffset false [offset] [input] [ ]
// dump_attachment true [filename] [input] [ ]
// muxdelay false [seconds] [input] [ ]
// muxpreload false [seconds] [input] [ ]
// f false [fmt] [input output] [X]
// c true [codec] [input output] [X]
// codec true [codec] [input output] [X]
// t false [duration] [input output] [X]
// ss false [position] [input output] [ ]
// sseof false [position] [input output] [ ]
// to false [position] [output] [ ]
// fs false [limit_size] [output] [ ]
// timestamp false [date] [output] [ ]
// metadata true [key=value] [output] [ ]
// disposition true [value] [output] [ ]
// target false [type] [output] [ ]
// dframes false [number] [output] [ ]
// frames true [framecount] [output] [ ]
// q true [q] [output] [ ]
// qscale true [q] [output] [ ]
// filter true [filtergraph] [output] [ ]
// filter_script true [filename] [output] [ ]
// pre true [preset_name] [output] [ ]
// attach false [filename] [output] [ ]
// rc_override true [override] [output] [ ]
// top true [n] [output] [ ]
// shortest false [] [output] [ ]
// streamid false [output-stream-index:new-value] [output] [ ]
// WithStreamLoop sets the number of times input stream shall be looped
//
// loop 0 means no loop, loop -1 means infinite loop
func WithStreamLoop(loop int) FileOption {
return func(f *File) error {
if f.typ == fileTypeInput {
f.options = append(f.options, []string{"-stream_loop", strconv.Itoa(loop)}...)
} else {
return fmt.Errorf("unable to apply -stream_loop flag: not input file")
}
return nil
}
}
// WithFormat forces the the input or output file format
//
// The format is normally auto detected for input files and guessed
// from the file extension for output files, so this option is
// not needed in most cases.
func WithFormat(ff FileFormat) FileOption {
return func(f *File) error {
f.options = append(f.options, []string{"-f", ff.String()}...)
return nil
}
}
// WithCodec selects an encoder (when used before an output file)
// or a decoder (when used before an input file) for one or more streams
func WithCodec(stream StreamSpecifier, codec Codec) FileOption {
return func(f *File) error {
f.options = append(f.options, []string{"-c" + stream.String(), codec.String()}...)
return nil
}
}
var regexpDuration = regexp.MustCompile(`((\d+)h)?((\d+)m)?(([0-9.]+)s)?`)
// WithDuration when used as an input option limits the duration of the data read from the input file
// and when used as an output option limits stops writing the ouput after its duration reaches duration.
func WithDuration(dur time.Duration) FileOption {
create := func(dur time.Duration) string {
matches := regexpDuration.FindAllStringSubmatch(dur.String(), -1)
hour, _ := strconv.ParseInt(matches[0][2], 10, 32)
minute, _ := strconv.ParseInt(matches[0][4], 10, 32)
seconds, _ := strconv.ParseFloat(matches[0][6], 32)
return fmt.Sprintf("%02d:%02d:%f", hour, minute, seconds)
}
return func(f *File) error {
f.options = append(f.options, []string{"-t", create(dur)}...)
return nil
}
}
// WithFileSizeLimit sets the file size limit, expressed in bytes
func WithFileSizeLimit(limit int) FileOption {
return func(f *File) error {
f.options = append(f.options, []string{"-fs", strconv.Itoa(limit)}...)
return nil
}
}
// WithTimestamp sets the recording timestamp in the container
func WithTimestamp(date time.Time) FileOption {
create := func(date time.Time) string {
return ""
}
return func(f *File) error {
if f.typ == fileTypeOutput {
f.options = append(f.options, []string{"-timestamp", create(date)}...)
} else {
return fmt.Errorf("unable to apply -timestamp flag: not output file")
}
return nil
}
}