-
Notifications
You must be signed in to change notification settings - Fork 2
/
webm.js
46 lines (36 loc) · 1.3 KB
/
webm.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
import {lazystream, readVint} from '../util.js';
export function readMediaAttributes(input) {
const stream = lazystream(input);
const byte = stream.goto(4).take()[0];
const mime = byte === 0xa3 ? 'video/x-matroska' : 'video/webm';
const path = ['18538067', '1654ae6b', 'ae', 'e0'];
const targets = ['b0', 'ba'];
const dimensions = new Map();
stream.goto(0);
while (stream.more()) {
const header = stream.take(40);
const tagVint = readVint(header);
const sizeVint = readVint(header, tagVint.length);
const tag = header.subarray(0, tagVint.length).toString('hex');
const skip = !path.includes(tag);
const extract = targets.includes(tag);
stream.rewind(header.length - tagVint.length - sizeVint.length);
if (extract) {
const dimension = stream
.take(sizeVint.value)
.readUIntBE(0, sizeVint.value);
dimensions.set(tag, dimension);
} else if (skip) {
stream.skip(sizeVint.value);
}
if (dimensions.size === targets.length) break;
}
const result = {
width: dimensions.get('b0', 0),
height: dimensions.get('ba', 0),
size: stream.size(),
mime: mime,
};
stream.close();
return result;
}