mirror of
https://git.stupid.fish/teidesu/scripts.git
synced 2026-01-12 07:01:09 +11:00
chore: update public repo
This commit is contained in:
parent
ef375d1188
commit
46cf487f04
5 changed files with 229 additions and 26 deletions
|
|
@ -5,8 +5,7 @@ import { writeWebStreamToFile } from './fs.ts'
|
|||
|
||||
interface SimpleMpd {
|
||||
codecs: string
|
||||
initUrl: string
|
||||
segmentUrls: string[]
|
||||
segments: string[]
|
||||
}
|
||||
|
||||
export function parseSimpleMpd(xml: string): SimpleMpd {
|
||||
|
|
@ -39,7 +38,7 @@ export function parseSimpleMpd(xml: string): SimpleMpd {
|
|||
const segments = timeline.find('S')
|
||||
assert(segments.length > 0, 'expected at least one segment')
|
||||
|
||||
const segmentUrls: string[] = []
|
||||
const segmentUrls: string[] = [initUrl]
|
||||
|
||||
let segmentNum = Number(startNum)
|
||||
for (const segment of segments) {
|
||||
|
|
@ -56,26 +55,51 @@ export function parseSimpleMpd(xml: string): SimpleMpd {
|
|||
|
||||
return {
|
||||
codecs: representation.attr('codecs')!,
|
||||
initUrl,
|
||||
segmentUrls,
|
||||
segments: segmentUrls,
|
||||
}
|
||||
}
|
||||
|
||||
export function concatMpdSegments(options: {
|
||||
mpd: SimpleMpd
|
||||
export function parseSimpleHls(m3u8: string): string[] {
|
||||
let initUrl: string | undefined
|
||||
const segments: string[] = []
|
||||
|
||||
const lines = m3u8.split('\n')
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i]
|
||||
if (line.startsWith('#EXT-X-MAP:URI=')) {
|
||||
initUrl = JSON.parse(line.slice('#EXT-X-MAP:URI='.length))
|
||||
} else if (line.startsWith('#EXTINF:')) {
|
||||
const segmentUrl = lines[i + 1]
|
||||
segments.push(segmentUrl)
|
||||
i++
|
||||
} else if (line.startsWith('#EXT-X-ENDLIST')) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (initUrl) {
|
||||
segments.unshift(initUrl)
|
||||
}
|
||||
|
||||
return segments
|
||||
}
|
||||
|
||||
export function concatSegments(options: {
|
||||
segments: string[]
|
||||
fetch: (url: string) => Promise<Uint8Array>
|
||||
poolSize?: number
|
||||
}): ReadableStream {
|
||||
const { mpd, fetch, poolSize = 8 } = options
|
||||
const { segments, fetch, poolSize = 8 } = options
|
||||
|
||||
let nextSegmentIdx = -1
|
||||
let nextWorkerSegmentIdx = -1
|
||||
let nextSegmentIdx = 0
|
||||
let nextWorkerSegmentIdx = 0
|
||||
const nextSegmentCv = new ConditionVariable()
|
||||
const buffer: Record<number, Uint8Array> = {}
|
||||
|
||||
const downloadSegment = async (idx = nextWorkerSegmentIdx++) => {
|
||||
// console.log('downloading segment %s', idx)
|
||||
const url = idx === -1 ? mpd.initUrl : mpd.segmentUrls[idx]
|
||||
const url = segments[idx]
|
||||
const chunk = await fetch(url)
|
||||
buffer[idx] = chunk
|
||||
|
||||
|
|
@ -83,14 +107,14 @@ export function concatMpdSegments(options: {
|
|||
nextSegmentCv.notify()
|
||||
}
|
||||
|
||||
if (nextWorkerSegmentIdx < mpd.segmentUrls.length) {
|
||||
if (nextWorkerSegmentIdx < segments.length) {
|
||||
return downloadSegment()
|
||||
}
|
||||
}
|
||||
|
||||
let error: unknown
|
||||
void Promise.all(Array.from({
|
||||
length: Math.min(poolSize, mpd.segmentUrls.length),
|
||||
length: Math.min(poolSize, segments.length),
|
||||
}, downloadSegment))
|
||||
.catch((e) => {
|
||||
error = e
|
||||
|
|
@ -113,7 +137,7 @@ export function concatMpdSegments(options: {
|
|||
controller.enqueue(buf)
|
||||
}
|
||||
|
||||
if (nextSegmentIdx >= mpd.segmentUrls.length) {
|
||||
if (nextSegmentIdx >= segments.length) {
|
||||
controller.close()
|
||||
return
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue