Skip to content

Commit 2f6fb96

Browse files
authored
Decipher live DASH manifest URL (#8530)
1 parent b3c7db5 commit 2f6fb96

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

src/renderer/helpers/api/local.js

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -540,15 +540,11 @@ export async function getLocalVideoInfo(id) {
540540
}
541541

542542
if (info.streaming_data.dash_manifest_url) {
543-
let url = info.streaming_data.dash_manifest_url
544-
545-
if (url.includes('?')) {
546-
url += `&pot=${encodeURIComponent(contentPoToken)}&mpd_version=7`
547-
} else {
548-
url += `${url.endsWith('/') ? '' : '/'}pot/${encodeURIComponent(contentPoToken)}/mpd_version/7`
549-
}
550-
551-
info.streaming_data.dash_manifest_url = url
543+
info.streaming_data.dash_manifest_url = await decipherDashManifestUrl(
544+
info.streaming_data.dash_manifest_url,
545+
webInnertube.session.player,
546+
contentPoToken
547+
)
552548
}
553549
}
554550

@@ -600,6 +596,49 @@ async function decipherFormats(formats, player) {
600596
}
601597
}
602598

599+
/**
600+
* @param {string} url
601+
* @param {import('youtubei.js').Player} player
602+
* @param {string} poToken
603+
*/
604+
async function decipherDashManifestUrl(url, player, poToken) {
605+
const urlObject = new URL(url)
606+
607+
if (urlObject.searchParams.size > 0) {
608+
urlObject.searchParams.set('pot', poToken)
609+
urlObject.searchParams.set('mpd_version', '7')
610+
611+
return await player.decipher(urlObject.toString())
612+
}
613+
614+
// Convert path params to query params
615+
const pathParts = urlObject.pathname
616+
.replace('/api/manifest/dash', '')
617+
.split('/')
618+
.filter(part => part.length > 0)
619+
620+
urlObject.pathname = '/api/manifest/dash'
621+
622+
for (let i = 0; i + 1 < pathParts.length; i += 2) {
623+
urlObject.searchParams.set(pathParts[i], decodeURIComponent(pathParts[i + 1]))
624+
}
625+
626+
// decipher
627+
const deciphered = await player.decipher(urlObject.toString())
628+
629+
// convert query parameters back to path parameters
630+
const decipheredUrlObject = new URL(deciphered)
631+
632+
for (const [key, value] of decipheredUrlObject.searchParams) {
633+
decipheredUrlObject.pathname += `/${key}/${encodeURIComponent(value)}`
634+
}
635+
636+
decipheredUrlObject.search = ''
637+
decipheredUrlObject.pathname += `/pot/${encodeURIComponent(poToken)}/mpd_version/7`
638+
639+
return decipheredUrlObject.toString()
640+
}
641+
603642
/**
604643
* @param {string} url
605644
* @param {boolean} doLogError

0 commit comments

Comments
 (0)