Modify vtt2ass to work with new hidive API

Modifies vtt2ass to work with the new hidive API
This commit is contained in:
AnimeDL 2024-03-16 21:20:04 -07:00
parent c7addc1c1a
commit 34fa052bc2
2 changed files with 25 additions and 17 deletions

View file

@ -1030,9 +1030,8 @@ export default class Hidive implements ServiceClass {
const files: DownloadedMedia[] = [];
const variables: Variable[] = [];
let dlFailed = false;
/*const subsMargin = 0;
const videoIndex = 0;
const chosenFontSize = options.fontSize;*/
const subsMargin = 0;
const chosenFontSize = options.fontSize;
let encryptionKeys: KeyContainer[] | undefined = undefined;
if (!canDecrypt) console.warn('Decryption not enabled!');
@ -1316,27 +1315,25 @@ export default class Hidive implements ServiceClass {
sxData.path = path.join(this.cfg.dir.content, sxData.file);
sxData.language = subLang;
if(options.dlsubs.includes('all') || options.dlsubs.includes(subLang.locale)) {
/*const subs4XUrl = sub.url.split('/');
const subsXUrl = subs4XUrl[subs4XUrl.length - 1].replace(/.vtt$/, '');
const getCssContent = await this.req.getData(await this.genSubsUrl('css', subsXUrl));
const getVttContent = await this.req.getData(await this.genSubsUrl('vtt', subsXUrl));*/
const getVttContent = await this.req.getData(sub.url);
console.log(sub.url);
if (getVttContent.ok && getVttContent.res) {
//TODO: Get vtt2ass working with css format of new hidive app
/*const cssLines = [];
console.info(`Subtitle Downloaded: ${sub.url}`);
const cssLines = [];
const cssGroups = getVttContent.res.body.matchAll(/::cue(?:.(.+)\))?{([^}]+)}/g);
let defaultCss = '';
for (const cssGroup of cssGroups) {
cssLines.push(`${cssGroup[1]} {${cssGroup[2]}}`);
}*/
if (cssGroup[1]) {
cssLines.push(`${cssGroup[1]}{${defaultCss}${cssGroup[2]}}`);
} else {
defaultCss = cssGroup[2];
}
}
//vttConvert(getVttContent.res.body, false, subLang.name, fontSize);
//.replace(/#FFFF00/g, '#FFFFFF')
const sBody = getVttContent.res.body.replace(/yellow/g, '#FFFFFF');
//const sBody = vtt(undefined, chosenFontSize, getVttContent.res.body, cssLines.join('\r\n'), subsMargin, options.fontName);
const sBody = vtt(undefined, chosenFontSize, getVttContent.res.body, cssLines.join('\r\n'), subsMargin, options.fontName);
sxData.title = `${subLang.language} / ${sxData.title}`;
sxData.fonts = fontsData.assFonts(sBody) as Font[];
fs.writeFileSync(sxData.path, sBody);
console.info(`Subtitle downloaded: ${sxData.file}`);
console.info(`Subtitle converted: ${sxData.file}`);
files.push({
type: 'Subtitle',
...sxData as sxItem,

View file

@ -261,6 +261,7 @@ function convert(css: Css, vtt: Vtt[]) {
song_cap: [],
};
const linesMap: Record<string, number> = {};
let previousLine: ReturnType<typeof convertLine> | undefined = undefined;
for (const l in vtt) {
const x = convertLine(stylesMap, vtt[l]);
if (x.ind !== '' && linesMap[x.ind] !== undefined) {
@ -278,7 +279,17 @@ function convert(css: Css, vtt: Vtt[]) {
linesMap[x.ind] = events[x.type as keyof typeof events].length - 1;
}
}
/**
* What cursed code have I brought upon this land?
* This checks if a subtitle should be multi-line, and if it is, pops the just inserted
* subtitle and the previous subtitle, and merges them into a single subtitle.
*/
if (previousLine?.start == x.start && previousLine.type == x.type && previousLine.style == x.style) {
events[x.type as keyof typeof events].pop();
const previousLinePop = events[x.type as keyof typeof events].pop();
events[x.type as keyof typeof events].push(previousLinePop + '\\N'+x.text);
}
previousLine = x;
}
if (events.subtitle.length > 0) {
ass = ass.concat(