feat(youtube): add mediaCache mode and safer stream media extraction

- Add `youtube.mediaCache.mode` config option (`direct` | `background`)
- Resolve EDL stream URLs to single audio/video URLs before ffmpeg extraction
- Pass reconnect, user-agent, and safe headers to ffmpeg for remote streams
- Add background yt-dlp media cache with fallback to direct extraction
- Introduce `MediaInput` structured type replacing bare path strings
This commit is contained in:
2026-06-20 02:25:32 -07:00
parent d199376364
commit 236f22662c
41 changed files with 1381 additions and 102 deletions
@@ -20,6 +20,7 @@ export type YoutubePlaybackRuntimeDeps = {
launchWindowsMpv: (playbackUrl: string, args: string[]) => Promise<LaunchResult>;
waitForYoutubeMpvConnected: (timeoutMs: number) => Promise<boolean>;
prepareYoutubePlaybackInMpv: (request: { url: string }) => Promise<boolean>;
startYoutubeMediaCache?: (url: string) => void | Promise<void>;
runYoutubePlaybackFlow: (request: {
url: string;
mode: NonNullable<CliArgs['youtubeMode']>;
@@ -126,6 +127,15 @@ export function createYoutubePlaybackRuntime(deps: YoutubePlaybackRuntimeDeps) {
if (!mediaReady) {
throw new Error('Timed out waiting for mpv to load the requested YouTube URL.');
}
if (deps.startYoutubeMediaCache) {
void Promise.resolve(deps.startYoutubeMediaCache(request.url)).catch((error) => {
deps.logWarn(
`Failed to start YouTube media cache: ${
error instanceof Error ? error.message : String(error)
}`,
);
});
}
await deps.runYoutubePlaybackFlow({
url: request.url,