-
-
Notifications
You must be signed in to change notification settings - Fork 313
/
util.js
172 lines (146 loc) · 5.37 KB
/
util.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
import { execa } from 'execa';
import assert from 'assert';
import sortBy from 'lodash-es/sortBy.js';
import fsExtra from 'fs-extra';
export function parseFps(fps) {
const match = typeof fps === 'string' && fps.match(/^([0-9]+)\/([0-9]+)$/);
if (match) {
const num = parseInt(match[1], 10);
const den = parseInt(match[2], 10);
if (den > 0) return num / den;
}
return undefined;
}
export async function readDuration(ffprobePath, p) {
const { stdout } = await execa(ffprobePath, ['-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', p]);
const parsed = parseFloat(stdout);
assert(!Number.isNaN(parsed));
return parsed;
}
export async function readFileStreams(ffprobePath, p) {
const { stdout } = await execa(ffprobePath, [
'-show_entries', 'stream', '-of', 'json', p,
]);
const json = JSON.parse(stdout);
return json.streams;
}
export async function readVideoFileInfo(ffprobePath, p) {
const streams = await readFileStreams(ffprobePath, p);
const stream = streams.find((s) => s.codec_type === 'video'); // TODO
const duration = await readDuration(ffprobePath, p);
let rotation = parseInt(stream.tags && stream.tags.rotate, 10);
// If we can't find rotation, try side_data_list
if (Number.isNaN(rotation) && Array.isArray(stream.side_data_list) && stream.side_data_list[0] && stream.side_data_list[0].rotation) {
rotation = parseInt(stream.side_data_list[0].rotation, 10);
}
return {
// numFrames: parseInt(stream.nb_frames, 10),
duration,
width: stream.width, // TODO coded_width?
height: stream.height,
framerateStr: stream.r_frame_rate,
rotation: !Number.isNaN(rotation) ? rotation : undefined,
};
}
export async function readAudioFileInfo(ffprobePath, p) {
const duration = await readDuration(ffprobePath, p);
return { duration };
}
export function toArrayInteger(buffer) {
if (buffer.length > 0) {
const data = new Uint8ClampedArray(buffer.length);
for (let i = 0; i < buffer.length; i += 1) {
data[i] = buffer[i];
}
return data;
}
return [];
}
// x264 requires multiple of 2
export const multipleOf2 = (x) => Math.round(x / 2) * 2;
export function getPositionProps({ position, width, height }) {
let originY = 'center';
let originX = 'center';
let top = height / 2;
let left = width / 2;
const margin = 0.05;
if (position === 'top') {
originY = 'top';
top = height * margin;
} else if (position === 'bottom') {
originY = 'bottom';
top = height * (1 - margin);
} else if (position === 'center') {
originY = 'center';
top = height / 2;
} else if (position === 'top-left') {
originX = 'left';
originY = 'top';
left = width * margin;
top = height * margin;
} else if (position === 'top-right') {
originX = 'right';
originY = 'top';
left = width * (1 - margin);
top = height * margin;
} else if (position === 'center-left') {
originX = 'left';
originY = 'center';
left = width * margin;
top = height / 2;
} else if (position === 'center-right') {
originX = 'right';
originY = 'center';
left = width * (1 - margin);
top = height / 2;
} else if (position === 'bottom-left') {
originX = 'left';
originY = 'bottom';
left = width * margin;
top = height * (1 - margin);
} else if (position === 'bottom-right') {
originX = 'right';
originY = 'bottom';
left = width * (1 - margin);
top = height * (1 - margin);
}
if (position && position.x != null) {
originX = position.originX || 'left';
left = width * position.x;
}
if (position && position.y != null) {
originY = position.originY || 'top';
top = height * position.y;
}
return { originX, originY, top, left };
}
export function getFrameByKeyFrames(keyframes, progress) {
if (keyframes.length < 2) throw new Error('Keyframes must be at least 2');
const sortedKeyframes = sortBy(keyframes, 't');
// TODO check that max is 1
// TODO check that all keyframes have all props
// TODO make smarter so user doesn't need to replicate non-changing props
const invalidKeyframe = sortedKeyframes.find((k, i) => {
if (i === 0) return false;
return k.t === sortedKeyframes[i - 1].t;
});
if (invalidKeyframe) throw new Error('Invalid keyframe');
let prevKeyframe = [...sortedKeyframes].reverse().find((k) => k.t < progress);
// eslint-disable-next-line prefer-destructuring
if (!prevKeyframe) prevKeyframe = sortedKeyframes[0];
let nextKeyframe = sortedKeyframes.find((k) => k.t >= progress);
if (!nextKeyframe) nextKeyframe = sortedKeyframes[sortedKeyframes.length - 1];
if (nextKeyframe.t === prevKeyframe.t) return prevKeyframe.props;
const interProgress = (progress - prevKeyframe.t) / (nextKeyframe.t - prevKeyframe.t);
return Object.fromEntries(Object.entries(prevKeyframe.props).map(([propName, prevVal]) => ([propName, prevVal + ((nextKeyframe.props[propName] - prevVal) * interProgress)])));
}
export const isUrl = (path) => /^https?:\/\//.test(path);
export const assertFileValid = async (path, allowRemoteRequests) => {
if (isUrl(path)) {
assert(allowRemoteRequests, 'Remote requests are not allowed');
return;
}
assert(await fsExtra.pathExists(path), `File does not exist ${path}`);
};
// See #16
export const checkTransition = (transition) => assert(transition == null || typeof transition === 'object', 'Transition must be an object');