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,40 +16,53 @@ 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';
if (lang === 'fr') { let lang = FILE.split('.').at(-2) ?? 'en';
lang = 'fre';
}
else if (lang === 'en') {
lang = 'eng';
}
const cmd = [ if (lang === 'fr') {
'subsync --cli sync', lang = 'fre';
`--sub-lang ${lang}`, }
`--ref-lang ${lang}`, else if (lang === 'en') {
lang = 'eng';
}
`--sub-file '${FILE}'`, const availLangs = data.streams
`--out-file '${FILE}'`, .filter((s) => s.codec_type === 'audio')
`--ref-file '${VIDEO}'`, .map((s) => s['tags']['language']);
'--overwrite', const cmd = [
]; 'subsync --cli sync',
`--sub-lang ${lang}`,
// TODO: actually call the command `--ref-stream-by-lang ${availLangs.includes(lang) ? lang : other(lang)}`,
console.log(cmd); '--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) { 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.