2024-03-31 03:44:23 -04:00
|
|
|
import {
|
|
|
|
mkdir,
|
|
|
|
readdir as readDir,
|
|
|
|
rename as mv,
|
|
|
|
} from 'fs/promises';
|
|
|
|
|
|
|
|
import { ffprobe as ffProbe } from 'fluent-ffmpeg';
|
2024-03-31 22:06:17 -04:00
|
|
|
import { spawnSync as spawn } from 'child_process';
|
2024-03-26 11:31:59 -04:00
|
|
|
|
2024-03-31 03:44:23 -04:00
|
|
|
import { ISO6391To3, ISO6393To1 } from './lang-codes';
|
2024-03-26 11:31:59 -04:00
|
|
|
|
2024-03-31 22:06:17 -04:00
|
|
|
const SPAWN_OPTS = {
|
|
|
|
shell: true,
|
|
|
|
stdio: [process.stdin, process.stdout, process.stderr],
|
|
|
|
};
|
2024-03-26 11:31:59 -04:00
|
|
|
|
2024-03-31 03:44:23 -04:00
|
|
|
/**
|
|
|
|
* These are the cli arguments
|
|
|
|
*
|
|
|
|
* @param directory the directory in which we want to sync the subtitles
|
|
|
|
* @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(',');
|
2024-03-26 16:42:00 -04:00
|
|
|
|
|
|
|
|
2024-03-31 03:44:23 -04:00
|
|
|
// Check if there are 2 params
|
|
|
|
if (DIR && langs) {
|
|
|
|
main();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.error('Error: no argument passed');
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2024-03-26 16:42:00 -04:00
|
|
|
|
2024-03-31 03:44:23 -04:00
|
|
|
function getVideoPath(files: string[]) {
|
|
|
|
const fileName = DIR.split('/').at(-1) ?? '';
|
2024-03-26 16:42:00 -04:00
|
|
|
|
2024-04-01 18:07:43 -04:00
|
|
|
const videoFiles = files.filter((f) =>
|
2024-03-31 03:44:23 -04:00
|
|
|
f.includes(fileName) &&
|
|
|
|
!f.endsWith('.nfo') &&
|
2024-04-01 18:07:43 -04:00
|
|
|
!f.endsWith('.srt'));
|
|
|
|
|
|
|
|
if (videoFiles.length === 0) {
|
|
|
|
console.warn('No video files were found');
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${DIR}/${videoFiles[0]}`;
|
2024-03-31 03:44:23 -04:00
|
|
|
}
|
2024-03-26 16:42:00 -04:00
|
|
|
|
2024-03-31 22:06:17 -04:00
|
|
|
async function backupSubs(files: string[]) {
|
2024-03-31 03:44:23 -04:00
|
|
|
// Check if backup folder already exists and create it if not
|
|
|
|
if (!files.some((f) => f.endsWith('.srt.bak'))) {
|
|
|
|
await mkdir(`${DIR}/.srt.bak`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const backups = await readDir(`${DIR}/.srt.bak`);
|
|
|
|
|
|
|
|
// Remove synced subtitles from the list to sync
|
|
|
|
// langs - backups
|
|
|
|
langs = langs.filter((n) => !backups
|
2024-03-31 22:06:17 -04:00
|
|
|
.some((s) => {
|
|
|
|
const l2 = s.split('.').at(-2) ?? '';
|
|
|
|
const l3 = ISO6391To3.get(l2);
|
|
|
|
|
|
|
|
return n === l3;
|
|
|
|
}));
|
2024-03-31 03:44:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (langs.length === 0) {
|
|
|
|
console.warn('Subtitles have already been synced');
|
|
|
|
process.exit(0);
|
|
|
|
}
|
2024-03-31 22:06:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function runSubSync(
|
|
|
|
cmd: string[],
|
|
|
|
onError = (error?: string) => {
|
|
|
|
console.error(error);
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
const { error } = spawn('subsync', cmd, SPAWN_OPTS);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
onError(error.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
spawn('chmod', ['-R', '775', `'${DIR}'`], SPAWN_OPTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const files = await readDir(DIR);
|
|
|
|
|
|
|
|
const VIDEO = getVideoPath(files);
|
|
|
|
const BASE_NAME = VIDEO.split('/').at(-1)?.replace(/\.[^.]*$/, '');
|
|
|
|
|
|
|
|
backupSubs(files);
|
2024-03-31 03:44:23 -04:00
|
|
|
|
|
|
|
// ffprobe the video file to see available audio tracks
|
|
|
|
ffProbe(VIDEO, (_e, data) => {
|
|
|
|
const AVAIL_LANGS = data.streams
|
|
|
|
.filter((s) => s.codec_type === 'audio')
|
2024-04-01 18:40:15 -04:00
|
|
|
.map((s) => s['tags'] && s['tags']['language']);
|
2024-03-30 01:38:27 -04:00
|
|
|
|
2024-03-31 03:44:23 -04:00
|
|
|
// Sync subtitles one by one
|
|
|
|
langs.forEach(async(lang) => {
|
|
|
|
const FILE_NAME = `${BASE_NAME}.${ISO6393To1.get(lang)}.srt`;
|
|
|
|
const IN_FILE = `${DIR}/.srt.bak/${FILE_NAME}`;
|
|
|
|
const OUT_FILE = `${DIR}/${FILE_NAME}`;
|
|
|
|
|
2024-03-27 01:27:03 -04:00
|
|
|
const cmd = [
|
2024-03-29 23:06:56 -04:00
|
|
|
'--cli sync',
|
2024-03-31 03:44:23 -04:00
|
|
|
`--sub-lang ${lang}`,
|
2024-03-26 16:42:00 -04:00
|
|
|
|
2024-03-31 03:44:23 -04:00
|
|
|
`--ref-stream-by-lang ${AVAIL_LANGS.includes(lang) ?
|
|
|
|
lang :
|
|
|
|
AVAIL_LANGS[0]}`,
|
2024-03-27 01:27:03 -04:00
|
|
|
'--ref-stream-by-type "audio"',
|
|
|
|
|
2024-03-31 03:44:23 -04:00
|
|
|
`--sub '${IN_FILE}'`,
|
|
|
|
`--out '${OUT_FILE}'`,
|
2024-03-27 01:27:03 -04:00
|
|
|
`--ref '${VIDEO}'`,
|
2024-03-29 23:06:56 -04:00
|
|
|
];
|
2024-03-27 01:27:03 -04:00
|
|
|
|
2024-03-31 06:10:19 -04:00
|
|
|
if (files.includes(FILE_NAME)) {
|
|
|
|
await mv(OUT_FILE, IN_FILE);
|
2024-03-31 22:06:17 -04:00
|
|
|
|
|
|
|
runSubSync(cmd, async() => {
|
|
|
|
await mv(IN_FILE, OUT_FILE);
|
|
|
|
});
|
2024-03-31 06:10:19 -04:00
|
|
|
}
|
|
|
|
else {
|
2024-04-01 18:40:15 -04:00
|
|
|
let subs = data.streams.filter((s) => {
|
|
|
|
return s['tags'] &&
|
|
|
|
s['tags']['language'] &&
|
|
|
|
s['tags']['language'] === lang &&
|
2024-03-31 06:10:19 -04:00
|
|
|
s.codec_type === 'subtitle';
|
2024-04-01 18:40:15 -04:00
|
|
|
});
|
2024-03-31 06:10:19 -04:00
|
|
|
|
2024-04-01 18:40:15 -04:00
|
|
|
const pgs = subs.filter((s) => s.codec_name === 'hdmv_pgs_subtitle');
|
2024-03-31 22:06:17 -04:00
|
|
|
|
2024-04-01 18:40:15 -04:00
|
|
|
// If we only have PGS subs, warn user
|
|
|
|
if (pgs.length === subs.length) {
|
|
|
|
console.warn(`No SRT subtitle tracks were found for ${lang}`);
|
2024-03-31 06:10:19 -04:00
|
|
|
}
|
2024-04-01 18:40:15 -04:00
|
|
|
// Remove PGS streams from subs
|
|
|
|
subs = subs.filter((s) => s.codec_name !== 'hdmv_pgs_subtitle');
|
2024-03-31 06:10:19 -04:00
|
|
|
|
2024-04-01 18:40:15 -04:00
|
|
|
// Prefer normal subs
|
|
|
|
if (subs.length !== 1) {
|
|
|
|
subs = subs.filter((s) => s.disposition?.forced === 0);
|
|
|
|
}
|
2024-03-31 06:10:19 -04:00
|
|
|
|
2024-04-01 18:40:15 -04:00
|
|
|
if (subs.length === 0) {
|
|
|
|
console.warn(`No subtitle tracks were found for ${lang}`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Extract subtitles
|
|
|
|
spawn('ffmpeg', [
|
|
|
|
'-i', `'${VIDEO}'`,
|
|
|
|
'-map', `"0:${subs[0].index}"`, `'${IN_FILE}'`,
|
|
|
|
], SPAWN_OPTS);
|
|
|
|
|
|
|
|
// Delete subtitles from video
|
|
|
|
spawn('mv', [`'${VIDEO}'`, `'${VIDEO}.bak'`], SPAWN_OPTS);
|
|
|
|
|
|
|
|
spawn('ffmpeg', [
|
|
|
|
'-i', `'${VIDEO}.bak'`,
|
|
|
|
'-map', '0',
|
|
|
|
'-map', `-0:${subs[0].index}`,
|
|
|
|
'-c', 'copy', `'${VIDEO}'`,
|
|
|
|
], SPAWN_OPTS);
|
|
|
|
|
|
|
|
spawn('rm', [`'${VIDEO}.bak'`], SPAWN_OPTS);
|
|
|
|
|
|
|
|
runSubSync(cmd, async() => {
|
|
|
|
await mv(IN_FILE, OUT_FILE);
|
|
|
|
});
|
|
|
|
}
|
2024-03-31 06:10:19 -04:00
|
|
|
}
|
2024-03-27 01:27:03 -04:00
|
|
|
});
|
2024-03-26 16:42:00 -04:00
|
|
|
});
|
|
|
|
}
|