Skip to content

Commit

Permalink
Refactor: implement stream selection based on language priority with …
Browse files Browse the repository at this point in the history
…fallback options
  • Loading branch information
mateussouzaweb committed Jan 31, 2024
1 parent d0569ab commit 751e4b7
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/scripts/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ declare var Hls: any
let hls = null
let area: HTMLElement = null
let video: HTMLVideoElement = null
let streams = []
let playing = false
let trackTimeout = null
let lastPlayhead = 0
Expand Down Expand Up @@ -106,7 +105,7 @@ const loadEpisode: Callback = async ({ state }) => {
const seasonNumber = episodeInfo.episode_metadata.season_number
const episodeNumber = Number(episodeInfo.episode_metadata.episode_number)
const episodeName = episodeInfo.title

const streamsLink = String(episodeInfo.streams_link)
const videoId = streamsLink.replace('/content/v2/cms/videos/', '').replace('/streams', '')
state.videoId = videoId
Expand Down Expand Up @@ -195,19 +194,26 @@ const streamVideo: Callback = async ({ state }) => {
}

const streamsResponse = await App.streams(videoId, {})
const streams = streamsResponse.streams.adaptive_hls || []
let stream = ''

const locale = localStorage.getItem('preferredContentSubtitleLanguage')
const priorities = [locale, 'en-US', '']

streams = streamsResponse.streams
priorities.forEach((locale) => {
if( streams[locale] && !stream ){
stream = streams[locale].url
}
})

let stream = streamsResponse.streams.adaptive_hls[locale]
if (!stream) {
throw Error('No streams to load.')
}

const proxyUrl = document.body.dataset.proxyUrl
const proxyEncode = document.body.dataset.proxyEncode
if (proxyUrl) {
stream.url = proxyUrl + (proxyEncode === "true" ? encodeURIComponent(stream.url) : stream.url)
stream = proxyUrl + (proxyEncode === "true" ? encodeURIComponent(stream) : stream)
}

area.classList.add('video-is-loading')
Expand All @@ -229,7 +235,7 @@ const streamVideo: Callback = async ({ state }) => {
})

hls.on(Hls.Events.MEDIA_ATTACHED, () => {
hls.loadSource(stream.url)
hls.loadSource(stream)
})

hls.on(Hls.Events.MANIFEST_PARSED, () => {
Expand Down Expand Up @@ -539,7 +545,7 @@ const setWatched: Callback = async (component) => {
'content_id': episodeId,
'playhead': duration
})

stopTrackProgress(component)

}
Expand Down

0 comments on commit 751e4b7

Please sign in to comment.