mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-11-22 04:46:07 -08:00
* feat(subtitles): generate Japanese subtitles during Jellyfin playback - Read the item's original file via toJellyfinOriginalStreamUrl and write output to a temp dir - Transcoded playback uses Jellyfin's audio index and only external references - Show Jellyfin media by title and redact URL tokens in subprocess errors - Update overlay copy, docs, and changelog fragment * fix(subtitles): redact full stream tokens and reuse one Jellyfin output folder * test(subtitles): make Jellyfin output path assertions platform-neutral - Compare dirname/basename via node:path instead of POSIX-only regexes - Assert the full sub-add command uses the generated output path
80 lines
2.8 KiB
TypeScript
80 lines
2.8 KiB
TypeScript
import { spawn } from 'node:child_process';
|
|
import { expandSubtitleGenerationPath } from './subtitle-generation-files';
|
|
|
|
const OUTPUT_LIMIT = 64 * 1024;
|
|
|
|
// ffmpeg echoes input URLs in its errors; streamed media carries access tokens in the query.
|
|
function redactUrlCredentials(text: string): string {
|
|
return text.replace(/([?&](?:api_?key|token|access_token)=)[^&#\s'"]+/gi, '$1<redacted>');
|
|
}
|
|
|
|
// Keep partial lines between chunks: ffmpeg and whisper both report progress on stderr.
|
|
export function runSubtitleGenerationProcess(input: {
|
|
command: string;
|
|
args: string[];
|
|
signal?: AbortSignal;
|
|
onLine?: (line: string) => void;
|
|
/** Shown when the executable is missing; defaults to pointing at subtitleGeneration settings. */
|
|
missingMessage?: string;
|
|
}): Promise<string> {
|
|
input.signal?.throwIfAborted();
|
|
return new Promise((resolve, reject) => {
|
|
const child = spawn(expandSubtitleGenerationPath(input.command), input.args, {
|
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
});
|
|
let stdout = '';
|
|
let stderr = '';
|
|
let killTimer: ReturnType<typeof setTimeout> | undefined;
|
|
const abort = () => {
|
|
child.kill('SIGTERM');
|
|
killTimer = setTimeout(() => child.kill('SIGKILL'), 2000);
|
|
killTimer.unref();
|
|
};
|
|
input.signal?.addEventListener('abort', abort, { once: true });
|
|
if (input.signal?.aborted) abort();
|
|
const cleanup = () => {
|
|
input.signal?.removeEventListener('abort', abort);
|
|
clearTimeout(killTimer);
|
|
};
|
|
for (const [stream, isStdout] of [
|
|
[child.stdout, true],
|
|
[child.stderr, false],
|
|
] as const) {
|
|
let pending = '';
|
|
stream.setEncoding('utf8');
|
|
stream.on('data', (chunk: string) => {
|
|
if (isStdout) stdout = (stdout + chunk).slice(-OUTPUT_LIMIT);
|
|
else stderr = (stderr + chunk).slice(-OUTPUT_LIMIT);
|
|
const lines = (pending + chunk).split(/[\r\n]/);
|
|
pending = (lines.pop() ?? '').slice(-OUTPUT_LIMIT);
|
|
for (const line of lines) input.onLine?.(line);
|
|
});
|
|
stream.on('end', () => {
|
|
if (pending) input.onLine?.(pending);
|
|
});
|
|
}
|
|
child.once('error', (error) => {
|
|
cleanup();
|
|
reject(
|
|
new Error(
|
|
'code' in error && error.code === 'ENOENT'
|
|
? (input.missingMessage ??
|
|
`${input.command} was not found. Install it or set its path under subtitleGeneration in Settings.`)
|
|
: `Could not run ${input.command}: ${error.message}`,
|
|
),
|
|
);
|
|
});
|
|
child.once('close', (code) => {
|
|
cleanup();
|
|
if (input.signal?.aborted) reject(new Error('Subtitle generation cancelled.'));
|
|
else if (code !== 0) {
|
|
reject(
|
|
new Error(
|
|
`${input.command} exited with status ${code}: ${redactUrlCredentials(stderr.trim())}`,
|
|
),
|
|
);
|
|
} else resolve(stdout);
|
|
});
|
|
});
|
|
}
|