From 8609ef7709832bd749e4d488c2fca58bef2b1973 Mon Sep 17 00:00:00 2001 From: Ivan0xFF Date: Tue, 17 Jun 2025 07:03:18 -0700 Subject: [PATCH] Added caption support for Piped backend (#867) Co-authored-by: Ivan --- Model/Applications/PipedAPI.swift | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Model/Applications/PipedAPI.swift b/Model/Applications/PipedAPI.swift index 6ecd7039..8d77c438 100644 --- a/Model/Applications/PipedAPI.swift +++ b/Model/Applications/PipedAPI.swift @@ -591,7 +591,8 @@ final class PipedAPI: Service, ObservableObject, VideosAPI { dislikes: details["dislikes"]?.int, streams: extractStreams(from: content), related: extractRelated(from: content), - chapters: extractChapters(from: content) + chapters: extractChapters(from: content), + captions: extractCaptions(from: content) ) } @@ -818,6 +819,24 @@ final class PipedAPI: Service, ObservableObject, VideosAPI { return Chapter(title: title, image: image, start: start) } } + + private func extractCaptions(from content: JSON) -> [Captions] { + content["subtitles"].arrayValue.compactMap { details in + guard let url = details["url"].url, + let code = details["code"].string, + let label = details["name"].string, + var components = URLComponents(url: url, resolvingAgainstBaseURL: false) + else { return nil } + + components.queryItems = components.queryItems?.map { item in + item.name == "fmt" ? URLQueryItem(name: "fmt", value: "srt") : item + } + + guard let newUrl = components.url else { return nil } + + return Captions(label: label, code: code, url: newUrl) + } + } private func contentItemsDictionary(from content: JSON) -> JSON { if let key = Self.contentItemsKeys.first(where: { content.dictionaryValue.keys.contains($0) }),