From 4ddda6673a2b5919d7b708deac9ac78180ad6775 Mon Sep 17 00:00:00 2001 From: WardPearce Date: Wed, 30 Apr 2025 20:03:17 +1200 Subject: [PATCH] Fix formatting of adaptive_formats --- materialious/src/lib/components/Player.svelte | 42 ++++--------------- materialious/src/lib/patches/youtubejs.ts | 36 ++++++++++++---- materialious/src/lib/timestamps.ts | 1 - materialious/src/routes/(app)/+layout.svelte | 8 ++-- 4 files changed, 40 insertions(+), 47 deletions(-) diff --git a/materialious/src/lib/components/Player.svelte b/materialious/src/lib/components/Player.svelte index 644f801b..fccd8970 100644 --- a/materialious/src/lib/components/Player.svelte +++ b/materialious/src/lib/components/Player.svelte @@ -174,8 +174,6 @@ return; } - let dashUrl: string | undefined = undefined; - player = new shaka.Player(); playerElement = document.getElementById('player') as HTMLMediaElement; @@ -221,11 +219,7 @@ player.configure({ abr: { - enabled: true, - restrictions: { - maxWidth: 1920, - maxHeight: 1080 - } + enabled: true }, streaming: { bufferingGoal: 120, @@ -267,13 +261,7 @@ .media_ustreamer_request_config?.video_playback_ustreamer_config; if (data.video.ytjs.video.streaming_data) { - formatList = data.video.ytjs.video.streaming_data.adaptive_formats.map((format) => { - const formatKey = fromFormat(format) || ''; - format.url = `https://sabr?___key=${formatKey}`; - format.signature_cipher = undefined; - format.decipher = () => format.url || ''; - return format; - }); + formatList = data.video.ytjs.video.streaming_data.adaptive_formats; } if (data.video.ytjs.video.streaming_data?.server_abr_streaming_url) @@ -283,20 +271,6 @@ ) ); - if (data.video.ytjs.video.streaming_data) { - if (isLive) { - dashUrl = data.video.ytjs.video.streaming_data.dash_manifest_url - ? `${data.video.ytjs.video.streaming_data.dash_manifest_url}/mpd_version/7` - : data.video.ytjs.video.streaming_data.hls_manifest_url; - } else if (data.video.ytjs.video.streaming_data.dash_manifest_url && isPostLiveDVR) { - dashUrl = data.video.ytjs.video.streaming_data.hls_manifest_url - ? data.video.ytjs.video.streaming_data.hls_manifest_url // HLS is preferred for DVR streams. - : `${data.video.ytjs.video.streaming_data.dash_manifest_url}/mpd_version/7`; - } else { - dashUrl = `data:application/dash+xml;base64,${btoa(await data.video.ytjs.video.toDash(undefined, undefined, { captions_format: 'vtt' }))}`; - } - } - playerElement.addEventListener('seeked', () => (lastSeekMs = Date.now())); player.addEventListener('variantchanged', (event) => { @@ -526,7 +500,7 @@ } } } else if (type == shaka.net.NetworkingEngine.RequestType.LICENSE) { - const innertube = await Innertube.create({ fetch: fetch }); + const innertube = await Innertube.create({ fetch: window.fetch }); const wrapped = {} as Record; wrapped.context = innertube.session.context; wrapped.cpn = data.video.ytjs?.clientPlaybackNonce; @@ -635,9 +609,7 @@ } if (!data.video.hlsUrl) { - if (!dashUrl) { - dashUrl = data.video.dashUrl; - } + let dashUrl = data.video.dashUrl; if (!data.video.fallbackPatch && (!Capacitor.isNativePlatform() || proxyVideos)) { dashUrl += '?local=true'; @@ -771,7 +743,11 @@ }); } } else { - await player.load(data.video.hlsUrl + '?local=true'); + if (data.video.fallbackPatch === 'youtubejs') { + await player.load(data.video.dashUrl); + } else { + await player.load(data.video.hlsUrl + '?local=true'); + } } if (data.video.fallbackPatch === 'youtubejs') { diff --git a/materialious/src/lib/patches/youtubejs.ts b/materialious/src/lib/patches/youtubejs.ts index ead119bc..40553994 100644 --- a/materialious/src/lib/patches/youtubejs.ts +++ b/materialious/src/lib/patches/youtubejs.ts @@ -9,6 +9,7 @@ import type { VideoPlay } from '$lib/api/model'; import { numberWithCommas } from '$lib/numbers'; +import { fromFormat } from '$lib/sabr/formatKeyUtils'; import { interfaceRegionStore, poTokenCacheStore } from '$lib/store'; import { Capacitor } from '@capacitor/core'; import { USER_AGENT } from 'bgutils-js'; @@ -76,12 +77,11 @@ export async function patchYoutubeJs(videoId: string): Promise { const rawNextResponse = await watchEndpoint.call(youtube.actions, { override_endpoint: '/next', racyCheckOk: true, - contentCheckOk: true, - parse: false + contentCheckOk: true }); const video = new YT.VideoInfo( [rawPlayerResponse, rawNextResponse], - youtube!.actions, + youtube.actions, clientPlaybackNonce ); @@ -89,13 +89,33 @@ export async function patchYoutubeJs(videoId: string): Promise { throw new Error('Unable to pull video info from youtube.js'); } - let dashUri: string = ''; + let dashUri: string | undefined; - if (!video.basic_info.is_live) { - const manifest = await video.toDash(); - dashUri = `data:application/dash+xml;charset=utf-8;base64,${Buffer.from(manifest).toString('base64')}`; + if (video.streaming_data) { + video.streaming_data.adaptive_formats = video.streaming_data.adaptive_formats.map((format) => { + const formatKey = fromFormat(format) || ''; + format.url = `https://sabr?___key=${formatKey}`; + format.signature_cipher = undefined; + format.decipher = () => format.url || ''; + + return format; + }); + + if (video.basic_info.is_live) { + dashUri = video.streaming_data.dash_manifest_url + ? `${video.streaming_data.dash_manifest_url}/mpd_version/7` + : video.streaming_data.hls_manifest_url; + } else if (video.streaming_data.dash_manifest_url && video.basic_info.is_post_live_dvr) { + dashUri = video.streaming_data.hls_manifest_url + ? video.streaming_data.hls_manifest_url // HLS is preferred for DVR streams. + : `${video.streaming_data.dash_manifest_url}/mpd_version/7`; + } else { + dashUri = `data:application/dash+xml;base64,${btoa(await video.toDash(undefined, undefined, { captions_format: 'vtt' }))}`; + } } + if (!dashUri) throw Error('Unable to find suitable dash manifest'); + const descString = video.secondary_info.description?.toString() || ''; let authorThumbnails: Image[]; @@ -207,7 +227,7 @@ export async function patchYoutubeJs(videoId: string): Promise { innertube: youtube, video: video, clientPlaybackNonce: clientPlaybackNonce, - rawApiResponse: rawResponse + rawApiResponse: rawPlayerResponse }, fallbackPatch: 'youtubejs' }; diff --git a/materialious/src/lib/timestamps.ts b/materialious/src/lib/timestamps.ts index 0fdd2389..f11fbd31 100644 --- a/materialious/src/lib/timestamps.ts +++ b/materialious/src/lib/timestamps.ts @@ -63,7 +63,6 @@ export function phaseDescription( /