Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for auto-generated captions in Playlet backend #474

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Spanish translations (Thanks to Kamborio)
- Support for auto-generated captions in Playlet backend

## [0.28.0] - 2024-10-13

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ namespace InnertubeService
"hlsUrl": streamingData["hlsManifestUrl"]
"adaptiveFormats": []
"formatStreams": []
"captions": []
"captions": ParseCaptions(payload)
"recommendedVideos": []
}

Expand All @@ -256,6 +256,66 @@ namespace InnertubeService
}
end function

function ParseCaptions(payload as object) as object
captions = payload["captions"]
if not IsAssociativeArray(captions)
return []
end if

playerCaptions = captions["playerCaptionsTracklistRenderer"]
if not IsAssociativeArray(playerCaptions)
return []
end if

tracks = playerCaptions["captionTracks"]
if not IsArray(tracks)
return []
end if

captions = []
for each track in tracks
if not IsAssociativeArray(track)
continue for
end if

baseUrl = track["baseUrl"]
if not IsString(baseUrl)
continue for
end if

queryComponents = UrlUtils.ParseQueryComponents(baseUrl)
queryComponents["fmt"] = "vtt"
baseUrl = UrlUtils.SetQueryParams(baseUrl, queryComponents)

languageCode = track["languageCode"]
if not IsString(languageCode)
continue for
end if

label = ""
name = track["name"]
if IsAssociativeArray(name)
runs = name["runs"]
if IsArray(runs)
for each rn in runs
text = rn["text"]
if IsString(text)
label += text
end if
end for
end if
end if

captions.Push({
"label": label
"language_code": languageCode
"url": baseUrl
})
end for

return captions
end function

function ParseInnertubeNextResponse(payload as object, parsedResponse as object) as object
if not parsedResponse.Success
LogWarn("Invalid parsed response", parsedResponse)
Expand Down
7 changes: 6 additions & 1 deletion playlet-lib/src/components/VideoPlayer/VideoContentTask.bs
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,15 @@ function SetCaptions(metadata as object, service as object, contentNode as objec
code = isoLanguageList[code]
end if

url = caption.url
if not url.StartsWith("http")
url = instance + url
end if

subtitleTracks.Push({
Description: caption.label
Language: code
TrackName: instance + caption.url
TrackName: url
})
end for

Expand Down
10 changes: 9 additions & 1 deletion playlet-lib/src/source/utils/UrlUtils.bs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace UrlUtils

function BuildUrl(url as string, pathParams as object, queryParams as object) as string
url = SetPathParams(url, pathParams)
url = SetQueryParams(url, queryParams)
url = AppendQueryParams(url, queryParams)
return url
end function

Expand All @@ -34,6 +34,14 @@ namespace UrlUtils
end function

function SetQueryParams(url as string, queryParams as object) as string
questionMarkIndex = url.InStr("?")
if questionMarkIndex <> -1
url = url.Left(questionMarkIndex)
end if
return AppendQueryParams(url, queryParams)
end function

function AppendQueryParams(url as string, queryParams as object) as string
if queryParams = invalid or queryParams.Count() = 0
return url
end if
Expand Down