Fix/live videos (#877)
* Initial fix * Optimize * changelog, move hls down in priority * Fix for audio track names when using Invidious * comments * comment * comment
This commit is contained in:
1 parent
99f606610c
commit
425f4d9660
6 files changed
+663
-76
No files matched your search
@@ -4,10 +4,20 @@ import "pkg:/source/utils/Logging.bs"
|
||||
import "pkg:/source/utils/StringUtils.bs"
|
||||
import "pkg:/source/utils/UrlUtils.bs"
|
||||
|
||||
' Number of native YouTube live fragments (each `targetDurationSec` long) that the
|
||||
' /api/dash/live-seg proxy concatenates into a single DASH segment. Roku dislikes
|
||||
' the ~2s native request cadence, so we present longer segments. Used by both the
|
||||
' manifest (segment duration / refresh timing) and DashRouter's live-seg handler
|
||||
' (which fragments to fetch per DASH segment number).
|
||||
const LIVE_SEGMENT_GROUP = 4
|
||||
|
||||
class DashManifest
|
||||
|
||||
public invidiousInstance as dynamic
|
||||
public disableAutoDubbed as boolean
|
||||
' Base URL (scheme://host:port) of Playlet's local web server, so live
|
||||
' SegmentTemplate URLs can address the /api/dash/live-seg proxy. Set by DashRouter.
|
||||
public liveSegmentBaseUrl as string = ""
|
||||
|
||||
function new(invidiousInstance as string, disableAutoDubbed = false as boolean)
|
||||
m.invidiousInstance = invidiousInstance
|
||||
@@ -51,8 +61,9 @@ class DashManifest
|
||||
end if
|
||||
|
||||
duration = m.GetDuration(metadata)
|
||||
isLive = ValidBool(metadata.liveNow)
|
||||
|
||||
groupings = m.CreateFormatGroupings(adaptiveFormats, metadata.isPostLiveDvr)
|
||||
groupings = m.CreateFormatGroupings(adaptiveFormats, metadata.isPostLiveDvr, isLive)
|
||||
audioGroups = groupings.audio
|
||||
videoGroups = groupings.video
|
||||
|
||||
@@ -86,16 +97,51 @@ class DashManifest
|
||||
xml.Comment(`Generated by Playlet - Video ID: ${metadata.videoId}`)
|
||||
xml.Comment(`Parser: ${ValidString(metadata.parser)}`)
|
||||
|
||||
if xml.Node("MPD", {
|
||||
"xmlns": "urn:mpeg:dash:schema:mpd:2011"
|
||||
"profiles": "urn:mpeg:dash:profile:full:2011"
|
||||
"minBufferTime": "PT1.5S"
|
||||
"type": "static"
|
||||
"mediaPresentationDuration": `PT${duration}S`
|
||||
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"
|
||||
"xsi:schemaLocation": "urn:mpeg:dash:schema:mpd:2011 http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-DASH_schema_files/DASH-MPD.xsd"
|
||||
})
|
||||
if xml.Node("Period")
|
||||
mpdAttributes = {
|
||||
"xmlns": "urn:mpeg:dash:schema:mpd:2011"
|
||||
"minBufferTime": "PT1.5S"
|
||||
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"
|
||||
"xsi:schemaLocation": "urn:mpeg:dash:schema:mpd:2011 http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-DASH_schema_files/DASH-MPD.xsd"
|
||||
}
|
||||
if isLive
|
||||
' Dynamic (live) manifest: the player derives the current segment from
|
||||
' wall-clock against availabilityStartTime, and the duration-based
|
||||
' SegmentTemplate (see GenerateSegmentInformation) auto-extends as the
|
||||
' stream advances. timeShiftBufferDepth exposes the DVR window.
|
||||
segmentSec = Cint(m.GetLiveFragmentDurationMs(metadata) * LIVE_SEGMENT_GROUP / 1000.0)
|
||||
if segmentSec < 1
|
||||
segmentSec = 1
|
||||
end if
|
||||
dvrWindowSec = ValidInt(metadata.liveDvrWindowSec)
|
||||
mpdAttributes["profiles"] = "urn:mpeg:dash:profile:isoff-live:2011"
|
||||
mpdAttributes["type"] = "dynamic"
|
||||
mpdAttributes["minimumUpdatePeriod"] = `PT${segmentSec}S`
|
||||
mpdAttributes["suggestedPresentationDelay"] = `PT${segmentSec * 3}S`
|
||||
if not StringUtils.IsNullOrEmpty(metadata.liveAvailabilityStartUtc)
|
||||
mpdAttributes["availabilityStartTime"] = metadata.liveAvailabilityStartUtc
|
||||
end if
|
||||
if dvrWindowSec > 0
|
||||
mpdAttributes["timeShiftBufferDepth"] = `PT${dvrWindowSec}S`
|
||||
else
|
||||
LogWarn("[LIVE-MPD] dvrWindowSec is 0/missing -> NO timeShiftBufferDepth (live seek may fail). metadata.liveDvrWindowSec:", metadata.liveDvrWindowSec)
|
||||
end if
|
||||
' LogInfo("[LIVE-MPD] live MPD attrs: availabilityStartTime:", metadata.liveAvailabilityStartUtc, "segmentSec(group-adjusted):", segmentSec, "dvrWindowSec:", dvrWindowSec, "liveHeadSeqnum:", metadata.liveHeadSeqnum, "baseUrl:", m.liveSegmentBaseUrl)
|
||||
else
|
||||
mpdAttributes["profiles"] = "urn:mpeg:dash:profile:full:2011"
|
||||
mpdAttributes["type"] = "static"
|
||||
mpdAttributes["mediaPresentationDuration"] = `PT${duration}S`
|
||||
end if
|
||||
|
||||
' Dynamic (live) manifests need an explicit Period start so the player can
|
||||
' anchor the timeline against availabilityStartTime; static keeps the
|
||||
' attribute-less Period it has always used.
|
||||
periodAttributes = {}
|
||||
if isLive
|
||||
periodAttributes["start"] = "PT0S"
|
||||
end if
|
||||
|
||||
if xml.Node("MPD", mpdAttributes)
|
||||
if xml.Node("Period", periodAttributes)
|
||||
|
||||
id = 0
|
||||
id = m.GenerateAudioAdaptationSets(xml, audioGroups, id, metadata)
|
||||
@@ -122,14 +168,33 @@ class DashManifest
|
||||
return ValidInt(metadata["lengthSeconds"])
|
||||
end function
|
||||
|
||||
function CreateFormatGroupings(formats as object, isPostLiveDvr as boolean) as object
|
||||
' Representative segment duration for a live stream, in whole seconds.
|
||||
' Effective per-native-fragment duration (ms) for live. Prefers the value
|
||||
' derived from the live head (headTime/headSeqnum, set by GetLiveStreamInfo)
|
||||
' since YouTube's reported targetDurationSec is unreliable for long-running
|
||||
' streams; falls back to targetDurationSec, then to 2s.
|
||||
function GetLiveFragmentDurationMs(metadata as object) as integer
|
||||
fragMs = ValidInt(metadata.liveFragmentDurationMs)
|
||||
if fragMs > 0
|
||||
return fragMs
|
||||
end if
|
||||
targetSec = ValidInt(ObjectUtils.Dig(metadata, ["adaptiveFormats", 0, "targetDurationSec"]))
|
||||
if targetSec < 1
|
||||
targetSec = 2
|
||||
end if
|
||||
return targetSec * 1000
|
||||
end function
|
||||
|
||||
function CreateFormatGroupings(formats as object, isPostLiveDvr as boolean, isLive as boolean) as object
|
||||
groupInfo = {}
|
||||
|
||||
for each format in formats
|
||||
isOtf = format.isTypeOTF = true
|
||||
hasInit = not StringUtils.IsNullOrEmpty(format.init)
|
||||
hasIndex = not StringUtils.IsNullOrEmpty(format.index)
|
||||
isValid = (hasInit and hasIndex) or isOtf or isPostLiveDvr
|
||||
' Live formats (like Post-Live DVR / OTF) have no init/index ranges;
|
||||
' they are segmented via `&sq=N`.
|
||||
isValid = (hasInit and hasIndex) or isOtf or isPostLiveDvr or isLive
|
||||
if not isValid
|
||||
continue for
|
||||
end if
|
||||
@@ -167,11 +232,6 @@ class DashManifest
|
||||
colorInfo = `${format.colorInfo.primaries}-${format.colorInfo.transferCharacteristics}-${format.colorInfo.matrixCoefficients}`
|
||||
end if
|
||||
|
||||
audioTrackId = ""
|
||||
if IsAssociativeArray(format.audioTrack) and IsString(format.audioTrack.id)
|
||||
audioTrackId = format.audioTrack.id
|
||||
end if
|
||||
|
||||
if format.type.startsWith("audio")
|
||||
xtagsInfo = m.GetXtagsInfo(format)
|
||||
format.xtagsInfo = xtagsInfo
|
||||
@@ -181,6 +241,21 @@ class DashManifest
|
||||
format.isDescriptive = xtagsInfo.isDescriptive
|
||||
format.isSecondary = xtagsInfo.isSecondary
|
||||
format.isOriginal = xtagsInfo.isOriginal
|
||||
|
||||
' Some sources (e.g. Invidious) don't provide an `audioTrack` object the
|
||||
' way Innertube does; the language only lives in the URL's xtags. Without
|
||||
' an audioTrack, every language collapses into a single AdaptationSet and
|
||||
' the device labels each track "Other". Synthesize a minimal audioTrack
|
||||
' from xtags so tracks group per language and carry a `lang` attribute the
|
||||
' device maps to a real name.
|
||||
if not IsAssociativeArray(format.audioTrack) and not StringUtils.IsNullOrEmpty(xtagsInfo.lang)
|
||||
format.audioTrack = m.SynthesizeAudioTrack(xtagsInfo)
|
||||
end if
|
||||
end if
|
||||
|
||||
audioTrackId = ""
|
||||
if IsAssociativeArray(format.audioTrack) and IsString(format.audioTrack.id)
|
||||
audioTrackId = format.audioTrack.id
|
||||
end if
|
||||
|
||||
drc = ""
|
||||
@@ -513,6 +588,33 @@ class DashManifest
|
||||
end if
|
||||
xml.End()
|
||||
end if
|
||||
else if metadata.liveNow = true
|
||||
' Live: segments are `&sq=N` with the live edge advancing over time. A
|
||||
' duration-based SegmentTemplate (no fixed SegmentTimeline) lets the
|
||||
' player compute the current segment from availabilityStartTime and keep
|
||||
' extending as the stream grows.
|
||||
|
||||
' Live segments are self-initializing (ftyp+moov+emsg+moof+mdat) and
|
||||
' YouTube serves no clean init or moof-only media, so Roku can't demux
|
||||
' them directly (it reads the init segment once; a per-segment moov
|
||||
' yields `error -36`). The /api/dash/live-seg proxy transforms them
|
||||
' (init -> ftyp+moov, media -> moof+mdat) and groups LIVE_SEGMENT_GROUP
|
||||
' native fragments into each DASH segment. The manifest just addresses
|
||||
' the proxy by itag + DASH segment number ($Number$).
|
||||
' Per-fragment duration x GROUP. NOT targetDurationSec (unreliable for
|
||||
' long streams - see GetLiveFragmentDurationMs); the head-derived value
|
||||
' keeps the player's wall-clock -> $Number$ -> sq mapping on the live edge.
|
||||
durationMs = m.GetLiveFragmentDurationMs(metadata) * LIVE_SEGMENT_GROUP
|
||||
' The `v` (video id) lets the proxy cache the per-video format lookup.
|
||||
segBase = `${m.liveSegmentBaseUrl}/api/dash/live-seg?v=${metadata.videoId}&itag=${stream.itag}`
|
||||
' LogInfo("[LIVE-MPD] SegmentTemplate itag:", stream.itag, "targetDurationSec:", stream.targetDurationSec, "group:", LIVE_SEGMENT_GROUP, "durationMs:", durationMs)
|
||||
xml.EmptyNode("SegmentTemplate", {
|
||||
"initialization": `${segBase}&sq=init`
|
||||
"media": `${segBase}&sq=$Number$`
|
||||
"timescale": "1000"
|
||||
"duration": durationMs
|
||||
"startNumber": "0"
|
||||
})
|
||||
else
|
||||
xml.TextNode("BaseURL", ValidString(stream.url))
|
||||
|
||||
@@ -794,6 +896,25 @@ class DashManifest
|
||||
return result
|
||||
end function
|
||||
|
||||
' Builds a minimal audioTrack for sources that only encode the language in the
|
||||
' stream URL's xtags (e.g. Invidious), mirroring the shape Innertube provides.
|
||||
' The id keeps each language + content variant (original/dubbed/...) in its own
|
||||
' group; languageCode is the base ISO code (e.g. "en" from "en-US") that the
|
||||
' device maps to a display name. No displayName is set on purpose: without a
|
||||
' Label, the device derives the name from `lang` ("English") rather than echoing
|
||||
' the raw code.
|
||||
function SynthesizeAudioTrack(xtagsInfo as object) as object
|
||||
lang = xtagsInfo.lang
|
||||
id = lang
|
||||
if not StringUtils.IsNullOrEmpty(xtagsInfo.acont)
|
||||
id = `${lang}.${xtagsInfo.acont}`
|
||||
end if
|
||||
return {
|
||||
"id": id
|
||||
"languageCode": lang.Tokenize("-")[0]
|
||||
}
|
||||
end function
|
||||
|
||||
function GetDrmSystemId(drmFamily as dynamic) as dynamic
|
||||
if not IsString(drmFamily)
|
||||
return invalid
|
||||
|
||||
Reference in new issue
Block a user