mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-08-13 13:55:57 -07:00
[codex] Make Windows mpv shortcut self-contained (#40)
This commit is contained in:
@@ -16,7 +16,7 @@ async function withTempDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
|
||||
|
||||
function makeFakeYtDlpScript(dir: string): string {
|
||||
const scriptPath = path.join(dir, 'yt-dlp');
|
||||
const script = `#!/usr/bin/env node
|
||||
const script = `#!/usr/bin/env bun
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
|
||||
@@ -87,6 +87,87 @@ if (process.env.YTDLP_FAKE_MODE === 'multi') {
|
||||
fs.writeFileSync(\`\${prefix}.vtt\`, 'WEBVTT\\n');
|
||||
}
|
||||
process.exit(0);
|
||||
`;
|
||||
fs.writeFileSync(scriptPath, script, 'utf8');
|
||||
fs.chmodSync(scriptPath, 0o755);
|
||||
if (process.platform === 'win32') {
|
||||
fs.writeFileSync(scriptPath + '.cmd', `@echo off\r\nbun "${scriptPath}" %*\r\n`, 'utf8');
|
||||
}
|
||||
return scriptPath;
|
||||
}
|
||||
|
||||
function makeFakeYtDlpShellScript(dir: string): string {
|
||||
const scriptPath = path.join(dir, 'yt-dlp');
|
||||
const script = `#!/bin/sh
|
||||
has_auto_subs=0
|
||||
wants_auto_subs=0
|
||||
wants_manual_subs=0
|
||||
sub_lang=''
|
||||
output_template=''
|
||||
|
||||
while [ "$#" -gt 0 ]; do
|
||||
case "$1" in
|
||||
--write-auto-subs)
|
||||
wants_auto_subs=1
|
||||
;;
|
||||
--write-subs)
|
||||
wants_manual_subs=1
|
||||
;;
|
||||
--sub-langs)
|
||||
sub_lang="$2"
|
||||
shift
|
||||
;;
|
||||
-o)
|
||||
output_template="$2"
|
||||
shift
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
if [ "$YTDLP_EXPECT_AUTO_SUBS" = "1" ] && [ "$wants_auto_subs" != "1" ]; then
|
||||
exit 2
|
||||
fi
|
||||
if [ "$YTDLP_EXPECT_MANUAL_SUBS" = "1" ] && [ "$wants_manual_subs" != "1" ]; then
|
||||
exit 3
|
||||
fi
|
||||
if [ -n "$YTDLP_EXPECT_SUB_LANG" ] && [ "$sub_lang" != "$YTDLP_EXPECT_SUB_LANG" ]; then
|
||||
exit 4
|
||||
fi
|
||||
|
||||
prefix="\${output_template%.%(ext)s}"
|
||||
if [ -z "$prefix" ]; then
|
||||
exit 1
|
||||
fi
|
||||
dir="\${prefix%/*}"
|
||||
[ -d "$dir" ] || /bin/mkdir -p "$dir"
|
||||
|
||||
if [ "$YTDLP_FAKE_MODE" = "multi" ]; then
|
||||
OLD_IFS="$IFS"
|
||||
IFS=","
|
||||
for lang in $sub_lang; do
|
||||
if [ -n "$lang" ]; then
|
||||
printf 'WEBVTT\\n' > "\${prefix}.\${lang}.vtt"
|
||||
fi
|
||||
done
|
||||
IFS="$OLD_IFS"
|
||||
elif [ "$YTDLP_FAKE_MODE" = "rolling-auto" ]; then
|
||||
printf 'WEBVTT\\n\\n00:00:01.000 --> 00:00:02.000\\n今日は\\n\\n00:00:02.000 --> 00:00:03.000\\n今日はいい天気ですね\\n\\n00:00:03.000 --> 00:00:04.000\\n今日はいい天気ですね本当に\\n' > "\${prefix}.vtt"
|
||||
elif [ "$YTDLP_FAKE_MODE" = "multi-primary-only-fail" ]; then
|
||||
primary_lang="\${sub_lang%%,*}"
|
||||
if [ -n "$primary_lang" ]; then
|
||||
printf 'WEBVTT\\n' > "\${prefix}.\${primary_lang}.vtt"
|
||||
fi
|
||||
printf "ERROR: Unable to download video subtitles for 'en': HTTP Error 429: Too Many Requests\\n" 1>&2
|
||||
exit 1
|
||||
elif [ "$YTDLP_FAKE_MODE" = "both" ]; then
|
||||
printf 'WEBVTT\\n' > "\${prefix}.vtt"
|
||||
printf 'webp' > "\${prefix}.orig.webp"
|
||||
elif [ "$YTDLP_FAKE_MODE" = "webp-only" ]; then
|
||||
printf 'webp' > "\${prefix}.orig.webp"
|
||||
else
|
||||
printf 'WEBVTT\\n' > "\${prefix}.vtt"
|
||||
fi
|
||||
`;
|
||||
fs.writeFileSync(scriptPath, script, 'utf8');
|
||||
fs.chmodSync(scriptPath, 0o755);
|
||||
@@ -100,7 +181,11 @@ async function withFakeYtDlp<T>(
|
||||
return await withTempDir(async (root) => {
|
||||
const binDir = path.join(root, 'bin');
|
||||
fs.mkdirSync(binDir, { recursive: true });
|
||||
makeFakeYtDlpScript(binDir);
|
||||
if (process.platform === 'win32') {
|
||||
makeFakeYtDlpScript(binDir);
|
||||
} else {
|
||||
makeFakeYtDlpShellScript(binDir);
|
||||
}
|
||||
|
||||
const originalPath = process.env.PATH ?? '';
|
||||
process.env.PATH = `${binDir}${path.delimiter}${originalPath}`;
|
||||
@@ -114,6 +199,43 @@ async function withFakeYtDlp<T>(
|
||||
});
|
||||
}
|
||||
|
||||
async function withFakeYtDlpCommand<T>(
|
||||
mode: 'both' | 'webp-only' | 'multi' | 'multi-primary-only-fail' | 'rolling-auto',
|
||||
fn: (dir: string, binDir: string) => Promise<T>,
|
||||
): Promise<T> {
|
||||
return await withTempDir(async (root) => {
|
||||
const binDir = path.join(root, 'bin');
|
||||
fs.mkdirSync(binDir, { recursive: true });
|
||||
|
||||
const originalPath = process.env.PATH;
|
||||
const originalCommand = process.env.SUBMINER_YTDLP_BIN;
|
||||
process.env.PATH = '';
|
||||
process.env.YTDLP_FAKE_MODE = mode;
|
||||
process.env.SUBMINER_YTDLP_BIN =
|
||||
process.platform === 'win32' ? path.join(binDir, 'yt-dlp.cmd') : path.join(binDir, 'yt-dlp');
|
||||
if (process.platform === 'win32') {
|
||||
makeFakeYtDlpScript(binDir);
|
||||
} else {
|
||||
makeFakeYtDlpShellScript(binDir);
|
||||
}
|
||||
try {
|
||||
return await fn(root, binDir);
|
||||
} finally {
|
||||
if (originalPath === undefined) {
|
||||
delete process.env.PATH;
|
||||
} else {
|
||||
process.env.PATH = originalPath;
|
||||
}
|
||||
delete process.env.YTDLP_FAKE_MODE;
|
||||
if (originalCommand === undefined) {
|
||||
delete process.env.SUBMINER_YTDLP_BIN;
|
||||
} else {
|
||||
process.env.SUBMINER_YTDLP_BIN = originalCommand;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function withFakeYtDlpExpectations<T>(
|
||||
expectations: Partial<
|
||||
Record<'YTDLP_EXPECT_AUTO_SUBS' | 'YTDLP_EXPECT_MANUAL_SUBS' | 'YTDLP_EXPECT_SUB_LANG', string>
|
||||
@@ -179,6 +301,29 @@ test('downloadYoutubeSubtitleTrack prefers subtitle files over later webp artifa
|
||||
});
|
||||
});
|
||||
|
||||
test('downloadYoutubeSubtitleTrack honors SUBMINER_YTDLP_BIN when yt-dlp is not on PATH', async () => {
|
||||
if (process.platform === 'win32') {
|
||||
return;
|
||||
}
|
||||
|
||||
await withFakeYtDlpCommand('both', async (root) => {
|
||||
const result = await downloadYoutubeSubtitleTrack({
|
||||
targetUrl: 'https://www.youtube.com/watch?v=abc123',
|
||||
outputDir: path.join(root, 'out'),
|
||||
track: {
|
||||
id: 'auto:ja-orig',
|
||||
language: 'ja',
|
||||
sourceLanguage: 'ja-orig',
|
||||
kind: 'auto',
|
||||
label: 'Japanese (auto)',
|
||||
},
|
||||
});
|
||||
|
||||
assert.equal(path.extname(result.path), '.vtt');
|
||||
assert.match(path.basename(result.path), /^auto-ja-orig\./);
|
||||
});
|
||||
});
|
||||
|
||||
test('downloadYoutubeSubtitleTrack ignores stale subtitle files from prior runs', async () => {
|
||||
if (process.platform === 'win32') {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user