-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimecode.go
More file actions
390 lines (335 loc) · 11.6 KB
/
timecode.go
File metadata and controls
390 lines (335 loc) · 11.6 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
package timecode
import (
"errors"
"fmt"
"regexp"
"strconv"
"time"
)
// rate represents frame rate.
type rate struct {
roundFPS int
actualFPS float64
numerator int32
denominator int32
dropFrames int
framesPer1Min int
framesPer10Min int
}
var (
// supportedNDFRates represents supported frame rates 23.976, 24, 25, 29.97NDF, 30, 48, 50, 59.94NDF, 60.
supportedNDFRates = []*rate{
{roundFPS: 10, actualFPS: 10, numerator: 10, denominator: 1, dropFrames: 0, framesPer1Min: 10 * 60, framesPer10Min: 10 * 600}, // 10
{roundFPS: 15, actualFPS: 15, numerator: 15, denominator: 1, dropFrames: 0, framesPer1Min: 15 * 60, framesPer10Min: 15 * 600}, // 15
{roundFPS: 24, actualFPS: 23.976, numerator: 24000, denominator: 1001, dropFrames: 0, framesPer1Min: 24 * 60, framesPer10Min: 24 * 600}, // 23.976
{roundFPS: 24, actualFPS: 24, numerator: 24, denominator: 1, dropFrames: 0, framesPer1Min: 24 * 60, framesPer10Min: 24 * 600}, // 24
{roundFPS: 25, actualFPS: 25, numerator: 25, denominator: 1, dropFrames: 0, framesPer1Min: 25 * 60, framesPer10Min: 25 * 600}, // 25
{roundFPS: 30, actualFPS: 29.97, numerator: 30000, denominator: 1001, dropFrames: 0, framesPer1Min: 30 * 60, framesPer10Min: 30 * 600}, // 29.97NDF (optional)
{roundFPS: 30, actualFPS: 30, numerator: 30, denominator: 1, dropFrames: 0, framesPer1Min: 30 * 60, framesPer10Min: 30 * 600}, // 30
{roundFPS: 48, actualFPS: 48, numerator: 48, denominator: 1, dropFrames: 0, framesPer1Min: 48 * 60, framesPer10Min: 48 * 600}, // 48
{roundFPS: 50, actualFPS: 50, numerator: 50, denominator: 1, dropFrames: 0, framesPer1Min: 50 * 60, framesPer10Min: 50 * 600}, // 50
{roundFPS: 60, actualFPS: 59.94, numerator: 60000, denominator: 1001, dropFrames: 0, framesPer1Min: 60 * 60, framesPer10Min: 60 * 600}, // 59.94NDF (optional)
{roundFPS: 60, actualFPS: 60, numerator: 60, denominator: 1, dropFrames: 0, framesPer1Min: 60 * 60, framesPer10Min: 60 * 600}, // 60
}
// supportedDFRates represents supported frame rates 29.97DF, 59.94DF.
supportedDFRates = []*rate{
{roundFPS: 30, actualFPS: 29.97, numerator: 30000, denominator: 1001, dropFrames: 2, framesPer1Min: 30*60 - 2, framesPer10Min: 30*600 - 9*2}, // 29.97DF (preferred)
{roundFPS: 60, actualFPS: 59.94, numerator: 60000, denominator: 1001, dropFrames: 4, framesPer1Min: 60*60 - 4, framesPer10Min: 60*600 - 9*4}, // 59.94DF (preferred)
}
// timecodePattern represents timecode pattern.
timecodePattern = regexp.MustCompile(`^([01][0-9]|2[0-3])([p:;.,])([0-5][0-9])([p:;.,])([0-5][0-9])([:;.,])([0-5][0-9])$`)
)
var (
ErrNilTimecode = errors.New("nil timecode") // error for nil timecode
ErrUnsupportedFrameRate = errors.New("unsupported frame rate") // error for unsupported frame rate
ErrMismatchFrameRate = errors.New("mismatch frame rate") // error for mismatch frame rate
ErrUnderflowFrames = errors.New("underflow frames") // error for underflow frames
ErrInvalidTimecode = errors.New("invalid timecode") // error for invalid timecode
ErrTooManyFrames = errors.New("too many frames") // error for too many frames
)
// Timecode represents timecode.
type Timecode struct {
forceAsNDF bool
sep string
lastSep string
r *rate
HH uint64
MM uint64
SS uint64
FF uint64
}
// newNDFRate returns new NDF rate.
func newNDFRate(num, den int32) (*rate, error) {
fps := float64(num) / float64(den)
for _, r := range supportedNDFRates {
if float64(r.numerator)/float64(r.denominator) == fps {
return r, nil
}
}
return nil, ErrUnsupportedFrameRate
}
// newDFRate returns new DF rate.
func newDFRate(num, den int32) (*rate, error) {
fps := float64(num) / float64(den)
for _, r := range supportedDFRates {
if float64(r.numerator)/float64(r.denominator) == fps {
return r, nil
}
}
return nil, ErrUnsupportedFrameRate
}
// newRate returns new rate.
func newRate(num, den int32, forceAsNDF bool) (*rate, error) {
ndf, err := newNDFRate(num, den)
if err != nil {
return nil, err
}
if forceAsNDF {
return ndf, err
}
df, err := newDFRate(num, den)
if err != nil {
if errors.Is(err, ErrUnsupportedFrameRate) {
return ndf, nil
}
return nil, err
}
return df, nil
}
// IsSupportedFrameRate returns whether frame rate is supported.
func IsSupportedFrameRate(num, den int32) bool {
_, err := newNDFRate(num, den)
return err == nil
}
// IsRepresentableFramesOptionParam represents IsRepresentableFrames option parameter.
type IsRepresentableFramesOptionParam struct {
ForceAsNDF bool
}
// IsRepresentableFramesOption represents IsRepresentableFrames option.
type IsRepresentableFramesOption func(*IsRepresentableFramesOptionParam)
// newIsRepresentableFramesOptionParam returns new IsRepresentableFramesOptionParam.
func newIsRepresentableFramesOptionParam() IsRepresentableFramesOptionParam {
return IsRepresentableFramesOptionParam{
ForceAsNDF: true, // if frame rate is DF or NDF, assume NDF
}
}
// applyIsRepresentableFramesOption applies IsRepresentableFramesOption to IsRepresentableFramesOptionParam.
func (p *IsRepresentableFramesOptionParam) applyIsRepresentableFramesOption(opts ...IsRepresentableFramesOption) {
for _, opt := range opts {
opt(p)
}
}
// IsRepresentableFrames returns whether frames is representable.
func IsRepresentableFrames(frames uint64, num, den int32, opts ...IsRepresentableFramesOption) bool {
p := newIsRepresentableFramesOptionParam()
p.applyIsRepresentableFramesOption(opts...)
r, err := newRate(num, den, p.ForceAsNDF)
if err != nil {
return false
}
return r.isRepresentableFrames(frames)
}
// TimecodeOptionParam represents timecode option parameter.
type TimecodeOptionParam struct {
ForceAsNDF bool
Sep string
LastSep string
}
// TimecodeOption represents timecode option.
type TimecodeOption func(*TimecodeOptionParam)
// newTimecodeOptionParam returns new TimecodeOptionParam.
func newTimecodeOptionParam() TimecodeOptionParam {
return TimecodeOptionParam{
ForceAsNDF: false, // true, if frame rate is DF or NDF, assume NDF. otherwise, assume DF
Sep: ":",
LastSep: ":",
}
}
// applyTimecodeOption applies TimecodeOption to TimecodeOptionParam.
func (p *TimecodeOptionParam) applyTimecodeOption(opts ...TimecodeOption) {
for _, opt := range opts {
opt(p)
}
}
// NewTimecode returns new Timecode.
func NewTimecode(frames uint64, num, den int32, opts ...TimecodeOption) (*Timecode, error) {
p := newTimecodeOptionParam()
p.applyTimecodeOption(opts...)
r, err := newRate(num, den, p.ForceAsNDF)
if err != nil {
return nil, err
}
lastSep := p.LastSep
if r.dropFrames == 0 {
lastSep = p.Sep
}
tc, err := Reset(&Timecode{
forceAsNDF: p.ForceAsNDF,
sep: p.Sep,
lastSep: lastSep,
r: r,
}, frames)
if err != nil {
return nil, err
}
return tc, nil
}
// TimecodeOptionParam represents timecode option parameter.
type ParseTimecodeOptionParam struct {
ForceAsNDF bool
Sep string
LastSep string
}
// ParseTimecodeOption represents parse timecode option.
type ParseTimecodeOption func(*ParseTimecodeOptionParam)
// newParseTimecodeOptionParam returns new ParseTimecodeOptionParam.
func newParseTimecodeOptionParam() ParseTimecodeOptionParam {
return ParseTimecodeOptionParam{
ForceAsNDF: false, // if frame rate is 29.97 or 59.94, assume NDF. otherwise, assume DF
}
}
// applyParseTimecodeOption applies ParseTimecodeOption to ParseTimecodeOptionParam.
func (p *ParseTimecodeOptionParam) applyParseTimecodeOption(opts ...ParseTimecodeOption) {
for _, opt := range opts {
opt(p)
}
}
// ParseTimecode returns new Timecode from formatted string.
func ParseTimecode(s string, num, den int32, opts ...ParseTimecodeOption) (*Timecode, error) {
p := newParseTimecodeOptionParam()
p.applyParseTimecodeOption(opts...)
r, err := newRate(num, den, p.ForceAsNDF)
if err != nil {
return nil, err
}
// pattern: HH Sep1 MM Sep2 SS Sep3 FF
// match : 1 2 3 4 5 6 7
match := timecodePattern.FindStringSubmatch(s)
if len(match) != 8 || match[2] != match[4] {
return nil, ErrInvalidTimecode
}
hh, _ := strconv.Atoi(match[1])
sep := match[2]
mm, _ := strconv.Atoi(match[3])
ss, _ := strconv.Atoi(match[5])
lastSep := match[6]
ff, _ := strconv.Atoi(match[7])
if ff < r.dropFrames && mm%10 != 0 {
ff = r.dropFrames
}
if r.dropFrames == 0 {
lastSep = sep
}
return &Timecode{
forceAsNDF: p.ForceAsNDF,
sep: sep,
lastSep: lastSep,
r: r,
HH: uint64(hh),
MM: uint64(mm),
SS: uint64(ss),
FF: uint64(ff),
}, nil
}
// Reset returns new Timecode from Timecode and frames.
func Reset(tc *Timecode, frames uint64) (*Timecode, error) {
if tc == nil {
return nil, ErrNilTimecode
}
new := *tc
if !new.r.isRepresentableFrames(frames) {
return nil, ErrTooManyFrames
}
d := frames / uint64(new.r.framesPer10Min)
m := frames % uint64(new.r.framesPer10Min)
df := uint64(new.r.dropFrames)
f := frames + 9*df*d
if m > df {
f += df * ((m - df) / uint64(new.r.framesPer1Min))
}
fps := uint64(new.r.roundFPS)
new.FF = f % fps
new.SS = f / fps % 60
new.MM = f / (fps * 60) % 60
new.HH = f / (fps * 3600)
return &new, nil
}
// equal returns whether rate is equal.
func (r *rate) equal(other *rate) bool {
if r == nil || other == nil {
return false
}
return r.numerator == other.numerator && r.denominator == other.denominator && r.dropFrames == other.dropFrames
}
// isRepresentableFrames returns whether frames is representable.
func (r *rate) isRepresentableFrames(frames uint64) bool {
return frames < uint64(24*6*r.framesPer10Min)
}
// Frames returns number of frames.
func (tc *Timecode) Frames() uint64 {
var frames uint64
frames += tc.HH * 3600 * uint64(tc.r.roundFPS)
frames += tc.MM * 60 * uint64(tc.r.roundFPS)
frames += tc.SS * uint64(tc.r.roundFPS)
frames += tc.FF
framesPer10Min := uint64(tc.r.roundFPS) * 60 * 10
framesPer1Min := framesPer10Min / 10
var df uint64
df += (frames / framesPer10Min) * uint64(tc.r.dropFrames) * 9
df += (frames % framesPer10Min) / framesPer1Min * uint64(tc.r.dropFrames)
return frames - df
}
// Duration returns duration from zero-origin.
func (tc *Timecode) Duration() time.Duration {
return time.Duration((float64(tc.Frames()) / float64(tc.r.actualFPS)) * float64(time.Second))
}
// Framerate denominator.
func (tc *Timecode) FramerateDenominator() int32 {
return tc.r.denominator
}
// Framerate numerator.
func (tc *Timecode) FramerateNumerator() int32 {
return tc.r.numerator
}
// Add Timecode and Timecode and return new Timecode.
func (tc *Timecode) Add(other *Timecode) (*Timecode, error) {
if !tc.r.equal(other.r) {
return nil, ErrMismatchFrameRate
}
return Reset(tc, tc.Frames()+other.Frames())
}
// Sub Timecode and Timecode and return new Timecode.
func (tc *Timecode) Sub(other *Timecode) (*Timecode, error) {
if !tc.r.equal(other.r) {
return nil, ErrMismatchFrameRate
}
if tc.Frames() < other.Frames() {
return nil, ErrUnderflowFrames
}
return Reset(tc, tc.Frames()-other.Frames())
}
// Add Timecode and frames and return new Timecode.
func (tc *Timecode) AddFrames(frames uint64) (*Timecode, error) {
return Reset(tc, tc.Frames()+frames)
}
// Sub Timecode and frames and return new Timecode.
func (tc *Timecode) SubFrames(frames uint64) (*Timecode, error) {
if tc.Frames() < frames {
return nil, ErrUnderflowFrames
}
return Reset(tc, tc.Frames()-frames)
}
// String returns Timecode formatted string.
// e.g. 01:23:45:28
func (tc *Timecode) String() string {
return fmt.Sprintf(
"%02d%s%02d%s%02d%s%02d",
tc.HH,
tc.sep,
tc.MM,
tc.sep,
tc.SS,
tc.lastSep,
tc.FF,
)
}