feat(node-syncsub): extract subs from video if not present
All checks were successful
Discord / discord commits (push) Has been skipped
All checks were successful
Discord / discord commits (push) Has been skipped
This commit is contained in:
parent
57816ab604
commit
1d1856351b
2 changed files with 66 additions and 31 deletions
|
@ -44,7 +44,7 @@ export const ISO6391To3 = new Map([
|
||||||
['fa', 'fas'],
|
['fa', 'fas'],
|
||||||
['fj', 'fij'],
|
['fj', 'fij'],
|
||||||
['fi', 'fin'],
|
['fi', 'fin'],
|
||||||
['fr', 'fra'],
|
['fr', 'fre'],
|
||||||
['fy', 'fry'],
|
['fy', 'fry'],
|
||||||
['ff', 'ful'],
|
['ff', 'ful'],
|
||||||
['gd', 'gla'],
|
['gd', 'gla'],
|
||||||
|
@ -231,7 +231,6 @@ export const ISO6393To1 = new Map([
|
||||||
['fas', 'fa'],
|
['fas', 'fa'],
|
||||||
['fij', 'fj'],
|
['fij', 'fj'],
|
||||||
['fin', 'fi'],
|
['fin', 'fi'],
|
||||||
['fra', 'fr'],
|
|
||||||
['fre', 'fr'],
|
['fre', 'fr'],
|
||||||
['fry', 'fy'],
|
['fry', 'fy'],
|
||||||
['ful', 'ff'],
|
['ful', 'ff'],
|
||||||
|
|
|
@ -5,10 +5,14 @@ import {
|
||||||
} from 'fs/promises';
|
} from 'fs/promises';
|
||||||
|
|
||||||
import { ffprobe as ffProbe } from 'fluent-ffmpeg';
|
import { ffprobe as ffProbe } from 'fluent-ffmpeg';
|
||||||
import { spawn } from 'child_process';
|
import { spawnSync as spawn } from 'child_process';
|
||||||
|
|
||||||
import { ISO6391To3, ISO6393To1 } from './lang-codes';
|
import { ISO6391To3, ISO6393To1 } from './lang-codes';
|
||||||
|
|
||||||
|
const SPAWN_OPTS = {
|
||||||
|
shell: true,
|
||||||
|
stdio: [process.stdin, process.stdout, process.stderr],
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* These are the cli arguments
|
* These are the cli arguments
|
||||||
|
@ -38,19 +42,7 @@ function getVideoPath(files: string[]) {
|
||||||
!f.endsWith('.srt'))[0]}`;
|
!f.endsWith('.srt'))[0]}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function runSubSync(cmd: string[]) {
|
async function backupSubs(files: string[]) {
|
||||||
spawn('subsync', cmd, {
|
|
||||||
shell: true,
|
|
||||||
stdio: [process.stdin, process.stdout, process.stderr],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function main() {
|
|
||||||
const files = await readDir(DIR);
|
|
||||||
|
|
||||||
const VIDEO = getVideoPath(files);
|
|
||||||
const BASE_NAME = VIDEO.split('/').at(-1)?.replace(/\.[^.]*$/, '');
|
|
||||||
|
|
||||||
// Check if backup folder already exists and create it if not
|
// Check if backup folder already exists and create it if not
|
||||||
if (!files.some((f) => f.endsWith('.srt.bak'))) {
|
if (!files.some((f) => f.endsWith('.srt.bak'))) {
|
||||||
await mkdir(`${DIR}/.srt.bak`);
|
await mkdir(`${DIR}/.srt.bak`);
|
||||||
|
@ -61,13 +53,42 @@ async function main() {
|
||||||
// Remove synced subtitles from the list to sync
|
// Remove synced subtitles from the list to sync
|
||||||
// langs - backups
|
// langs - backups
|
||||||
langs = langs.filter((n) => !backups
|
langs = langs.filter((n) => !backups
|
||||||
.some((s) => n === ISO6391To3.get(s.split('.').at(-2) ?? '')));
|
.some((s) => {
|
||||||
|
const l2 = s.split('.').at(-2) ?? '';
|
||||||
|
const l3 = ISO6391To3.get(l2);
|
||||||
|
|
||||||
|
return n === l3;
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (langs.length === 0) {
|
if (langs.length === 0) {
|
||||||
console.warn('Subtitles have already been synced');
|
console.warn('Subtitles have already been synced');
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
// ffprobe the video file to see available audio tracks
|
// ffprobe the video file to see available audio tracks
|
||||||
ffProbe(VIDEO, (_e, data) => {
|
ffProbe(VIDEO, (_e, data) => {
|
||||||
|
@ -97,36 +118,51 @@ async function main() {
|
||||||
|
|
||||||
if (files.includes(FILE_NAME)) {
|
if (files.includes(FILE_NAME)) {
|
||||||
await mv(OUT_FILE, IN_FILE);
|
await mv(OUT_FILE, IN_FILE);
|
||||||
runSubSync(cmd);
|
|
||||||
|
runSubSync(cmd, async() => {
|
||||||
|
await mv(IN_FILE, OUT_FILE);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
let stream = data.streams.find((s) => {
|
let stream = data.streams.find((s) => {
|
||||||
return s['tags']['language'] === lang &&
|
return s['tags']['language'] === lang &&
|
||||||
s.disposition!.forced === 0 &&
|
s.disposition?.forced === 0 &&
|
||||||
s.codec_type === 'subtitle';
|
s.codec_type === 'subtitle';
|
||||||
})!.index;
|
})?.index ?? -1;
|
||||||
|
|
||||||
if (!stream) {
|
if (stream === -1) {
|
||||||
stream = data.streams.find((s) => {
|
stream = data.streams.find((s) => {
|
||||||
return s['tags']['language'] === lang &&
|
return s['tags']['language'] === lang &&
|
||||||
s.codec_type === 'subtitle';
|
s.codec_type === 'subtitle';
|
||||||
})!.index;
|
})?.index ?? -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!stream) {
|
if (stream === -1) {
|
||||||
console.warn(`No subtitle tracks were found for ${lang}`);
|
console.warn(`No subtitle tracks were found for ${lang}`);
|
||||||
process.exit(0);
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Extract subtitles
|
||||||
spawn('ffmpeg', [
|
spawn('ffmpeg', [
|
||||||
'-i', `'${VIDEO}'`,
|
'-i', `'${VIDEO}'`,
|
||||||
'-map', `0:${stream}`, `'${IN_FILE}'`,
|
'-map', `"0:${stream}"`, `'${IN_FILE}'`,
|
||||||
], {
|
], SPAWN_OPTS);
|
||||||
shell: true,
|
|
||||||
stdio: [process.stdin, process.stdout, process.stderr],
|
|
||||||
|
|
||||||
}).on('close', () => {
|
// Delete subtitles from video
|
||||||
runSubSync(cmd);
|
spawn('mv', [`'${VIDEO}'`, `'${VIDEO}.bak'`], SPAWN_OPTS);
|
||||||
|
|
||||||
|
spawn('ffmpeg', [
|
||||||
|
'-i', `'${VIDEO}.bak'`,
|
||||||
|
'-map', '0',
|
||||||
|
'-map', `-0:${stream}`,
|
||||||
|
'-c', 'copy', `'${VIDEO}'`,
|
||||||
|
], SPAWN_OPTS);
|
||||||
|
|
||||||
|
spawn('rm', [`'${VIDEO}.bak'`], SPAWN_OPTS);
|
||||||
|
|
||||||
|
runSubSync(cmd, async() => {
|
||||||
|
await mv(IN_FILE, OUT_FILE);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue