Skip to content

Commit

Permalink
Multi language audio tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
iBicha committed Oct 14, 2023
1 parent b3a2095 commit 4a3a1cf
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import "pkg:/components/Web/WebServer/Http/HttpUtils.bs"
import "pkg:/source/services/XmlObject.bs"
import "pkg:/source/utils/StringUtils.bs"

class DashManifest

Expand All @@ -7,6 +9,7 @@ class DashManifest
public invidiousInstance as dynamic

function new(invidiousInstance as string)
m.utils = new Http.Utils()
m.invidiousInstance = invidiousInstance
end function

Expand All @@ -26,7 +29,7 @@ class DashManifest
videoStreams = m.getVideoStreams(adaptiveFormats)

videoMimeTypes = m.getMimeTypes(videoStreams)
audioMimeTypes = m.getMimeTypes(audioStreams)
audioSets = m.getMimeTypeLanguageCombos(audioStreams)

xml = new XmlObject()

Expand All @@ -40,23 +43,28 @@ class DashManifest
if xml.Node("Period")

id = 0
for each mimeType in audioMimeTypes
if xml.Node("AdaptationSet", {
"id": `${id}`,
"mimeType": mimeType,
"startWithSAP": "1",
"subsegmentAlignment": "true"
})
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": id = 0 ? "main" : "alternate"
"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 <> mimeType
if streamMimeType <> audioSet.mimeType or stream.languageInfo.lang <> audioSet.lang or stream.languageInfo.acont <> audioSet.acont
continue for
end if

Expand Down Expand Up @@ -185,6 +193,7 @@ class DashManifest
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
Expand Down Expand Up @@ -215,4 +224,48 @@ class DashManifest
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "pkg:/components/Services/Dash/DashManifest.bs"
import "pkg:/components/Services/Invidious/InvidiousService.bs"
import "pkg:/source/services/DashManifest.bs"
import "pkg:/source/utils/Types.bs"

namespace Http
Expand Down

0 comments on commit 4a3a1cf

Please sign in to comment.