feat(player): external subtitles via OpenSubtitles

Add an OpenSubtitles client, /api/subtitles routes and useSubtitles to
fetch subtitles by show/episode/filename, wire them as player tracks
alongside embedded ones, and strip hls.js's auto-added tracks. New
OPENSUBTITLES_* / SUBTITLE_LANGUAGES env vars (documented in env.example).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
nerandClaude Opus 4.8 committed 2026-08-08 13:17:12 +03:00
1 parent c695697c4f
commit ecc57ff135
24 files changed
+372 -67

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
import { NextResponse } from 'next/server';
import { downloadSubtitle } from '@/data/clients/openSubtitles';
import redis from '@/data/clients/redis';
type SubtitleParams = {
params: {
fileId: string;
};
};
const CACHE_TTL = 60 * 60 * 24 * 30;
export async function GET(_: Request, { params: { fileId } }: SubtitleParams) {
const id = fileId.replace(/\.vtt$/i, '');
if (!/^\d+$/.test(id)) {
return NextResponse.json({ error: 'Invalid subtitle file id' }, { status: 400 });
}
const key = `subtitles:${id}`;
try {
const cached = await redis?.get<string>(key).catch(() => null);
const content = cached ?? (await downloadSubtitle(Number(id)));
if (!cached) await redis?.set(key, content, { ex: CACHE_TTL }).catch(() => null);
return new NextResponse(content, {
headers: {
'Content-Type': 'text/vtt; charset=utf-8',
'Cache-Control': 'public, max-age=31536000, immutable'
}
});
} catch (error) {
console.error('[subtitles] download', error);
return NextResponse.json({ error: 'Failed to download subtitle' }, { status: 502 });
}
}