From 57816ab604126fe79f36965f7d8d70934d490fa6 Mon Sep 17 00:00:00 2001 From: matt1432 Date: Sun, 31 Mar 2024 06:10:19 -0400 Subject: [PATCH] feat(node-syncsub): substract subs from video file if not found --- .../syncing/node-syncsub/lang-codes.ts | 1 + .../subtitles/syncing/node-syncsub/main.ts | 52 +++++++++++++++---- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/devices/nos/modules/subtitles/syncing/node-syncsub/lang-codes.ts b/devices/nos/modules/subtitles/syncing/node-syncsub/lang-codes.ts index 447efbc..599d6a9 100644 --- a/devices/nos/modules/subtitles/syncing/node-syncsub/lang-codes.ts +++ b/devices/nos/modules/subtitles/syncing/node-syncsub/lang-codes.ts @@ -232,6 +232,7 @@ export const ISO6393To1 = new Map([ ['fij', 'fj'], ['fin', 'fi'], ['fra', 'fr'], + ['fre', 'fr'], ['fry', 'fy'], ['ful', 'ff'], ['gla', 'gd'], diff --git a/devices/nos/modules/subtitles/syncing/node-syncsub/main.ts b/devices/nos/modules/subtitles/syncing/node-syncsub/main.ts index 578bfc3..3b2190d 100755 --- a/devices/nos/modules/subtitles/syncing/node-syncsub/main.ts +++ b/devices/nos/modules/subtitles/syncing/node-syncsub/main.ts @@ -38,6 +38,13 @@ function getVideoPath(files: string[]) { !f.endsWith('.srt'))[0]}`; } +function runSubSync(cmd: string[]) { + spawn('subsync', cmd, { + shell: true, + stdio: [process.stdin, process.stdout, process.stderr], + }); +} + async function main() { const files = await readDir(DIR); @@ -74,13 +81,6 @@ async function main() { const IN_FILE = `${DIR}/.srt.bak/${FILE_NAME}`; const OUT_FILE = `${DIR}/${FILE_NAME}`; - if (files.includes(FILE_NAME)) { - await mv(OUT_FILE, IN_FILE); - } - else { - // TODO: check data and extract sub - } - const cmd = [ '--cli sync', `--sub-lang ${lang}`, @@ -95,10 +95,40 @@ async function main() { `--ref '${VIDEO}'`, ]; - spawn('subsync', cmd, { - shell: true, - stdio: [process.stdin, process.stdout, process.stderr], - }); + if (files.includes(FILE_NAME)) { + await mv(OUT_FILE, IN_FILE); + runSubSync(cmd); + } + else { + let stream = data.streams.find((s) => { + return s['tags']['language'] === lang && + s.disposition!.forced === 0 && + s.codec_type === 'subtitle'; + })!.index; + + if (!stream) { + stream = data.streams.find((s) => { + return s['tags']['language'] === lang && + s.codec_type === 'subtitle'; + })!.index; + } + + if (!stream) { + console.warn(`No subtitle tracks were found for ${lang}`); + process.exit(0); + } + + spawn('ffmpeg', [ + '-i', `'${VIDEO}'`, + '-map', `0:${stream}`, `'${IN_FILE}'`, + ], { + shell: true, + stdio: [process.stdin, process.stdout, process.stderr], + + }).on('close', () => { + runSubSync(cmd); + }); + } }); }); }