feat(node-subsync): use ffprobe to check available languages
All checks were successful
Discord / discord commits (push) Has been skipped

This commit is contained in:
matt1432 2024-03-27 01:27:03 -04:00
parent 0653a897d7
commit 902364e391
6 changed files with 47 additions and 27 deletions

View file

@ -31,9 +31,9 @@ in {
]; ];
script = '' script = ''
find /data/anime -name '*.srt' -exec node-syncsub "{}" \; # find /data/anime -name '*.srt' -exec node-syncsub "{}" \;
find /data/movies -name '*.srt' -exec node-syncsub "{}" \; # find /data/movies -name '*.srt' -exec node-syncsub "{}" \;
find /data/tv -name '*.srt' -exec node-syncsub "{}" \; # find /data/tv -name '*.srt' -exec node-syncsub "{}" \;
''; '';
}; };
timers.subsync-job = { timers.subsync-job = {

View file

@ -1,5 +1,6 @@
{ {
buildNpmPackage, buildNpmPackage,
ffmpeg,
nodejs_20, nodejs_20,
subsync, subsync,
typescript, typescript,
@ -8,13 +9,13 @@
buildNpmPackage { buildNpmPackage {
name = "node-syncsub"; name = "node-syncsub";
src = ./.; src = ./.;
npmDepsHash = "sha256-kQBZ13bTMxZnv45IwyIV0cYA5tjr4KKU1cpDNx02th0= npmDepsHash = "sha256-O00VQPCUX6T+rtK3VcAibBipXFwNs4AFA3251qycPBQ=";
";
nativeBuildInputs = [ nativeBuildInputs = [
nodejs_20
ffmpeg
subsync subsync
typescript typescript
nodejs_20
]; ];
buildPhase = '' buildPhase = ''

View file

@ -1,4 +1,6 @@
import { readdir } from 'fs'; import { readdir } from 'fs';
import { ffprobe } from 'fluent-ffmpeg';
import { exec } from 'child_process';
const SUB_EXT_LENGTH = 7; const SUB_EXT_LENGTH = 7;
@ -14,12 +16,15 @@ const main = () => {
const DIR = FILE.substring(0, FILE.lastIndexOf('/')); const DIR = FILE.substring(0, FILE.lastIndexOf('/'));
readdir(DIR, (_, files) => { readdir(DIR, (_, files) => {
const VIDEO = files.filter((f) => const VIDEO = `${DIR}/${files.filter((f) =>
f.includes(BASE_NAME) && f.includes(BASE_NAME) &&
!f.endsWith('.nfo') && !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';
let lang = FILE.split('.').at(-2) ?? 'en';
if (lang === 'fr') { if (lang === 'fr') {
lang = 'fre'; lang = 'fre';
@ -28,26 +33,36 @@ const main = () => {
lang = 'eng'; lang = 'eng';
} }
const availLangs = data.streams
.filter((s) => s.codec_type === 'audio')
.map((s) => s['tags']['language']);
const cmd = [ const cmd = [
'subsync --cli sync', 'subsync --cli sync',
`--sub-lang ${lang}`, `--sub-lang ${lang}`,
`--ref-lang ${lang}`,
`--sub-file '${FILE}'`, `--ref-stream-by-lang ${availLangs.includes(lang) ? lang : other(lang)}`,
`--out-file '${FILE}'`, '--ref-stream-by-type "audio"',
`--ref-file '${VIDEO}'`,
'--overwrite', `--sub '${FILE}'`,
]; `--out '${DIR}/${BASE_NAME}.synced.${lang.substring(0, 2)}.srt'`,
// `--out '${FILE}'`,
`--ref '${VIDEO}'`,
// TODO: actually call the command // '--overwrite',
console.log(cmd); ].join(' ');
exec(cmd, (error, stdout, stderr) => {
console.log(error);
console.log(stdout);
console.log(stderr);
});
});
}); });
}; };
if (FILE) { if (FILE) {
main(); main();
process.exit();
} }
else { else {
console.error('Error: no argument passed'); console.error('Error: no argument passed');

View file

@ -6,5 +6,9 @@
"@typescript-eslint/parser": "^6.9.1", "@typescript-eslint/parser": "^6.9.1",
"eslint": "^8.52.0", "eslint": "^8.52.0",
"typescript": "^5.3.3" "typescript": "^5.3.3"
},
"dependencies": {
"@types/fluent-ffmpeg": "^2.1.24",
"fluent-ffmpeg": "^2.1.2"
} }
} }

BIN
flake.nix

Binary file not shown.