mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-27 17:51:20 -08:00
refactor(multiple-subtitles): [PKG-MULTI-SUB-03] own requests and subtitle resources
This commit is contained in:
1 parent
36cd151095
commit
beaa93d6cc
25 files changed
+7908
-250
No files matched your search
@@ -0,0 +1,33 @@
|
||||
export default async function loadVtt(option, { getExt, srtToVtt, assToVtt }, lifetime) {
|
||||
if (lifetime.closed)
|
||||
return
|
||||
const controller = typeof AbortController === 'function' ? new AbortController() : null
|
||||
const release = lifetime.own(() => controller?.abort())
|
||||
try {
|
||||
const url = option.url
|
||||
if (lifetime.closed)
|
||||
return
|
||||
const response = await lifetime.wait(controller ? fetch(url, { signal: controller.signal }) : fetch(url))
|
||||
if (lifetime.closed)
|
||||
return
|
||||
if (response.ok === false)
|
||||
throw new Error(`Failed to fetch multiple subtitles: HTTP ${response.status}`)
|
||||
const buffer = await lifetime.wait(response.arrayBuffer())
|
||||
if (lifetime.closed)
|
||||
return
|
||||
const text = new TextDecoder(option.encoding || 'utf-8').decode(buffer)
|
||||
switch (option.type || getExt(option.url)) {
|
||||
case 'srt': return srtToVtt(text)
|
||||
case 'ass': return assToVtt(text)
|
||||
case 'vtt': return text
|
||||
default: return ''
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
controller?.abort()
|
||||
throw error
|
||||
}
|
||||
finally {
|
||||
release()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user