From 902364e391798051fa77dd55c3f7ea714b155105 Mon Sep 17 00:00:00 2001 From: matt1432 Date: Wed, 27 Mar 2024 01:27:03 -0400 Subject: [PATCH] feat(node-subsync): use ffprobe to check available languages --- .../nos/modules/subtitles/syncing/default.nix | 6 +- .../syncing/node-syncsub/default.nix | 7 ++- .../subtitles/syncing/node-syncsub/main.ts | 57 +++++++++++------- .../syncing/node-syncsub/package-lock.json | Bin 64244 -> 65701 bytes .../syncing/node-syncsub/package.json | 4 ++ flake.nix | Bin 9201 -> 9220 bytes 6 files changed, 47 insertions(+), 27 deletions(-) diff --git a/devices/nos/modules/subtitles/syncing/default.nix b/devices/nos/modules/subtitles/syncing/default.nix index 414b4fbc..3e64b5e0 100644 --- a/devices/nos/modules/subtitles/syncing/default.nix +++ b/devices/nos/modules/subtitles/syncing/default.nix @@ -31,9 +31,9 @@ in { ]; script = '' - find /data/anime -name '*.srt' -exec node-syncsub "{}" \; - find /data/movies -name '*.srt' -exec node-syncsub "{}" \; - find /data/tv -name '*.srt' -exec node-syncsub "{}" \; + # find /data/anime -name '*.srt' -exec node-syncsub "{}" \; + # find /data/movies -name '*.srt' -exec node-syncsub "{}" \; + # find /data/tv -name '*.srt' -exec node-syncsub "{}" \; ''; }; timers.subsync-job = { diff --git a/devices/nos/modules/subtitles/syncing/node-syncsub/default.nix b/devices/nos/modules/subtitles/syncing/node-syncsub/default.nix index 034a0033..e5f679cb 100644 --- a/devices/nos/modules/subtitles/syncing/node-syncsub/default.nix +++ b/devices/nos/modules/subtitles/syncing/node-syncsub/default.nix @@ -1,5 +1,6 @@ { buildNpmPackage, + ffmpeg, nodejs_20, subsync, typescript, @@ -8,13 +9,13 @@ buildNpmPackage { name = "node-syncsub"; src = ./.; - npmDepsHash = "sha256-kQBZ13bTMxZnv45IwyIV0cYA5tjr4KKU1cpDNx02th0= -"; + npmDepsHash = "sha256-O00VQPCUX6T+rtK3VcAibBipXFwNs4AFA3251qycPBQ="; nativeBuildInputs = [ + nodejs_20 + ffmpeg subsync typescript - nodejs_20 ]; buildPhase = '' diff --git a/devices/nos/modules/subtitles/syncing/node-syncsub/main.ts b/devices/nos/modules/subtitles/syncing/node-syncsub/main.ts index 4be7d25f..2574ddbe 100755 --- a/devices/nos/modules/subtitles/syncing/node-syncsub/main.ts +++ b/devices/nos/modules/subtitles/syncing/node-syncsub/main.ts @@ -1,4 +1,6 @@ import { readdir } from 'fs'; +import { ffprobe } from 'fluent-ffmpeg'; +import { exec } from 'child_process'; const SUB_EXT_LENGTH = 7; @@ -14,40 +16,53 @@ const main = () => { const DIR = FILE.substring(0, FILE.lastIndexOf('/')); readdir(DIR, (_, files) => { - const VIDEO = files.filter((f) => + const VIDEO = `${DIR}/${files.filter((f) => f.includes(BASE_NAME) && !f.endsWith('.nfo') && - !f.endsWith('.srt'))[0]; + !f.endsWith('.srt'))[0]}`; - let lang = FILE.split('.').at(-2); + ffprobe(VIDEO, (_e, data) => { + const other = (lang: string) => lang === 'fre' ? 'eng' : 'fre'; - if (lang === 'fr') { - lang = 'fre'; - } - else if (lang === 'en') { - lang = 'eng'; - } + let lang = FILE.split('.').at(-2) ?? 'en'; - const cmd = [ - 'subsync --cli sync', - `--sub-lang ${lang}`, - `--ref-lang ${lang}`, + if (lang === 'fr') { + lang = 'fre'; + } + else if (lang === 'en') { + lang = 'eng'; + } - `--sub-file '${FILE}'`, - `--out-file '${FILE}'`, - `--ref-file '${VIDEO}'`, + const availLangs = data.streams + .filter((s) => s.codec_type === 'audio') + .map((s) => s['tags']['language']); - '--overwrite', - ]; + const cmd = [ + 'subsync --cli sync', + `--sub-lang ${lang}`, - // TODO: actually call the command - console.log(cmd); + `--ref-stream-by-lang ${availLangs.includes(lang) ? lang : other(lang)}`, + '--ref-stream-by-type "audio"', + + `--sub '${FILE}'`, + `--out '${DIR}/${BASE_NAME}.synced.${lang.substring(0, 2)}.srt'`, + // `--out '${FILE}'`, + `--ref '${VIDEO}'`, + + // '--overwrite', + ].join(' '); + + exec(cmd, (error, stdout, stderr) => { + console.log(error); + console.log(stdout); + console.log(stderr); + }); + }); }); }; if (FILE) { main(); - process.exit(); } else { console.error('Error: no argument passed'); diff --git a/devices/nos/modules/subtitles/syncing/node-syncsub/package-lock.json b/devices/nos/modules/subtitles/syncing/node-syncsub/package-lock.json index 112148a5cc9b0a73d33f6f4b638646cb3f34797a..b4dd66f2faf15ee9893ab7363eb32a7d8a81f3a8 100644 GIT binary patch delta 802 zcmZva%WD%+6vnB22*nmEE?N<3)2OsG^USN23P~o?kjPzVe9HfEQPkMG18AV}w)D>v8Y#=2nT`qI?sJwWF$ z@C={!k8gkj<2vAN11rN-P6~AxI+~Ldteq<3HruO59BNmj2DXV>QM+N$~A$aAl0|N&LWP($8giHXdvnp)`pQ#csq( zGl&7lYdtfa5yg_tFiy3t7=+ZK!w#3DsA`Cpr2@@_Lu!Y2-Ci+I@+9BLCK450NZYC# zS4s$TOmEF2F-pg^Z3M!g&HvNk$Vx-@l7m7hICzauB2W}Od~j)g+&SnRtxcj34uo4c z_;@rwfr6)RE^`?-*W_vjnrE|2J%{TK(l9J5(_sr0EF2+Kljx;+w8p6}QMM~KS&?ZW zr^zUa!7Q(3T(=_jaz=5dOluLF=nED?m$QG3gL^-*v;Pd%6&>~jxO;JR_+tO|1rNRU N_T5dsV`sQ04;?e+E0te;* delta 12 TcmZqi_~^djImhN-99+@>DC`A(