-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
202 lines (171 loc) · 5.39 KB
/
index.js
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
/*
* index.js
* porting https://github.com/FFmpeg/FFmpeg/blob/master/libavutil/timecode.h
* https://github.com/FFmpeg/FFmpeg/blob/master/libavutil/timecode.c
*/
/**
* Adjust frame number for NTSC drop frame time code.
*
* @param framenum frame number to adjust (int)
* @param fps frame per second, 30 or 60 (int)
* @return adjusted frame number
* @warning adjustment is only valid in NTSC 29.97 and 59.94
*/
exports.av_timecode_adjust_ntsc_framenum2 = function(framenum, fps) {
/* only works for NTSC 29.97 and 59.94 */
var drop_frames = 0;
var d, m, frames_per_10mins;
if (fps == 30) {
drop_frames = 2;
frames_per_10mins = 17982;
} else if (fps == 60) {
drop_frames = 4;
frames_per_10mins = 35964;
} else {
return framenum;
}
d = Math.floor(framenum / frames_per_10mins);
m = framenum % frames_per_10mins;
return framenum + 9 * drop_frames * d + drop_frames * Math.floor((m - drop_frames) / Math.floor(frames_per_10mins / 10));
};
exports.fps_from_frame_rate = function(rate) {
if (!rate.den || !rate.num) {
return -1;
}
return Math.floor((rate.num + Math.floor(rate.den/2)) / rate.den);
};
exports.Flags = class Flags {
constructor(dropframe, max24hours, allownegative) {
///< timecode is drop frame
this.dropframe = dropframe;
///< timecode wraps after 24 hours
this.max24hours = max24hours;
///< negative time values are allowed
this.allownegative = allownegative;
}
};
exports.Timecode = class Timecode {
constructor(start, flags, rate) {
///< timecode frame start (first base frame number)
this.start = start;
///< flags such as drop frame, +24 hours support, ...
this.flags = flags;
///< frame rate in rational form
this.rate = rate;
///< frame per second; must be consistent with the rate field
this.fps = exports.fps_from_frame_rate(rate);
return this.check_timecode();
}
/**
* Load timecode string in buf.
*
* @param buf destination buffer, must be at least AV_TIMECODE_STR_SIZE long
* @param tc timecode data correctly initialized
* @param framenum frame number (int)
* @return the buf parameter
*
* @note Timecode representation can be a negative timecode and have more than
* 24 hours, but will only be honored if the flags are correctly set.
* @note The frame number is relative to tc->start.
*/
av_timecode_make_string(framenum) {
var tc = this.make(framenum);
var neg = tc.sign;
var hh = tc.hour;
var mm = tc.minute;
var ss = tc.second;
var drop = tc.drop;
var ff = tc.frame;
return `${neg}${(hh+"").padStart(2, '0')}:${(mm+"").padStart(2, '0')}:${(ss+"").padStart(2, '0')}${drop}${(ff+"").padStart(2, '0')}`;
}
make(framenum) {
var fps = this.fps;
var drop = this.flags.dropframe;
var hh, mm, ss, ff, neg = false;
framenum += this.start;
if (drop) {
framenum = exports.av_timecode_adjust_ntsc_framenum2(framenum, fps);
}
if (framenum < 0) {
framenum = -framenum;
neg = this.flags.allownegative;
}
ff = framenum % fps;
ss = Math.floor(framenum / fps) % 60;
mm = Math.floor(framenum / (fps*60)) % 60;
hh = Math.floor(framenum / (fps*3600));
if (this.flags.max24hours) {
hh = hh % 24;
}
return {
sign: neg ? '-' : '',
hour: hh,
minute: mm,
second: ss,
drop: drop ? ';' : ':',
frame: ff
};
}
check_fps() {
var i;
var supported_fps = [
24, 25, 30, 48, 50, 60, 100, 120, 150
];
for (i = 0; i < supported_fps.length; i++) {
if (this.fps == supported_fps[i]) {
return 0;
}
}
return -1;
}
check_timecode(){
if (this.fps <= 0) {
console.log("Valid timecode frame rate must be specified. Minimum value is 1.");
return 1;
}
if ((this.flags.dropframe) && this.fps != 30 && this.fps != 60) {
console.log("Drop frame is only allowed with 30000/1001 or 60000/1001 FPS\n");
return 2;
}
if (this.check_fps() < 0) {
console.log("Using non-standard frame rate %d/%d", this.rate.num, this.rate.den);
}
return 0;
}
};
/**
* Parse timecode representation (hh:mm:ss[:;.]ff).
*
* @param log_ctx a pointer to an arbitrary struct of which the first field is a
* pointer to an AVClass struct (used for av_log).
* @param tc pointer to an allocated AVTimecode
* @param rate frame rate in rational form
* @param str timecode string which will determine the frame start
* @return 0 on success, AVERROR otherwise
*/
exports.av_timecode_init_from_string = function(rate, str, flags) {
var c;
var hh, mm, ss, ff, ret;
var exp = /^([0-9][0-9]):([0-9][0-9]):([0-9][0-9])([;:])([0-9]+)$/;
var match = str.match(exp);
if(match) {
hh = parseInt(match[1], 10);
mm = parseInt(match[2], 10);
ss = parseInt(match[3], 10);
c = match[4];
ff = parseInt(match[5]);
var tc = new exports.Timecode(0, flags || new exports.Flags(c != ':', true, false), rate);
tc.start = (hh*3600 + mm*60 + ss) * tc.fps + ff;
if (tc.flags.dropframe) { /* adjust frame number */
var tmins = 60*hh + mm;
tc.start -= (tc.fps == 30 ? 2 : 4) * (tmins - Math.floor(tmins/10));
}
return tc;
} else {
throw new Error("Invalid timecode string");
}
}
/*
* alias
*/
exports.parse = exports.av_timecode_init_from_string;