-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepack.js
More file actions
48 lines (40 loc) · 1.4 KB
/
repack.js
File metadata and controls
48 lines (40 loc) · 1.4 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
import fs from 'fs';
import { parseString, Builder } from 'xml2js';
const filename = process.argv[2];
const output = process.argv[3] || 'updated.mpd';
if (!filename) {
throw Error('Please provide a file');
}
fs.existsSync('./output') || fs.mkdirSync('./output');
const __dirname = new URL('.', import.meta.url).pathname;
const mpdFile = fs.readFileSync(__dirname + filename, 'utf8');
function repackMPD(mpdObject) {
if (mpdObject.MPD.Period.length > 1) {
throw Error('Multiple Periods not supported');
}
const period = mpdObject.MPD.Period[0];
period.AdaptationSet.forEach((adaptationSet) => {
if (adaptationSet.SegmentTemplate.length > 1) {
throw Error(
'Multiple SegmentTemplates not supported at adaptation level'
);
}
const timescale = adaptationSet.SegmentTemplate[0].$['timescale'];
delete adaptationSet.SegmentTemplate;
adaptationSet.Representation.forEach((representation) => {
if (representation.SegmentTemplate.length > 1) {
throw Error(
'Multiple SegmentTemplates not supported at representation level'
);
}
representation.SegmentTemplate[0].$['timescale'] = timescale;
});
});
return mpdObject;
}
parseString(mpdFile, (err, jsObject) => {
const repackedMPD = repackMPD(jsObject);
const builder = new Builder();
const xml = builder.buildObject(repackedMPD);
fs.writeFileSync(`output/${output}`, xml);
});