Format all code files using the new configuration

This commit is contained in:
Maurice Oegerli
2021-04-07 13:45:40 +02:00
parent bbea6116af
commit 8eed88f17f
14 changed files with 323 additions and 525 deletions

View File

@@ -1,189 +1,188 @@
// Based of https://github.com/GilgusMaximus/yt-dash-manifest-generator/blob/master/src/DashGenerator.js
const xml = require('xml-js')
const xml = require("xml-js");
const DashUtils = {
generate_dash_file_from_formats(VideoFormats, VideoLength) {
const generatedJSON = this.generate_xmljs_json_from_data(VideoFormats, VideoLength)
return xml.json2xml(generatedJSON)
const generatedJSON = this.generate_xmljs_json_from_data(VideoFormats, VideoLength);
return xml.json2xml(generatedJSON);
},
generate_xmljs_json_from_data(VideoFormatArray, VideoLength) {
const convertJSON = {
"declaration": {
"attributes": {
"version": "1.0",
"encoding": "utf-8"
}
declaration: {
attributes: {
version: "1.0",
encoding: "utf-8",
},
},
"elements": [
elements: [
{
"type": "element",
"name": "MPD",
"attributes": {
"xmlns": "urn:mpeg:dash:schema:mpd:2011",
"profiles": "urn:mpeg:dash:profile:full:2011",
"minBufferTime": "PT1.5S",
"type": "static",
"mediaPresentationDuration": `PT${VideoLength}S`
type: "element",
name: "MPD",
attributes: {
xmlns: "urn:mpeg:dash:schema:mpd:2011",
profiles: "urn:mpeg:dash:profile:full:2011",
minBufferTime: "PT1.5S",
type: "static",
mediaPresentationDuration: `PT${VideoLength}S`,
},
"elements": [
elements: [
{
"type": "element",
"name": "Period",
"elements": this.generate_adaptation_set(VideoFormatArray)
}
]
}
]
}
return convertJSON
type: "element",
name: "Period",
elements: this.generate_adaptation_set(VideoFormatArray),
},
],
},
],
};
return convertJSON;
},
generate_adaptation_set(VideoFormatArray) {
const adaptationSets = []
const mimeTypes = []
const mimeObjects = [[]]
const adaptationSets = [];
const mimeTypes = [];
const mimeObjects = [[]];
// sort the formats by mime types
VideoFormatArray.forEach((videoFormat) => {
VideoFormatArray.forEach(videoFormat => {
// the dual formats should not be used
if (videoFormat.mimeType.indexOf("video") != -1 && !videoFormat.videoOnly) {
return
return;
}
// if these properties are not available, then we skip it because we cannot set these properties
//if (!(videoFormat.hasOwnProperty('initRange') && videoFormat.hasOwnProperty('indexRange'))) {
// return
//}
const mimeType = videoFormat.mimeType
const mimeTypeIndex = mimeTypes.indexOf(mimeType)
const mimeType = videoFormat.mimeType;
const mimeTypeIndex = mimeTypes.indexOf(mimeType);
if (mimeTypeIndex > -1) {
mimeObjects[mimeTypeIndex].push(videoFormat)
mimeObjects[mimeTypeIndex].push(videoFormat);
} else {
mimeTypes.push(mimeType)
mimeObjects.push([])
mimeObjects[mimeTypes.length - 1].push(videoFormat)
mimeTypes.push(mimeType);
mimeObjects.push([]);
mimeObjects[mimeTypes.length - 1].push(videoFormat);
}
})
});
// for each MimeType generate a new Adaptation set with Representations as sub elements
for (let i = 0; i < mimeTypes.length; i++) {
let isVideoFormat = false
let isVideoFormat = false;
const adapSet = {
"type": "element",
"name": "AdaptationSet",
"attributes": {
"id": i,
"mimeType": mimeTypes[i],
"startWithSAP": "1",
"subsegmentAlignment": "true"
type: "element",
name: "AdaptationSet",
attributes: {
id: i,
mimeType: mimeTypes[i],
startWithSAP: "1",
subsegmentAlignment: "true",
},
"elements": []
}
elements: [],
};
if (!mimeTypes[i].includes("audio")) {
adapSet.attributes.scanType = "progressive"
isVideoFormat = true
adapSet.attributes.scanType = "progressive";
isVideoFormat = true;
}
mimeObjects[i].forEach((format) => {
mimeObjects[i].forEach(format => {
if (isVideoFormat) {
adapSet.elements.push(this.generate_representation_video(format))
adapSet.elements.push(this.generate_representation_video(format));
} else {
adapSet.elements.push(this.generate_representation_audio(format))
adapSet.elements.push(this.generate_representation_audio(format));
}
})
adaptationSets.push(adapSet)
});
adaptationSets.push(adapSet);
}
return adaptationSets
}, generate_representation_audio(Format) {
const representation =
{
"type": "element",
"name": "Representation",
"attributes": {
"id": Format.itag,
"codecs": Format.codec,
"bandwidth": Format.bitrate
return adaptationSets;
},
generate_representation_audio(Format) {
const representation = {
type: "element",
name: "Representation",
attributes: {
id: Format.itag,
codecs: Format.codec,
bandwidth: Format.bitrate,
},
"elements": [
elements: [
{
"type": "element",
"name": "AudioChannelConfiguration",
"attributes": {
"schemeIdUri": "urn:mpeg:dash:23003:3:audio_channel_configuration:2011",
"value": "2"
type: "element",
name: "AudioChannelConfiguration",
attributes: {
schemeIdUri: "urn:mpeg:dash:23003:3:audio_channel_configuration:2011",
value: "2",
},
},
{
"type": "element",
"name": "BaseURL",
"elements": [
type: "element",
name: "BaseURL",
elements: [
{
"type": "text",
"text": Format.url
}
]
type: "text",
text: Format.url,
},
],
},
{
"type": "element",
"name": "SegmentBase",
"attributes": {
"indexRange": `${Format.indexStart}-${Format.indexEnd}`
type: "element",
name: "SegmentBase",
attributes: {
indexRange: `${Format.indexStart}-${Format.indexEnd}`,
},
"elements": [
elements: [
{
"type": "element",
"name": "Initialization",
"attributes": {
"range": `${Format.initStart}-${Format.initEnd}`
}
}
]
}
]
}
return representation
type: "element",
name: "Initialization",
attributes: {
range: `${Format.initStart}-${Format.initEnd}`,
},
},
],
},
],
};
return representation;
},
generate_representation_video(Format) {
const representation =
{
"type": "element",
"name": "Representation",
"attributes": {
"id": Format.itag,
"codecs": Format.codec,
"bandwidth": Format.bitrate,
"width": Format.width,
"height": Format.height,
"maxPlayoutRate": "1",
"frameRate": Format.fps
const representation = {
type: "element",
name: "Representation",
attributes: {
id: Format.itag,
codecs: Format.codec,
bandwidth: Format.bitrate,
width: Format.width,
height: Format.height,
maxPlayoutRate: "1",
frameRate: Format.fps,
},
"elements": [
elements: [
{
"type": "element",
"name": "BaseURL",
"elements": [
type: "element",
name: "BaseURL",
elements: [
{
"type": "text",
"text": Format.url
}
]
type: "text",
text: Format.url,
},
],
},
{
"type": "element",
"name": "SegmentBase",
"attributes": {
"indexRange": `${Format.indexStart}-${Format.indexEnd}`
type: "element",
name: "SegmentBase",
attributes: {
indexRange: `${Format.indexStart}-${Format.indexEnd}`,
},
"elements": [
elements: [
{
"type": "element",
"name": "Initialization",
"attributes": {
"range": `${Format.initStart}-${Format.initEnd}`
}
}
]
}
]
}
return representation
}
}
type: "element",
name: "Initialization",
attributes: {
range: `${Format.initStart}-${Format.initEnd}`,
},
},
],
},
],
};
return representation;
},
};
export default DashUtils;