[skip ci] Redo OTF processing
This commit is contained in:
1 parent
c602fe3ed2
commit
a42c7221c6
5 files changed
+199
-156
No files matched your search
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
- Fix in url decipher logic
|
- Fix in url decipher logic
|
||||||
|
- Redo OTF segment processing
|
||||||
|
|
||||||
## [0.43.7] - 2025-11-06
|
## [0.43.7] - 2025-11-06
|
||||||
|
|
||||||
|
|||||||
@@ -496,11 +496,7 @@ class DashManifest
|
|||||||
xml.Comment(`[ERROR] OTF stream has empty otfSegmentDurations for itag: ${stream.itag}`)
|
xml.Comment(`[ERROR] OTF stream has empty otfSegmentDurations for itag: ${stream.itag}`)
|
||||||
end if
|
end if
|
||||||
for each segment in segmentDurations
|
for each segment in segmentDurations
|
||||||
sAttributes = { "d": segment.duration }
|
xml.EmptyNode("S", segment)
|
||||||
if segment.repeatCount <> invalid
|
|
||||||
sAttributes["r"] = segment.repeatCount
|
|
||||||
end if
|
|
||||||
xml.EmptyNode("S", sAttributes)
|
|
||||||
end for
|
end for
|
||||||
xml.End()
|
xml.End()
|
||||||
end if
|
end if
|
||||||
|
|||||||
@@ -0,0 +1,181 @@
|
|||||||
|
import "pkg:/components/Services/Innertube/Context.bs"
|
||||||
|
|
||||||
|
namespace Innertube
|
||||||
|
|
||||||
|
const OTF_MAX_REQUEST_COUNT = 8
|
||||||
|
|
||||||
|
function OTFGetSegmentInfo(adaptiveFormats as object, client as Innertube.ClientType, cancellation as object) as void
|
||||||
|
errors = {
|
||||||
|
message: ""
|
||||||
|
attachments: []
|
||||||
|
}
|
||||||
|
|
||||||
|
formatsToProcess = []
|
||||||
|
for i = 0 to adaptiveFormats.Count() - 1
|
||||||
|
formatsToProcess.Push({
|
||||||
|
format: adaptiveFormats[i]
|
||||||
|
file: `tmp:/otf_segments/${i}.bin`
|
||||||
|
})
|
||||||
|
end for
|
||||||
|
|
||||||
|
pendingFormats = []
|
||||||
|
|
||||||
|
requestHeaders = Innertube.CreateHeaders(client)
|
||||||
|
requestHeaders["origin"] = "https://www.youtube.com"
|
||||||
|
requestHeaders["referer"] = "https://www.youtube.com"
|
||||||
|
requestHeaders["DNT"] = "?1"
|
||||||
|
|
||||||
|
fs = CreateObject("roFileSystem")
|
||||||
|
fs.Delete("tmp:/otf_segments")
|
||||||
|
fs.CreateDirectory("tmp:/otf_segments")
|
||||||
|
|
||||||
|
while true
|
||||||
|
while formatsToProcess.Count() > 0 and pendingFormats.Count() < OTF_MAX_REQUEST_COUNT
|
||||||
|
formatToProcess = formatsToProcess.Pop()
|
||||||
|
request = OTFMakeSegmentInfoRequest(formatToProcess.format, formatToProcess.file, requestHeaders, errors, cancellation)
|
||||||
|
if request <> invalid
|
||||||
|
formatToProcess.request = request
|
||||||
|
pendingFormats.Push(formatToProcess)
|
||||||
|
end if
|
||||||
|
end while
|
||||||
|
|
||||||
|
if pendingFormats.Count() = 0
|
||||||
|
exit while
|
||||||
|
end if
|
||||||
|
|
||||||
|
pendingFormat = pendingFormats.Shift()
|
||||||
|
' returns false if processing should stop (e.g., cancelled)
|
||||||
|
if not OTFProcessPendingFormat(pendingFormat.format, pendingFormat.request, pendingFormat.file, errors)
|
||||||
|
exit while
|
||||||
|
end if
|
||||||
|
end while
|
||||||
|
|
||||||
|
fs.Delete("tmp:/otf_segments")
|
||||||
|
|
||||||
|
if not StringUtils.IsNullOrEmpty(errors.message) or errors.attachments.Count() > 0
|
||||||
|
LogTelemetryEvent({
|
||||||
|
level: "error"
|
||||||
|
message: `OTF errors during segment info retrieval: \n${errors.message}`
|
||||||
|
fingerprint: ["OTF errors during segment info retrieval"]
|
||||||
|
attachments: errors.attachments
|
||||||
|
})
|
||||||
|
end if
|
||||||
|
end function
|
||||||
|
|
||||||
|
function OTFMakeSegmentInfoRequest(format as object, file as string, requestHeaders as object, errors as object, cancellation as object) as object
|
||||||
|
url = format.url
|
||||||
|
if StringUtils.IsNullOrEmpty(url)
|
||||||
|
errors.message += `URL is empty for itag ${format.itag}.\n`
|
||||||
|
return invalid
|
||||||
|
end if
|
||||||
|
|
||||||
|
queryComponents = UrlUtils.ParseQueryComponents(url)
|
||||||
|
queryComponents["rn"] = "0"
|
||||||
|
queryComponents["sq"] = "0"
|
||||||
|
otfUrl = UrlUtils.SetQueryParams(url, queryComponents)
|
||||||
|
|
||||||
|
request = HttpClient.Get(otfUrl)
|
||||||
|
request.Headers(requestHeaders)
|
||||||
|
request.ToFile(file)
|
||||||
|
request.Cancellation(cancellation)
|
||||||
|
request.Send()
|
||||||
|
|
||||||
|
return request
|
||||||
|
end function
|
||||||
|
|
||||||
|
function OTFProcessPendingFormat(format as object, request as object, file as string, errors as object) as boolean
|
||||||
|
response = request.Await()
|
||||||
|
|
||||||
|
if response.IsCancelled()
|
||||||
|
return false
|
||||||
|
end if
|
||||||
|
|
||||||
|
if not response.IsSuccess()
|
||||||
|
errors.message += `Failed to get segment info for itag ${format.itag}: ${response.ErrorMessage()}\n`
|
||||||
|
return true
|
||||||
|
end if
|
||||||
|
|
||||||
|
segmentDurations = OTFParseFileForSegmentInfo(file, errors)
|
||||||
|
if segmentDurations = invalid
|
||||||
|
errors.message += `Failed to parse segment info for itag ${format.itag}.\n`
|
||||||
|
errors.message += `Headers: ${ToString(response.Headers())}\n`
|
||||||
|
return true
|
||||||
|
end if
|
||||||
|
|
||||||
|
format.otfSegmentInfo = segmentDurations
|
||||||
|
return true
|
||||||
|
end function
|
||||||
|
|
||||||
|
function OTFParseFileForSegmentInfo(filePath as string, errors as object) as dynamic
|
||||||
|
buffer = CreateObject("roByteArray")
|
||||||
|
|
||||||
|
buffer.FromAsciiString("Segment-Durations-Ms:")
|
||||||
|
startMarkerHex = buffer.ToHexString()
|
||||||
|
|
||||||
|
buffer.FromAsciiString(`\r\n`)
|
||||||
|
endMarkerHex = buffer.ToHexString()
|
||||||
|
|
||||||
|
if not buffer.ReadFile(filePath)
|
||||||
|
errors.message += `Failed to read file at path ${filePath}.\n`
|
||||||
|
return invalid
|
||||||
|
end if
|
||||||
|
contentHex = buffer.ToHexString()
|
||||||
|
|
||||||
|
startIndex = contentHex.InStr(startMarkerHex)
|
||||||
|
if startIndex = -1
|
||||||
|
errors.message += `Start marker not found in file at path ${filePath}.\n`
|
||||||
|
errors.attachments.Push({
|
||||||
|
filename: filePath
|
||||||
|
content_type: "plain/text"
|
||||||
|
data: contentHex
|
||||||
|
})
|
||||||
|
return invalid
|
||||||
|
end if
|
||||||
|
|
||||||
|
endIndex = contentHex.InStr(startIndex, endMarkerHex)
|
||||||
|
if endIndex = -1
|
||||||
|
errors.message += `End marker not found in file at path ${filePath}.\n`
|
||||||
|
errors.attachments.Push({
|
||||||
|
filename: filePath
|
||||||
|
content_type: "plain/text"
|
||||||
|
data: contentHex
|
||||||
|
})
|
||||||
|
return invalid
|
||||||
|
end if
|
||||||
|
|
||||||
|
segmentDurationsMsHex = contentHex.Mid(startIndex + startMarkerHex.len(), endIndex - (startIndex + startMarkerHex.len()))
|
||||||
|
buffer.FromHexString(segmentDurationsMsHex)
|
||||||
|
segmentDurationsMs = buffer.ToAsciiString()
|
||||||
|
|
||||||
|
regex = CreateObject("roRegex", "(\d+)(?:\(r=(\d+)\))?", "g")
|
||||||
|
matches = regex.MatchAll(segmentDurationsMs)
|
||||||
|
if not IsArray(matches) or matches.Count() = 0
|
||||||
|
errors.message += `No segment duration matches found in file at path ${filePath}.\n`
|
||||||
|
errors.attachments.Push({
|
||||||
|
filename: filePath
|
||||||
|
content_type: "plain/text"
|
||||||
|
data: contentHex
|
||||||
|
})
|
||||||
|
return invalid
|
||||||
|
end if
|
||||||
|
|
||||||
|
segmentDurations = CreateObject("roArray", matches.Count(), false)
|
||||||
|
|
||||||
|
for i = 0 to matches.Count() - 1
|
||||||
|
match = matches[i]
|
||||||
|
segment = {}
|
||||||
|
' We set duration and repeatCount to "d" and "r" so that we can feed
|
||||||
|
' them directly into the DashManifest generation code.
|
||||||
|
if match.Count() >= 2
|
||||||
|
segment["d"] = match[1].ToInt()
|
||||||
|
end if
|
||||||
|
if match.Count() >= 3
|
||||||
|
segment["r"] = match[2].ToInt()
|
||||||
|
end if
|
||||||
|
segmentDurations[i] = segment
|
||||||
|
end for
|
||||||
|
|
||||||
|
return segmentDurations
|
||||||
|
end function
|
||||||
|
|
||||||
|
end namespace
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import "pkg:/components/Services/Innertube/Context.bs"
|
import "pkg:/components/Services/Innertube/Context.bs"
|
||||||
import "pkg:/components/Services/Innertube/NodesParser.bs"
|
import "pkg:/components/Services/Innertube/NodesParser.bs"
|
||||||
|
import "pkg:/components/Services/Innertube/OTFSegmentInfo.bs"
|
||||||
import "pkg:/source/services/HttpClient.bs"
|
import "pkg:/source/services/HttpClient.bs"
|
||||||
import "pkg:/source/utils/ObjectUtils.bs"
|
import "pkg:/source/utils/ObjectUtils.bs"
|
||||||
import "pkg:/source/utils/Types.bs"
|
import "pkg:/source/utils/Types.bs"
|
||||||
@@ -168,7 +169,7 @@ namespace Innertube
|
|||||||
|
|
||||||
if isOTF
|
if isOTF
|
||||||
LogDebug("Fetching OTF segment info")
|
LogDebug("Fetching OTF segment info")
|
||||||
GetOTFSegmentInfo(adaptiveFormats, client, cancellation)
|
OTFGetSegmentInfo(adaptiveFormats, client, cancellation)
|
||||||
end if
|
end if
|
||||||
|
|
||||||
return parsedResponse
|
return parsedResponse
|
||||||
@@ -731,153 +732,4 @@ namespace Innertube
|
|||||||
end if
|
end if
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function GetOTFSegmentInfo(adaptiveFormats as object, client as Innertube.ClientType, cancellation as object) as void
|
|
||||||
requests = CreateObject("roArray", adaptiveFormats.Count(), false)
|
|
||||||
|
|
||||||
requestHeaders = Innertube.CreateHeaders(client)
|
|
||||||
requestHeaders["origin"] = "https://www.youtube.com"
|
|
||||||
requestHeaders["referer"] = "https://www.youtube.com"
|
|
||||||
requestHeaders["DNT"] = "?1"
|
|
||||||
for i = 0 to adaptiveFormats.Count() - 1
|
|
||||||
format = adaptiveFormats[i]
|
|
||||||
if format.isTypeOTF <> true
|
|
||||||
continue for
|
|
||||||
end if
|
|
||||||
|
|
||||||
url = format.url
|
|
||||||
if StringUtils.IsNullOrEmpty(url)
|
|
||||||
LogError("GetOTFSegmentInfo: URL is empty for itag", format.itag)
|
|
||||||
continue for
|
|
||||||
end if
|
|
||||||
|
|
||||||
queryComponents = UrlUtils.ParseQueryComponents(url)
|
|
||||||
queryComponents["rn"] = "0"
|
|
||||||
queryComponents["sq"] = "0"
|
|
||||||
|
|
||||||
otfUrl = UrlUtils.SetQueryParams(url, queryComponents)
|
|
||||||
|
|
||||||
request = HttpClient.Get(otfUrl)
|
|
||||||
request.Headers(requestHeaders)
|
|
||||||
request.TryCount(2)
|
|
||||||
request.Cancellation(cancellation)
|
|
||||||
request.Send()
|
|
||||||
requests[i] = request
|
|
||||||
end for
|
|
||||||
|
|
||||||
for i = 0 to requests.Count() - 1
|
|
||||||
request = requests[i]
|
|
||||||
if request = invalid
|
|
||||||
continue for
|
|
||||||
end if
|
|
||||||
|
|
||||||
format = adaptiveFormats[i]
|
|
||||||
response = request.Await()
|
|
||||||
|
|
||||||
if response.IsCancelled()
|
|
||||||
return
|
|
||||||
end if
|
|
||||||
|
|
||||||
if not response.IsSuccess()
|
|
||||||
LogErrorNoTelemetry("Failed to fetch OTF segment info for itag", format.itag, response.ErrorMessage())
|
|
||||||
LogTelemetryEvent({
|
|
||||||
level: "error"
|
|
||||||
message: `Failed to fetch OTF segment info for itag ${format.itag}\nurl: ${request.BuildUrl()}\nstatus: ${response.StatusCode()}`
|
|
||||||
fingerprint: ["OTF Fetch Error"]
|
|
||||||
attachments: [{
|
|
||||||
filename: "otf.txt"
|
|
||||||
content_type: "plain/text"
|
|
||||||
data: response.ErrorMessage()
|
|
||||||
}, {
|
|
||||||
filename: "headers.json"
|
|
||||||
content_type: "application/json"
|
|
||||||
data: FormatJson(response.Headers())
|
|
||||||
}]
|
|
||||||
})
|
|
||||||
continue for
|
|
||||||
end if
|
|
||||||
|
|
||||||
responseText = response.Text()
|
|
||||||
|
|
||||||
segmentDurationsMs = StringUtils.GetBetween(responseText, "Segment-Durations-Ms:", `\r\n`)
|
|
||||||
if StringUtils.IsNullOrEmpty(segmentDurationsMs)
|
|
||||||
LogErrorNoTelemetry("Failed to extract Segment-Durations-Ms from OTF stream for itag", format.itag)
|
|
||||||
LogTelemetryEvent({
|
|
||||||
level: "error"
|
|
||||||
message: `Failed to extract Segment-Durations-Ms from OTF stream for itag ${format.itag}\nurl: ${request.BuildUrl()}\nstatus: ${response.StatusCode()}`
|
|
||||||
fingerprint: ["OTF Missing Segment Durations"]
|
|
||||||
attachments: [{
|
|
||||||
filename: "otf.txt"
|
|
||||||
content_type: "plain/text"
|
|
||||||
data: responseText
|
|
||||||
}, {
|
|
||||||
filename: "headers.json"
|
|
||||||
content_type: "application/json"
|
|
||||||
data: FormatJson(response.Headers())
|
|
||||||
}]
|
|
||||||
})
|
|
||||||
continue for
|
|
||||||
end if
|
|
||||||
|
|
||||||
segmentDurationStrings = segmentDurationsMs.Split(",")
|
|
||||||
segmentDurations = []
|
|
||||||
for each segmentDurationString in segmentDurationStrings
|
|
||||||
trimmedSegmentDuration = segmentDurationString.Trim()
|
|
||||||
if trimmedSegmentDuration.Len() = 0
|
|
||||||
continue for
|
|
||||||
end if
|
|
||||||
|
|
||||||
repeatCount = invalid
|
|
||||||
repeatCountString = StringUtils.GetBetween(trimmedSegmentDuration, "(r=", ")")
|
|
||||||
if not StringUtils.IsNullOrEmpty(repeatCountString)
|
|
||||||
repeatCount = repeatCountString.ToInt()
|
|
||||||
end if
|
|
||||||
|
|
||||||
duration = trimmedSegmentDuration.ToInt()
|
|
||||||
if duration <= 0
|
|
||||||
LogErrorNoTelemetry("Invalid OTF segment duration parsed", trimmedSegmentDuration, "for itag", format.itag)
|
|
||||||
LogTelemetryEvent({
|
|
||||||
level: "error"
|
|
||||||
message: `Invalid OTF segment duration parsed: ${trimmedSegmentDuration} for itag ${format.itag}\nurl: ${request.BuildUrl()}\nstatus: ${response.StatusCode()}`
|
|
||||||
fingerprint: ["OTF Invalid Segment Duration"]
|
|
||||||
attachments: [{
|
|
||||||
filename: "otf.txt"
|
|
||||||
content_type: "plain/text"
|
|
||||||
data: responseText
|
|
||||||
}, {
|
|
||||||
filename: "headers.json"
|
|
||||||
content_type: "application/json"
|
|
||||||
data: FormatJson(response.Headers())
|
|
||||||
}]
|
|
||||||
})
|
|
||||||
continue for
|
|
||||||
end if
|
|
||||||
|
|
||||||
segmentDurations.Push({
|
|
||||||
duration: duration
|
|
||||||
repeatCount: repeatCount
|
|
||||||
})
|
|
||||||
end for
|
|
||||||
|
|
||||||
format["otfSegmentDurations"] = segmentDurations
|
|
||||||
|
|
||||||
if segmentDurations.Count() = 0
|
|
||||||
LogErrorNoTelemetry("No OTF segment durations found for itag", format.itag)
|
|
||||||
LogTelemetryEvent({
|
|
||||||
level: "error"
|
|
||||||
message: `No OTF segment durations found for itag ${format.itag}\nurl: ${request.BuildUrl()}\nstatus: ${response.StatusCode()}`
|
|
||||||
fingerprint: ["OTF No Segment Durations"]
|
|
||||||
attachments: [{
|
|
||||||
filename: "otf.txt"
|
|
||||||
content_type: "plain/text"
|
|
||||||
data: responseText
|
|
||||||
}, {
|
|
||||||
filename: "headers.json"
|
|
||||||
content_type: "application/json"
|
|
||||||
data: FormatJson(response.Headers())
|
|
||||||
}]
|
|
||||||
})
|
|
||||||
end if
|
|
||||||
end for
|
|
||||||
end function
|
|
||||||
|
|
||||||
end namespace
|
end namespace
|
||||||
@@ -299,6 +299,11 @@ namespace HttpClient
|
|||||||
return m
|
return m
|
||||||
end function
|
end function
|
||||||
|
|
||||||
|
function ToFile(filePath as string) as HttpRequest
|
||||||
|
m._toFilePath = filePath
|
||||||
|
return m
|
||||||
|
end function
|
||||||
|
|
||||||
function ToCurlCommand() as string
|
function ToCurlCommand() as string
|
||||||
command = `curl "${m.BuildUrl()}"`
|
command = `curl "${m.BuildUrl()}"`
|
||||||
if m._method = "HEAD"
|
if m._method = "HEAD"
|
||||||
@@ -387,6 +392,10 @@ namespace HttpClient
|
|||||||
end if
|
end if
|
||||||
#end if
|
#end if
|
||||||
|
|
||||||
|
if m._toFilePath <> invalid and m._method <> "GET"
|
||||||
|
throw "ToFile() can only be used with GET requests."
|
||||||
|
end if
|
||||||
|
|
||||||
if m._method = "POST"
|
if m._method = "POST"
|
||||||
body = m._body
|
body = m._body
|
||||||
if body = invalid
|
if body = invalid
|
||||||
@@ -394,7 +403,11 @@ namespace HttpClient
|
|||||||
end if
|
end if
|
||||||
m._sent = m.urlTransfer.AsyncPostFromString(body)
|
m._sent = m.urlTransfer.AsyncPostFromString(body)
|
||||||
else if m._method = "GET"
|
else if m._method = "GET"
|
||||||
m._sent = m.urlTransfer.AsyncGetToString()
|
if m._toFilePath <> invalid
|
||||||
|
m._sent = m.urlTransfer.AsyncGetToFile(m._toFilePath)
|
||||||
|
else
|
||||||
|
m._sent = m.urlTransfer.AsyncGetToString()
|
||||||
|
end if
|
||||||
else if m._method = "HEAD"
|
else if m._method = "HEAD"
|
||||||
m._sent = m.urlTransfer.AsyncHead()
|
m._sent = m.urlTransfer.AsyncHead()
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in new issue
Block a user