-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhls_parser.js
112 lines (108 loc) · 3.04 KB
/
hls_parser.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
const fs = require("fs-extra");
const classes = require("./tagClasses.js");
class HlsParser {
constructor(inputFile, outputFile, testMode = false) {
this.inputFile = inputFile;
this.testMode = testMode;
this.outputFile = outputFile || "./out.m3u8";
}
readFile(file) {
return new Promise((resolve, reject) => {
const fileToRead = this.inputFile || file;
fs.readFile(fileToRead, "utf-8", (err, data) => {
if(err){
reject(err)
}
this.proccessFile(data);
resolve(data)
})
})
}
proccessFile(data) {
const items = data.split("#").slice(1);
this.manifest = new Manifest(data, this.outputFile);
this.getTagType(items, this.manifest.tags);
}
getTagType(items, saveObj) {
items.forEach((item, index) => {
let unidentified = true;
for(let i in classes){
if(classes[i].prototype.validator(item) === "BYTE"){
// TODO this is not a pretty solution
saveObj.push(new classes.EXTXBYTERANGE(items[index + 1], item, items[index + 1]));
unidentified = false;
break;
}else if (classes[i].prototype.validator(item) === "BYTERANGETAG") {
unidentified = false;
break;
}
else if(classes[i].prototype.validator(item)){
saveObj.push(new classes[i](item, item));
unidentified = false;
break;
}
}
if(unidentified){
saveObj.push(new classes.UnidentifiedTag(item, item));
}
});
}
getSupportedTags() {
return classes;
}
}
class Manifest {
constructor(originalManifest, outputFile) {
this.originalManifest = originalManifest;
this.tags = [];
this.outputFile = outputFile;
}
test() {
return this.write() === this.originalManifest; //
}
write() {
let writtenManifest = "";
this.tags.forEach((item, index) => {
const line = item.print();
writtenManifest += line;
});
return writtenManifest;
}
writeToFile() {
const fileToWrite = this.outputFile || "out.m3u8";
fs.writeFile(this.outputFile, this.write()).
then((err) => {
//console.log(err);
})
}
getSegmentsInsideDateRange() {
const segmentsToReplace = [];
let sectionList = [];
let cueOut, cueIn;
this.tags.forEach((item, index) => {
if(item instanceof classes.EXTXDATERANGE && item.value[" SCTE35-OUT"]){
sectionList.push(item.value[" PLANNED-DURATION"])
cueOut = true;
cueIn = false;
}else if (item instanceof classes.EXTXDATERANGE && item.value[" SCTE35-IN"]) {
cueIn = true;
cueOut = false;
}
if(cueOut && item instanceof classes.EXTINF){
sectionList.push({segment: item, index: index});
}else if (cueIn) {
cueIn = null;
segmentsToReplace.push(sectionList);
sectionList = [];
}
})
return segmentsToReplace;
}
getTags(type) {
const tags = this.tags.filter((item) => {
return item.validator(type);
});
return tags;
}
}
module.exports.HlsParser = HlsParser;