481 lines
19 KiB
Plaintext
481 lines
19 KiB
Plaintext
import "pkg:/components/Web/WebServer/Http/HttpUtils.bs"
|
|
import "pkg:/source/services/XmlObject.bs"
|
|
import "pkg:/source/utils/Logging.bs"
|
|
import "pkg:/source/utils/StringUtils.bs"
|
|
import "pkg:/source/utils/UrlUtils.bs"
|
|
|
|
class DashManifest
|
|
|
|
public invidiousInstance as dynamic
|
|
|
|
function new(invidiousInstance as string)
|
|
m.invidiousInstance = invidiousInstance
|
|
|
|
m.utils = new Http.Utils()
|
|
m.urlRegex = /https?:\/\/\S+(\/\S+)/i
|
|
|
|
' https://github.com/LuanRT/YouTube.js/blob/6082b4a52ee07a622735e6e9128a0531a5ae3bfb/src/utils/StreamingInfo.ts#L332-L349
|
|
m.colorInfo = {
|
|
primaries: {
|
|
"COLOR_PRIMARIES_BT709": "1"
|
|
"COLOR_PRIMARIES_BT2020": "9"
|
|
}
|
|
transferCharacteristics: {
|
|
"COLOR_TRANSFER_CHARACTERISTICS_BT709": "1"
|
|
"COLOR_TRANSFER_CHARACTERISTICS_BT2020_10": "14"
|
|
"COLOR_TRANSFER_CHARACTERISTICS_SMPTEST2084": "16"
|
|
"COLOR_TRANSFER_CHARACTERISTICS_ARIB_STD_B67": "18"
|
|
}
|
|
matrixCoefficients: {
|
|
"COLOR_MATRIX_COEFFICIENTS_BT709": "1"
|
|
"COLOR_MATRIX_COEFFICIENTS_BT2020_NCL": "14"
|
|
}
|
|
}
|
|
end function
|
|
|
|
function fromVideoMetadata(metadata as object, qualityFilter as object, local as boolean) as string
|
|
adaptiveFormats = []
|
|
adaptiveFormats.Append(metadata.adaptiveFormats)
|
|
|
|
if local
|
|
for i = 0 to adaptiveFormats.Count() - 1
|
|
adaptiveFormats[i].url = m.urlRegex.replace(adaptiveFormats[i].url, m.invidiousInstance + "\1")
|
|
end for
|
|
end if
|
|
|
|
audioStreams = m.getAudioStreams(adaptiveFormats)
|
|
videoStreams = m.getVideoStreams(adaptiveFormats, qualityFilter)
|
|
if videoStreams.Count() = 0
|
|
LogWarn("No video streams found using quality filter:", qualityFilter, "Falling back to all video streams")
|
|
videoStreams = m.getVideoStreams(adaptiveFormats, {})
|
|
end if
|
|
|
|
videoMimeTypes = m.getMimeTypes(videoStreams)
|
|
audioSets = m.getMimeTypeLanguageCombos(audioStreams)
|
|
storyboards = metadata.storyboards
|
|
|
|
xml = new XmlObject()
|
|
|
|
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${metadata.lengthSeconds ?? 0}S`
|
|
})
|
|
if xml.Node("Period")
|
|
|
|
id = 0
|
|
for each audioSet in audioSets
|
|
adaptationSetAttributes = {
|
|
"id": `${id}`
|
|
"mimeType": audioSet.mimeType
|
|
"startWithSAP": "1"
|
|
"subsegmentAlignment": "true"
|
|
}
|
|
if not StringUtils.IsNullOrEmpty(audioSet.lang)
|
|
adaptationSetAttributes["lang"] = audioSet.lang
|
|
end if
|
|
|
|
if xml.Node("AdaptationSet", adaptationSetAttributes)
|
|
xml.EmptyNode("Role", {
|
|
"schemeIdUri": "urn:mpeg:dash:role:2011"
|
|
"value": audioSet.acont = "original" ? "main" : "alternate"
|
|
})
|
|
|
|
for i = 0 to audioStreams.Count() - 1
|
|
stream = audioStreams[i]
|
|
streamMimeType = stream.type.Tokenize(";")[0]
|
|
if streamMimeType <> audioSet.mimeType or stream.languageInfo.lang <> audioSet.lang or stream.languageInfo.acont <> audioSet.acont
|
|
continue for
|
|
end if
|
|
|
|
codecs = stream.type.split("codecs=")[1]
|
|
if codecs.startsWith(`"`)
|
|
codecs = codecs.mid(1)
|
|
end if
|
|
if codecs.endsWith(`"`)
|
|
codecs = codecs.left(codecs.len() - 1)
|
|
end if
|
|
|
|
if xml.Node("Representation", {
|
|
"id": stream.itag
|
|
"codecs": codecs
|
|
"bandwidth": stream.bitrate
|
|
})
|
|
|
|
xml.EmptyNode("AudioChannelConfiguration", {
|
|
"schemeIdUri": "urn:mpeg:dash:23003:3:audio_channel_configuration:2011"
|
|
"value": `${stream.audioChannels}`
|
|
})
|
|
|
|
xml.TextNode("BaseURL", stream.url)
|
|
|
|
if xml.Node("SegmentBase", { "indexRange": stream.index })
|
|
xml.EmptyNode("Initialization", { "range": stream.init })
|
|
|
|
xml.End()
|
|
end if
|
|
|
|
xml.End()
|
|
end if
|
|
end for
|
|
|
|
xml.End()
|
|
end if
|
|
|
|
id += 1
|
|
end for
|
|
|
|
for each mimeType in videoMimeTypes
|
|
if xml.Node("AdaptationSet", {
|
|
"id": `${id}`
|
|
"mimeType": mimeType
|
|
"startWithSAP": "1"
|
|
"subsegmentAlignment": "true"
|
|
})
|
|
|
|
for i = 0 to videoStreams.Count() - 1
|
|
stream = videoStreams[i]
|
|
streamMimeType = stream.type.Tokenize(";")[0]
|
|
if streamMimeType <> mimeType
|
|
continue for
|
|
end if
|
|
|
|
codecs = stream.type.split("codecs=")[1]
|
|
if codecs.startsWith(`"`)
|
|
codecs = codecs.mid(1)
|
|
end if
|
|
if codecs.endsWith(`"`)
|
|
codecs = codecs.left(codecs.len() - 1)
|
|
end if
|
|
|
|
representationAttributes = {
|
|
"id": stream.itag
|
|
"codecs": codecs
|
|
"startWithSAP": "1"
|
|
"maxPlayoutRate": "1"
|
|
"bandwidth": stream.bitrate
|
|
}
|
|
if stream.width <> invalid
|
|
representationAttributes["width"] = `${stream.width}`
|
|
end if
|
|
if stream.height <> invalid
|
|
representationAttributes["height"] = `${stream.height}`
|
|
end if
|
|
if stream.fps <> invalid
|
|
representationAttributes["frameRate"] = `${stream.fps}`
|
|
end if
|
|
|
|
if xml.Node("Representation", representationAttributes)
|
|
m.AddColorInfo(stream, xml)
|
|
if xml.Node("BaseURL")
|
|
xml.Text(stream.url)
|
|
xml.End()
|
|
end if
|
|
|
|
if xml.Node("SegmentBase", { "indexRange": stream.index })
|
|
xml.EmptyNode("Initialization", { "range": stream.init })
|
|
|
|
xml.End()
|
|
end if
|
|
|
|
xml.End()
|
|
end if
|
|
end for
|
|
|
|
xml.End()
|
|
end if
|
|
|
|
id += 1
|
|
end for
|
|
|
|
' https://developer.roku.com/en-ca/docs/developer-program/media-playback/trick-mode/hls-and-dash.md#dash-standard-thumbnail-tiles
|
|
for each storyboard in storyboards
|
|
interval = ValidInt(storyboard.interval)
|
|
if interval = 0
|
|
LogWarn("Invalid storyboard interval:", storyboard.interval)
|
|
continue for
|
|
end if
|
|
|
|
templateUrl = storyboard.templateUrl
|
|
if templateUrl.Instr("$M") = -1
|
|
LogWarn("Invalid storyboard url: url does not contain $M:", templateUrl)
|
|
continue for
|
|
end if
|
|
|
|
storyboardWidth = storyboard.storyboardWidth
|
|
storyboardHeight = storyboard.storyboardHeight
|
|
storyboardCount = storyboard.storyboardCount
|
|
tileWidthInPixels = storyboard.width
|
|
tileHeightInPixels = storyboard.height
|
|
totalTileCount = storyboard.count
|
|
|
|
' For the last page, the image height might be smaller than the storyboard height
|
|
if storyboardCount = 1
|
|
storyboardHeight = totalTileCount \ storyboardWidth
|
|
if totalTileCount mod storyboardWidth > 0
|
|
storyboardHeight += 1
|
|
end if
|
|
end if
|
|
|
|
intervalInSeconds = interval \ 1000
|
|
|
|
' YouTube template uses the var $M for tile pages
|
|
' DASH-IF uses $Number$ in the SegmentTemplate
|
|
' https://dashif.org/docs/DASH-IF-IOP-v4.3.pdf
|
|
url = templateUrl.replace("$M", "$Number$")
|
|
|
|
tilesPerPage = storyboardWidth * storyboardHeight
|
|
duration = tilesPerPage * intervalInSeconds
|
|
|
|
storyboardWidthInPixels = tileWidthInPixels * storyboardWidth
|
|
storyboardHeightInPixels = tileHeightInPixels * storyboardHeight
|
|
|
|
' Bandwidth is kind of a guess...
|
|
bandwidth = ((storyboardWidthInPixels * storyboardHeightInPixels * 0.5) / duration)
|
|
|
|
if xml.Node("AdaptationSet", {
|
|
"id": `${id}`
|
|
"mimeType": "image/jpeg"
|
|
"contentType": "image"
|
|
})
|
|
xml.EmptyNode("SegmentTemplate", {
|
|
"media": url
|
|
"duration": duration
|
|
"startNumber": 0
|
|
})
|
|
|
|
if xml.Node("Representation", {
|
|
"id": `thumbnails_${id}`
|
|
"bandwidth": bandwidth
|
|
"width": storyboardWidthInPixels
|
|
"height": storyboardHeightInPixels
|
|
})
|
|
|
|
xml.EmptyNode("EssentialProperty", {
|
|
"schemeIdUri": "http://dashif.org/guidelines/thumbnail_tile"
|
|
"value": `${storyboardWidth}x${storyboardHeight}`
|
|
})
|
|
|
|
xml.End()
|
|
end if
|
|
|
|
xml.End()
|
|
end if
|
|
|
|
id += 1
|
|
end for
|
|
|
|
xml.End()
|
|
end if
|
|
xml.End()
|
|
end if
|
|
|
|
return xml.ToXmlString()
|
|
end function
|
|
|
|
function getAudioStreams(streams as object) as object
|
|
audioStreams = []
|
|
for i = 0 to streams.Count() - 1
|
|
stream = streams[i]
|
|
if stream.type.startsWith("audio/")
|
|
if stream.bitrate = invalid
|
|
LogWarn("Invalid audio stream bitrate, itag:", stream.itag)
|
|
continue for
|
|
end if
|
|
if stream.init = invalid
|
|
LogWarn("Invalid audio stream init, itag:", stream.itag)
|
|
continue for
|
|
end if
|
|
if stream.index = invalid
|
|
LogWarn("Invalid audio stream index, itag:", stream.itag)
|
|
continue for
|
|
end if
|
|
stream.languageInfo = m.ParseAudioLanguage(stream.url)
|
|
audioStreams.Push(stream)
|
|
end if
|
|
end for
|
|
|
|
audioStreams.SortBy("bitrate", "r")
|
|
return audioStreams
|
|
end function
|
|
|
|
function getVideoStreams(streams as object, qualityFilter as object) as object
|
|
videoStreams = []
|
|
for i = 0 to streams.Count() - 1
|
|
if not m.isVideoFormatIncluded(streams[i], qualityFilter)
|
|
continue for
|
|
end if
|
|
|
|
stream = streams[i]
|
|
if stream.type.startsWith("video/")
|
|
if stream.bitrate = invalid
|
|
LogWarn("Invalid video stream bitrate, itag:", stream.itag)
|
|
continue for
|
|
end if
|
|
if stream.init = invalid
|
|
LogWarn("Invalid video stream init, itag:", stream.itag)
|
|
continue for
|
|
end if
|
|
if stream.index = invalid
|
|
LogWarn("Invalid video stream index, itag:", stream.itag)
|
|
continue for
|
|
end if
|
|
|
|
if (stream.width = invalid or stream.height = invalid) and not StringUtils.IsNullOrEmpty(stream.size)
|
|
if stream.size.instr("x") > 0
|
|
parts = stream.size.Tokenize("x")
|
|
if parts.Count() = 2
|
|
stream.width = ValidInt(parts[0].toInt())
|
|
stream.height = ValidInt(parts[1].toInt())
|
|
end if
|
|
end if
|
|
end if
|
|
|
|
videoStreams.Push(stream)
|
|
end if
|
|
end for
|
|
|
|
videoStreams.SortBy("bitrate", "r")
|
|
return videoStreams
|
|
end function
|
|
|
|
function isVideoFormatIncluded(stream as object, qualityFilter as object) as boolean
|
|
if qualityFilter.Count() = 0
|
|
return true
|
|
end if
|
|
|
|
qualityLabel = stream.qualityLabel
|
|
if not StringUtils.IsNullOrEmpty(qualityLabel)
|
|
pIndex = qualityLabel.InStr("p")
|
|
if pIndex <> -1
|
|
qualityLabel = qualityLabel.Mid(0, pIndex)
|
|
end if
|
|
if qualityFilter[qualityLabel] = true
|
|
return true
|
|
end if
|
|
end if
|
|
|
|
resolution = stream.resolution
|
|
if not StringUtils.IsNullOrEmpty(resolution)
|
|
pIndex = resolution.InStr("p")
|
|
if pIndex <> -1
|
|
resolution = resolution.Mid(0, pIndex)
|
|
end if
|
|
if qualityFilter[resolution] = true
|
|
return true
|
|
end if
|
|
end if
|
|
|
|
size = stream.size
|
|
if not StringUtils.IsNullOrEmpty(size)
|
|
size = size.Split("x")
|
|
if size.Count() = 2
|
|
if qualityFilter[size[1]] = true
|
|
return true
|
|
end if
|
|
end if
|
|
end if
|
|
|
|
return false
|
|
end function
|
|
|
|
function getMimeTypes(streams as object) as object
|
|
mimeTypes = {}
|
|
for i = 0 to streams.Count() - 1
|
|
stream = streams[i]
|
|
mimeType = stream.type.Tokenize(";")[0]
|
|
mimeTypes[mimeType] = true
|
|
end for
|
|
return mimeTypes.Keys()
|
|
end function
|
|
|
|
function getMimeTypeLanguageCombos(streams as object) as object
|
|
types = {}
|
|
for i = 0 to streams.Count() - 1
|
|
stream = streams[i]
|
|
mimeType = stream.type.Tokenize(";")[0]
|
|
types[`${mimeType}-${stream.languageInfo.lang}-${stream.languageInfo.acont}`] = {
|
|
"mimeType": mimeType
|
|
"lang": stream.languageInfo.lang
|
|
"acont": stream.languageInfo.acont
|
|
}
|
|
end for
|
|
|
|
combos = []
|
|
for each t in types
|
|
combos.Push(types[t])
|
|
end for
|
|
|
|
return combos
|
|
end function
|
|
|
|
function ParseAudioLanguage(url as string) as object
|
|
result = { lang: "", acont: "" }
|
|
queryParams = UrlUtils.ParseQueryComponents(url)
|
|
if StringUtils.IsNullOrEmpty(queryParams["xtags"])
|
|
return result
|
|
end if
|
|
|
|
xtags = queryParams["xtags"].split(":")
|
|
for each xtag in xtags
|
|
if xtag.instr("=") > 0
|
|
parts = xtag.split("=")
|
|
if parts.Count() = 2
|
|
if parts[0] = "lang"
|
|
result["lang"] = parts[1]
|
|
end if
|
|
if parts[0] = "acont"
|
|
result["acont"] = parts[1]
|
|
end if
|
|
end if
|
|
end if
|
|
end for
|
|
return result
|
|
end function
|
|
|
|
function AddColorInfo(videoStream as object, xml as object) as void
|
|
if videoStream.colorInfo = invalid
|
|
return
|
|
end if
|
|
|
|
if videoStream.colorInfo.primaries <> invalid
|
|
primaries = m.colorInfo.primaries[videoStream.colorInfo.primaries]
|
|
if primaries <> invalid
|
|
xml.EmptyNode("EssentialProperty", {
|
|
"schemeIdUri": "urn:mpeg:mpegB:cicp:ColourPrimaries"
|
|
"value": primaries
|
|
})
|
|
else
|
|
LogWarn("Unknown colorInfo.primaries:", videoStream.colorInfo.primaries)
|
|
end if
|
|
end if
|
|
|
|
if videoStream.colorInfo.transferCharacteristics <> invalid
|
|
transferCharacteristics = m.colorInfo.transferCharacteristics[videoStream.colorInfo.transferCharacteristics]
|
|
if transferCharacteristics <> invalid
|
|
xml.EmptyNode("EssentialProperty", {
|
|
"schemeIdUri": "urn:mpeg:mpegB:cicp:TransferCharacteristics"
|
|
"value": transferCharacteristics
|
|
})
|
|
else
|
|
LogWarn("Unknown colorInfo.transferCharacteristics:", videoStream.colorInfo.transferCharacteristics)
|
|
end if
|
|
end if
|
|
|
|
if videoStream.colorInfo.matrixCoefficients <> invalid
|
|
matrixCoefficients = m.colorInfo.matrixCoefficients[videoStream.colorInfo.matrixCoefficients]
|
|
if matrixCoefficients <> invalid
|
|
xml.EmptyNode("EssentialProperty", {
|
|
"schemeIdUri": "urn:mpeg:mpegB:cicp:MatrixCoefficients"
|
|
"value": matrixCoefficients
|
|
})
|
|
else
|
|
LogWarn("Unknown colorInfo.matrixCoefficients:", videoStream.colorInfo.matrixCoefficients)
|
|
end if
|
|
end if
|
|
end function
|
|
|
|
end class
|