Feature/local dash (#166)
* Generate dash locally * Refactor xml gen * Lint fix * Add feature flag * Multi language audio tracks * Unused var * Set video expiration to 5 hours --------- Co-authored-by: github-action linter <githubaction@githubaction.com>
This commit is contained in:
1 parent
fc3629de09
commit
fc161ffe4c
10 files changed
+444
-13
No files matched your search
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
|
||||
- Local DASH manifest generation
|
||||
- This adds support for multi languages audio tracks
|
||||
|
||||
### Fixed
|
||||
|
||||
- Layout in channel view so that the upload time and view count of videos is visible
|
||||
|
||||
@@ -0,0 +1,269 @@
|
||||
import "pkg:/components/Web/WebServer/Http/HttpUtils.bs"
|
||||
import "pkg:/source/services/XmlObject.bs"
|
||||
import "pkg:/source/utils/StringUtils.bs"
|
||||
|
||||
class DashManifest
|
||||
|
||||
public invidiousInstance as dynamic
|
||||
|
||||
function new(invidiousInstance as string)
|
||||
m.utils = new Http.Utils()
|
||||
m.invidiousInstance = invidiousInstance
|
||||
end function
|
||||
|
||||
function fromVideoMetadata(metadata as object, local as boolean) as string
|
||||
adaptiveFormats = []
|
||||
adaptiveFormats.Append (metadata.adaptiveFormats)
|
||||
|
||||
if local
|
||||
urlRegex = CreateObject("roRegex", "https?:\/\/\S+(\/\S+)", "i")
|
||||
|
||||
for i = 0 to adaptiveFormats.Count() - 1
|
||||
adaptiveFormats[i].url = urlRegex.replace(adaptiveFormats[i].url, m.invidiousInstance + "\1")
|
||||
end for
|
||||
end if
|
||||
|
||||
audioStreams = m.getAudioStreams(adaptiveFormats)
|
||||
videoStreams = m.getVideoStreams(adaptiveFormats)
|
||||
|
||||
videoMimeTypes = m.getMimeTypes(videoStreams)
|
||||
audioSets = m.getMimeTypeLanguageCombos(audioStreams)
|
||||
|
||||
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.Node("Role", {
|
||||
"schemeIdUri": "urn:mpeg:dash:role:2011",
|
||||
"value": audioSet.acont = "original" ? "main" : "alternate"
|
||||
})
|
||||
xml.End()
|
||||
|
||||
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.Node("AudioChannelConfiguration", {
|
||||
"schemeIdUri": "urn:mpeg:dash:23003:3:audio_channel_configuration:2011",
|
||||
"value": `${stream.audioChannels}`
|
||||
})
|
||||
xml.End()
|
||||
|
||||
if xml.Node("BaseURL")
|
||||
xml.Text(stream.url)
|
||||
xml.End()
|
||||
end if
|
||||
|
||||
if xml.Node("SegmentBase", {
|
||||
"indexRange": stream.index
|
||||
})
|
||||
if xml.Node("Initialization", {
|
||||
"range": stream.init
|
||||
})
|
||||
xml.End()
|
||||
end if
|
||||
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)
|
||||
if xml.Node("BaseURL")
|
||||
xml.Text(stream.url)
|
||||
xml.End()
|
||||
end if
|
||||
|
||||
if xml.Node("SegmentBase", {
|
||||
"indexRange": stream.index
|
||||
})
|
||||
if xml.Node("Initialization", {
|
||||
"range": stream.init
|
||||
})
|
||||
xml.End()
|
||||
end if
|
||||
xml.End()
|
||||
end if
|
||||
|
||||
xml.End()
|
||||
end if
|
||||
end for
|
||||
|
||||
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/") and stream.bitrate <> invalid and stream.init <> invalid and stream.index <> invalid
|
||||
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) as object
|
||||
videoStreams = []
|
||||
for i = 0 to streams.Count() - 1
|
||||
stream = streams[i]
|
||||
if stream.type.startsWith("video/") and stream.init <> invalid and stream.index <> invalid
|
||||
videoStreams.Push(stream)
|
||||
end if
|
||||
end for
|
||||
|
||||
videoStreams.SortBy("width", "r")
|
||||
return videoStreams
|
||||
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 = m.utils.ParseUrlQueryComponents(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
|
||||
end class
|
||||
@@ -99,7 +99,11 @@ namespace Invidious
|
||||
request = HttpClient.Get(url)
|
||||
request.QueryParams(m.DefaultQueryParams())
|
||||
request.QueryParam("q", q)
|
||||
request.CacheSeconds(60 * 60 * 6)' 6 hours
|
||||
' 5 hours cache. Videos should expire in 6 hours, but since we don't
|
||||
' have the exact expiration time, we'll use 5 hours. Invidious currently
|
||||
' Does a fresh fetch if the cache is older than 10 minutes.
|
||||
' https://github.com/iv-org/invidious/blob/069e91d2a609b1df94bdfbb693335969b2a281d8/src/invidious/videos.cr#L349
|
||||
request.CacheSeconds(60 * 60 * 5)
|
||||
|
||||
request.Cancellation(cancellation)
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ function VideoContentTask(input as object) as object
|
||||
contentNode = input.content
|
||||
invidiousNode = input.invidious
|
||||
preferencesNode = input.preferences
|
||||
playletServerUrl = input.playletServerUrl
|
||||
|
||||
if m.top.cancel
|
||||
return invalid
|
||||
@@ -34,7 +35,7 @@ function VideoContentTask(input as object) as object
|
||||
contentNode.title = metadata.title
|
||||
contentNode.secondaryTitle = metadata.author
|
||||
|
||||
streamUrls = CreateStreamUrls(metadata, service, preferencesNode)
|
||||
streamUrls = CreateStreamUrls(metadata, service, preferencesNode, playletServerUrl)
|
||||
contentNode.url = streamUrls[0]
|
||||
|
||||
SetCaptions(metadata, service, contentNode)
|
||||
@@ -51,7 +52,7 @@ function VideoContentTask(input as object) as object
|
||||
}
|
||||
end function
|
||||
|
||||
function CreateStreamUrls(metadata as object, service as Invidious.InvidiousService, preferences as object) as object
|
||||
function CreateStreamUrls(metadata as object, service as Invidious.InvidiousService, preferences as object, playletServerUrl as string) as object
|
||||
instance = service.GetInstance()
|
||||
proxyVideos = preferences["invidious.proxy_videos"]
|
||||
|
||||
@@ -64,9 +65,9 @@ function CreateStreamUrls(metadata as object, service as Invidious.InvidiousServ
|
||||
|
||||
if preferredQuality = "720p"
|
||||
AddStreamUrls(streamUrls, metadata, service, proxyVideos)
|
||||
AddDashUrls(streamUrls, metadata, proxyVideos)
|
||||
AddDashUrls(streamUrls, metadata, proxyVideos, playletServerUrl)
|
||||
else
|
||||
AddDashUrls(streamUrls, metadata, proxyVideos)
|
||||
AddDashUrls(streamUrls, metadata, proxyVideos, playletServerUrl)
|
||||
AddStreamUrls(streamUrls, metadata, service, proxyVideos)
|
||||
end if
|
||||
end if
|
||||
@@ -83,14 +84,18 @@ function AddHlsUrls(streamUrls as object, metadata as object, proxyVideos as str
|
||||
AddUrls(streamUrls, hlsUrl, hlsUrlLocal, proxyVideos)
|
||||
end function
|
||||
|
||||
function AddDashUrls(streamUrls as object, metadata as object, proxyVideos as string) as void
|
||||
if metadata.dashUrl = invalid
|
||||
return
|
||||
end if
|
||||
function AddDashUrls(streamUrls as object, metadata as object, proxyVideos as string, playletServerUrl as string) as void
|
||||
#if LOCAL_DASH
|
||||
dashUrl = `${playletServerUrl}/api/dash?v=${metadata.videoId}`
|
||||
#else
|
||||
if metadata.dashUrl = invalid
|
||||
return
|
||||
end if
|
||||
|
||||
dashUrl = metadata.dashUrl
|
||||
#end if
|
||||
|
||||
dashUrl = metadata.dashUrl
|
||||
dashUrlLocal = AddLocalFlag(dashUrl)
|
||||
|
||||
AddUrls(streamUrls, dashUrl, dashUrlLocal, proxyVideos)
|
||||
end function
|
||||
|
||||
|
||||
@@ -71,7 +71,8 @@ function StartVideoContentTask(videoContentNode as object)
|
||||
m.videoContentTask = StartAsyncTask(VideoContentTask, {
|
||||
content: videoContentNode,
|
||||
invidious: m.top.invidious,
|
||||
preferences: m.top.preferences
|
||||
preferences: m.top.preferences,
|
||||
playletServerUrl: m.top.webServer@.GetServerAddress(invalid)
|
||||
}, OnVideoContentTaskResults)
|
||||
end function
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
<field id="playQueue" type="node" bind="../../PlayQueue" />
|
||||
<field id="preferences" type="node" bind="../../Preferences" />
|
||||
<field id="invidious" type="node" bind="../../Invidious" />
|
||||
<field id="webServer" type="node" bind="../../WebServer" />
|
||||
<field id="minRect" type="rect2D" value="[834,460,426,240]" />
|
||||
<field id="maxRect" type="rect2D" value="[0,0,1280,720]" />
|
||||
<field id="chapter" type="string" alias="chapterLabel.text" />
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import "pkg:/components/Services/Dash/DashManifest.bs"
|
||||
import "pkg:/components/Services/Invidious/InvidiousService.bs"
|
||||
import "pkg:/source/utils/Types.bs"
|
||||
|
||||
namespace Http
|
||||
|
||||
class DashRouter extends HttpRouter
|
||||
|
||||
function new()
|
||||
super()
|
||||
|
||||
m.Get("/api/dash", function(context as object) as boolean
|
||||
request = context.request
|
||||
response = context.response
|
||||
task = context.server.task
|
||||
invidiousNode = task.invidious
|
||||
|
||||
v = request.query.v
|
||||
local = request.query.local
|
||||
|
||||
service = new Invidious.InvidiousService(invidiousNode)
|
||||
metadataRequest = service.GetVideoMetadata(v)
|
||||
if not metadataRequest.IsSuccess()
|
||||
response.Default(metadataRequest.StatusCode(), metadataRequest.ErrorMessage())
|
||||
return true
|
||||
end if
|
||||
|
||||
metadata = metadataRequest.Json()
|
||||
instance = service.GetInstance()
|
||||
|
||||
dash = new DashManifest(instance)
|
||||
xmlContent = dash.fromVideoMetadata(metadata, local <> invalid)
|
||||
|
||||
response.http_code = 200
|
||||
response.SetBodyDataString(xmlContent)
|
||||
response.ContentType("application/dash+xml")
|
||||
|
||||
return true
|
||||
end function)
|
||||
end function
|
||||
|
||||
end class
|
||||
|
||||
end namespace
|
||||
@@ -1,3 +1,4 @@
|
||||
import "pkg:/components/Web/PlayletWebServer/Middleware/DashRouter.bs"
|
||||
import "pkg:/components/Web/PlayletWebServer/Middleware/InvidiousRouter.bs"
|
||||
import "pkg:/components/Web/PlayletWebServer/Middleware/PlayletLibUrlsRouter.bs"
|
||||
import "pkg:/components/Web/PlayletWebServer/Middleware/PlayQueueRouter.bs"
|
||||
@@ -33,6 +34,7 @@ function SetupRoutes(server as object)
|
||||
server.UseRouter(new Http.CorsMiddleware())
|
||||
server.UseRouter(homeRouter)
|
||||
server.UseRouter(new Http.StateApiRouter())
|
||||
server.UseRouter(new Http.DashRouter())
|
||||
server.UseRouter(new Http.PreferencesRouter())
|
||||
server.UseRouter(new Http.InvidiousRouter())
|
||||
server.UseRouter(new Http.PlayQueueRouter())
|
||||
|
||||
@@ -12,4 +12,4 @@ hidden=1
|
||||
|
||||
git_commit_sha=unknown
|
||||
|
||||
bs_const=DEBUG=false
|
||||
bs_const=DEBUG=false;LOCAL_DASH=true
|
||||
@@ -0,0 +1,100 @@
|
||||
import "pkg:/source/utils/Types.bs"
|
||||
|
||||
class XmlObject
|
||||
private stack = []
|
||||
|
||||
private root = invalid
|
||||
|
||||
private INDENT_SIZE = 2
|
||||
|
||||
function new()
|
||||
m.escapeChars = {}
|
||||
m.escapeChars["&"] = "&"
|
||||
m.escapeChars["<"] = "<"
|
||||
m.escapeChars[">"] = ">"
|
||||
m.escapeChars[`"`] = `"`
|
||||
m.escapeChars["'"] = "'"
|
||||
end function
|
||||
|
||||
function Node(name as string, attributes = invalid as object) as boolean
|
||||
newNode = {
|
||||
name: name,
|
||||
attributes: attributes,
|
||||
children: []
|
||||
}
|
||||
|
||||
if m.root = invalid
|
||||
m.root = newNode
|
||||
end if
|
||||
|
||||
if m.stack.count() > 0
|
||||
m.stack[m.stack.count() - 1].children.push(newNode)
|
||||
end if
|
||||
|
||||
m.stack.push(newNode)
|
||||
return true
|
||||
end function
|
||||
|
||||
function Text(text as string)
|
||||
if m.stack.count() > 0
|
||||
m.stack[m.stack.count() - 1].children.push(text)
|
||||
end if
|
||||
end function
|
||||
|
||||
function End()
|
||||
m.stack.pop()
|
||||
end function
|
||||
|
||||
function ToXmlString() as string
|
||||
return `<?xml version="1.0" encoding="UTF-8"?>\n` + m.NodeToXmlString(m.root, 0)
|
||||
end function
|
||||
|
||||
function NodeToXmlString(node as object, indentLevel as integer) as string
|
||||
indentString = string(indentLevel * m.INDENT_SIZE, " ")
|
||||
xmlString = indentString + "<" + node.name
|
||||
|
||||
if node.attributes <> invalid
|
||||
for each key in node.attributes.keys()
|
||||
xmlString += ` ${key}="${node.attributes[key]}"`
|
||||
end for
|
||||
end if
|
||||
|
||||
if node.children.count() = 0
|
||||
xmlString += ` />\n`
|
||||
else
|
||||
xmlString += `>`
|
||||
|
||||
for i = 0 to node.children.count() - 1
|
||||
child = node.children[i]
|
||||
if i = 0
|
||||
if not IsString(child)
|
||||
xmlString += `\n`
|
||||
end if
|
||||
end if
|
||||
|
||||
if IsString(child)
|
||||
xmlString += m.XmlEscape(child)
|
||||
else
|
||||
xmlString += m.NodeToXmlString(child, indentLevel + 1)
|
||||
end if
|
||||
|
||||
if i = node.children.count() - 1
|
||||
if not IsString(child)
|
||||
xmlString += `${indentString}`
|
||||
end if
|
||||
end if
|
||||
end for
|
||||
xmlString += `</${node.name}>\n`
|
||||
end if
|
||||
|
||||
return xmlString
|
||||
end function
|
||||
|
||||
function XmlEscape(input as string) as string
|
||||
for each escChar in m.escapeChars
|
||||
input = input.replace(escChar, m.escapeChars[escChar])
|
||||
end for
|
||||
return input
|
||||
end function
|
||||
|
||||
end class
|
||||
Reference in new issue
Block a user