From 17de9416d0a5f691a200fe2e7e9ae8cd4ab5b4e5 Mon Sep 17 00:00:00 2001 From: Daniel Haller Date: Wed, 24 Apr 2024 04:55:26 +0200 Subject: [PATCH] added ass pos resampler --- src/api/services/subs.ts | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/api/services/subs.ts b/src/api/services/subs.ts index 8f8d333..11a2250 100644 --- a/src/api/services/subs.ts +++ b/src/api/services/subs.ts @@ -18,7 +18,9 @@ export async function downloadCRSub( const stream = fs.createWriteStream(path) const response = await fetch(sub.url) - var parsedASS = parse(await response.text()) + var resampledSubs = resamplePOSSubtitle(await response.text()) + + var parsedASS = parse(resampledSubs) parsedASS.info['Original Script'] = 'crd [https://github.com/stratuma/]' @@ -27,7 +29,10 @@ export async function downloadCRSub( for (const s of parsedASS.styles.style) { if (s.Fontname === 'Arial') { - (s.Fontsize = "54"), (s.Outline = "4"); + (s.Fontsize = "54"), (s.Outline = "4"), (s.MarginV = "60"); + } + if (s.Name === 'TypePlaceholder') { + (s.Fontsize = "57"), (s.Outline = "4"), (s.MarginL = "30"), (s.MarginR = "30"), (s.MarginV = "60"); } } @@ -41,6 +46,32 @@ export async function downloadCRSub( return path } +function resamplePOSSubtitle(subtitle: string) { + let lines = subtitle.split('\n'); + + for (let i = 0; i < lines.length; i++) { + let line = lines[i]; + + if (line.includes("\\pos(")) { + let posMatch = line.match(/\\pos\((\d+),(\d+)\)/); + if (posMatch) { + let oldX = parseInt(posMatch[1]); + let oldY = parseInt(posMatch[2]); + + let newX = Math.round((oldX / 640) * 1920); + let newY = Math.round((oldY / 360) * 1080); + + let newPos = `\\pos(${newX},${newY})`; + + line = line.replace(/\\pos\(\d+,\d+\)/, newPos); + lines[i] = line; + } + } + } + + return lines.join('\n'); +} + export async function downloadADNSub(link: string, dir: string, secret: string) { var templateASS = `[Script Info] ; Script generated by Aegisub 3.2.2