feat(node-sub): improve support for series
All checks were successful
Discord / discord commits (push) Has been skipped

This commit is contained in:
matt1432 2024-04-24 15:23:10 -04:00
parent d625b740be
commit b7b25ea3cc

View file

@ -21,11 +21,12 @@ const SPAWN_OPTS = {
* @param languages a comma-separated list of languages (3 letters) to sync the subtitles
*/
const DIR = process.argv[2];
let langs = process.argv[3].split(',');
const LANGS = process.argv[3]?.split(',');
let langs: string[];
// Check if there are 2 params
if (DIR && langs) {
if (DIR && LANGS) {
main();
}
else {
@ -33,9 +34,9 @@ else {
process.exit(1);
}
const escapePath = (p: string) => p.replaceAll("'", "'\\''");
const escapePath = (p: string): string => p.replaceAll("'", "'\\''");
function getVideoPath(files: string[]) {
function getVideoPath(files: string[]): string[] {
const fileName = DIR.split('/').at(-1) ?? '';
const videoFiles = files.filter((f) =>
@ -46,10 +47,10 @@ function getVideoPath(files: string[]) {
process.exit(0);
}
return `${DIR}/${videoFiles[0]}`;
return videoFiles.map((file) => `${DIR}/${file}`);
}
async function backupSubs(files: string[]) {
async function backupSubs(files: string[], base: string) {
// Check if backup folder already exists and create it if not
if (!files.some((f) => f.endsWith('.srt.bak'))) {
await mkdir(`${DIR}/.srt.bak`);
@ -60,7 +61,9 @@ async function backupSubs(files: string[]) {
// Remove synced subtitles from the list to sync
// langs - backups
langs = langs.filter((n) => !backups
langs = langs
.filter((n) => !backups
.filter((l) => l.includes(base))
.some((s) => {
const l2 = s.split('.').at(-2) ?? '';
const l3 = ISO6391To3.get(l2);
@ -70,8 +73,7 @@ async function backupSubs(files: string[]) {
}
if (langs.length === 0) {
console.warn('Subtitles have already been synced');
process.exit(0);
console.warn(`Subtitles have already been synced for ${base}`);
}
}
@ -93,18 +95,20 @@ async function runSubSync(
async function main() {
const files = await readDir(DIR);
const VIDEO = getVideoPath(files);
const BASE_NAME = VIDEO.split('/').at(-1)?.replace(/\.[^.]*$/, '');
const VIDEO_FILES = getVideoPath(files);
backupSubs(files);
VIDEO_FILES.forEach((VIDEO) => {
langs = LANGS;
const BASE_NAME = VIDEO.split('/').at(-1)!.replace(/\.[^.]*$/, '');
backupSubs(files, BASE_NAME);
// ffprobe the video file to see available audio tracks
ffProbe(VIDEO, (_e, data) => {
if (!data?.streams) {
console.error('Couldn\'t find streams in video file');
process.exit(0);
}
else {
const AVAIL_LANGS = data.streams
.filter((s) => s.codec_type === 'audio')
.map((s) => s['tags'] && s['tags']['language']);
@ -186,5 +190,7 @@ async function main() {
}
}
});
}
});
});
}