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 112148a5..b4dd66f2 100644 Binary files a/devices/nos/modules/subtitles/syncing/node-syncsub/package-lock.json and b/devices/nos/modules/subtitles/syncing/node-syncsub/package-lock.json differ diff --git a/devices/nos/modules/subtitles/syncing/node-syncsub/package.json b/devices/nos/modules/subtitles/syncing/node-syncsub/package.json index b8880e51..ed3f8425 100644 --- a/devices/nos/modules/subtitles/syncing/node-syncsub/package.json +++ b/devices/nos/modules/subtitles/syncing/node-syncsub/package.json @@ -6,5 +6,9 @@ "@typescript-eslint/parser": "^6.9.1", "eslint": "^8.52.0", "typescript": "^5.3.3" + }, + "dependencies": { + "@types/fluent-ffmpeg": "^2.1.24", + "fluent-ffmpeg": "^2.1.2" } } diff --git a/flake.nix b/flake.nix index 063d48d7..da2226df 100644 Binary files a/flake.nix and b/flake.nix differ