-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support subtitle autodetection on NRK TV
- Loading branch information
1 parent
e5f9dc8
commit 96a74cb
Showing
5 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { inferTracks } from './util'; | ||
|
||
const originalFetch = window.fetch; | ||
let lastMetadataUrl: string | undefined; | ||
|
||
window.fetch = (...args) => { | ||
let metadataUrl = undefined; | ||
|
||
for (const arg of args) { | ||
if (typeof arg === 'string' && arg.includes('metadata')) { | ||
metadataUrl = arg; | ||
} | ||
if (arg instanceof Request && arg.url.includes('metadata')) { | ||
metadataUrl = arg.url; | ||
} | ||
} | ||
|
||
if (metadataUrl !== undefined) { | ||
lastMetadataUrl = metadataUrl; | ||
} | ||
|
||
return originalFetch(...args); | ||
}; | ||
|
||
const requestTracks = async (url: string) => { | ||
const tracks = []; | ||
|
||
try { | ||
const value = await (await fetch(url)).json(); | ||
|
||
if (typeof value?.playable?.subtitles === 'object' && Array.isArray(value.playable.subtitles)) { | ||
for (const track of value.playable.subtitles) { | ||
if ( | ||
typeof track.label === 'string' && | ||
typeof track.language === 'string' && | ||
typeof track.webVtt === 'string' | ||
) { | ||
tracks.push({ | ||
label: track.label as string, | ||
language: track.language as string, | ||
url: track.webVtt as string, | ||
extension: 'vtt', | ||
}); | ||
} | ||
} | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
|
||
return tracks; | ||
}; | ||
|
||
inferTracks({ | ||
onRequest: async (addTrack, setBasename) => { | ||
if (lastMetadataUrl === undefined) { | ||
return; | ||
} | ||
|
||
const manifestUrl = new URL(lastMetadataUrl); | ||
const value = await (await fetch(lastMetadataUrl)).json(); | ||
|
||
if (typeof value?.preplay?.titles?.title === 'string') { | ||
if (typeof value?.preplay?.titles?.subtitle === 'string') { | ||
setBasename(`${value.preplay.titles.title} ${value.preplay.titles.subtitle}`); | ||
} else { | ||
setBasename(value.preplay.titles.title); | ||
} | ||
} | ||
|
||
if (typeof value?._links?.manifests === 'object' && Array.isArray(value._links.manifests)) { | ||
for (const manifest of value._links.manifests) { | ||
if (typeof manifest.href === 'string') { | ||
const tracks = await requestTracks(`${manifestUrl.origin}${manifest.href}`); | ||
|
||
for (const track of tracks) { | ||
addTrack(track); | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
|
||
waitForBasename: true, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters