From 5826a1870b785c4b9018e3a07c3fe4f06548ab02 Mon Sep 17 00:00:00 2001 From: Alexander Eichhorn Date: Wed, 13 Dec 2023 17:25:14 +0100 Subject: [PATCH] Automatically filter out potential invalid stream sources --- Sources/YouTubeKit/Extraction.swift | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Sources/YouTubeKit/Extraction.swift b/Sources/YouTubeKit/Extraction.swift index d0745ed..93a1182 100644 --- a/Sources/YouTubeKit/Extraction.swift +++ b/Sources/YouTubeKit/Extraction.swift @@ -280,6 +280,8 @@ class Extraction { class func applySignature(streamManifest: inout [InnerTube.StreamingData.Format], videoInfo: InnerTube.VideoInfo, js: String) throws { var cipher = ThrowingLazy(try Cipher(js: js)) + var invalidStreamIndices = [Int]() + for (i, stream) in streamManifest.enumerated() { if let url = stream.url { if url.contains("signature") || (stream.s == nil && (url.contains("&sig=") || url.contains("&lsig="))) { @@ -288,6 +290,10 @@ class Extraction { } if let cipheredSignature = stream.s { + // Remove the stream from `streamManifest` for now, as signature extraction currently doesn't work most of time + invalidStreamIndices.append(i) + continue // Skip the rest of the code as we are removing this stream + let signature = try cipher.value.getSignature(cipheredSignature: cipheredSignature) os_log("finished descrambling signature for itag=%{public}i", log: log, type: .debug, stream.itag) @@ -311,6 +317,11 @@ class Extraction { } } } + + // Remove invalid streams + for index in invalidStreamIndices.reversed() { + streamManifest.remove(at: index) + } } /// Breaks up the data in the ``type`` key of the manifest, which contains the