Skip to content

Commit 7de7798

Browse files
committed
support netflix
1 parent d185740 commit 7de7798

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/subtitle/netflix.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ export default class Netflix extends BaseVideo {
3939
if (this.checkPlayerCaptionOff()) {
4040
console.log("caption is off");
4141
} else {
42-
console.log(this.getPlayer().getTextTrack())
4342
var videoId = this.getVideoId();
4443
this.requestTrack(lang, videoId); //turn on caption on specified lang
4544
}
@@ -156,6 +155,8 @@ export default class Netflix extends BaseVideo {
156155
}
157156

158157
static parseSubtitle(sub, videoId) {
158+
var styles ={}
159+
var regions = {}
159160
const concatSubtitles = [];
160161
const parser = new DOMParser();
161162
const xmlDoc = parser.parseFromString(sub, "text/xml");
@@ -177,18 +178,28 @@ export default class Netflix extends BaseVideo {
177178
});
178179
}
179180

181+
const styling = xmlDoc.getElementsByTagName("styling")[0];
182+
if (styling) {
183+
var styles = Array.from(styling.getElementsByTagName("style"));
184+
styles.forEach((style) => {
185+
const newId = `${style.getAttribute("xml:id")}_${lang}`;
186+
style.setAttribute("xml:id", newId);
187+
});
188+
}
189+
180190
// parse subtitles
181191
for (let i = 0; i < subtitles.length; i++) {
182192
const subtitle = subtitles[i];
183193
const start = parseInt(subtitle.getAttribute("begin").replace("t", ""));
184194
const end = parseInt(subtitle.getAttribute("end").replace("t", ""));
185195
const text = subtitle.textContent;
186196
const region = subtitle.getAttribute("region") + "_" + lang;
197+
const style = subtitle.getElementsByTagName("span")[0]?.getAttribute("style")+"_" + lang;
187198
var prev = concatSubtitles?.[concatSubtitles.length - 1];
188199
if (prev && prev.start === start && prev.end === end) {
189200
prev.text += " " + text;
190201
} else {
191-
concatSubtitles.push({ start, end, text, region });
202+
concatSubtitles.push({ start, end, text, region, style });
192203
}
193204
}
194205

@@ -197,6 +208,7 @@ export default class Netflix extends BaseVideo {
197208
lang,
198209
subtitles: concatSubtitles,
199210
regions,
211+
styles,
200212
};
201213
if (!this.sub[videoId]) {
202214
this.sub[videoId] = {};
@@ -217,6 +229,12 @@ export default class Netflix extends BaseVideo {
217229
layout?.appendChild(region);
218230
});
219231

232+
// Merge styles from sub2 into sub1
233+
const styling = sub1Meta?.xmlDoc?.getElementsByTagName("styling")?.[0];
234+
sub2Meta?.styles?.forEach((style) => {
235+
styling?.appendChild(style);
236+
});
237+
220238
// fix mismatch length between sub1 sub2
221239
for (let [i, sub1Line] of sub1.entries()) {
222240
var line1 = sub1Line;
@@ -260,6 +278,7 @@ export default class Netflix extends BaseVideo {
260278
p.setAttribute("begin", `${sub.start}t`);
261279
p.setAttribute("end", `${sub.end}t`);
262280
p.setAttribute("region", sub.region);
281+
p.setAttribute("style", sub.style);
263282

264283
const span = xmlDoc.createElement("span");
265284
span.setAttribute("style", "style0");

0 commit comments

Comments
 (0)