mirror of
https://github.com/yattee/yattee.git
synced 2026-08-05 23:01:28 +00:00
Fix missing thumbnails for videos saved to library
Persistence models (local playlists, watch history, bookmarks, downloads) store only the best advertised thumbnail URL - usually maxresdefault.jpg, which doesn't exist for many older videos and 404s. Live API results survive this because views fall back through the full quality chain, but toVideo() rebuilt videos with that single dead URL, leaving placeholder covers. Reconstruct the quality fallback chain from the stored YouTube-style URL at read time (Thumbnail.fallbackChain), and rewrite playlist list covers to the always-available hqdefault variant (Thumbnail.reliableURL), since those views render a single URL without fallback. Also expand the fabricated maxres URL in PipedAPI into a full chain.
This commit is contained in:
@@ -217,7 +217,7 @@ final class Bookmark {
|
|||||||
publishedText: publishedText,
|
publishedText: publishedText,
|
||||||
viewCount: viewCount,
|
viewCount: viewCount,
|
||||||
likeCount: nil,
|
likeCount: nil,
|
||||||
thumbnails: thumbnailURL.map { [Thumbnail(url: $0, width: nil, height: nil)] } ?? [],
|
thumbnails: Thumbnail.fallbackChain(for: thumbnailURL),
|
||||||
isLive: isLive,
|
isLive: isLive,
|
||||||
isUpcoming: false,
|
isUpcoming: false,
|
||||||
scheduledStartTime: nil
|
scheduledStartTime: nil
|
||||||
|
|||||||
@@ -75,10 +75,16 @@ final class LocalPlaylist {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The first video's thumbnail URL for display.
|
/// The first video's thumbnail URL for display.
|
||||||
|
///
|
||||||
|
/// Rewritten to the always-available `hqdefault` variant because the stored
|
||||||
|
/// URL is the best advertised quality (often `maxresdefault`), which 404s
|
||||||
|
/// for many older videos and would leave the cover blank.
|
||||||
var thumbnailURL: URL? {
|
var thumbnailURL: URL? {
|
||||||
(items ?? []).sorted { $0.sortOrder < $1.sortOrder }
|
Thumbnail.reliableURL(
|
||||||
.first?
|
for: (items ?? []).sorted { $0.sortOrder < $1.sortOrder }
|
||||||
.thumbnailURL
|
.first?
|
||||||
|
.thumbnailURL
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sorted items by order.
|
/// Sorted items by order.
|
||||||
@@ -249,7 +255,7 @@ extension LocalPlaylistItem {
|
|||||||
publishedText: nil,
|
publishedText: nil,
|
||||||
viewCount: nil,
|
viewCount: nil,
|
||||||
likeCount: nil,
|
likeCount: nil,
|
||||||
thumbnails: thumbnailURL.map { [Thumbnail(url: $0, quality: .medium)] } ?? [],
|
thumbnails: Thumbnail.fallbackChain(for: thumbnailURL),
|
||||||
isLive: isLive,
|
isLive: isLive,
|
||||||
isUpcoming: false,
|
isUpcoming: false,
|
||||||
scheduledStartTime: nil
|
scheduledStartTime: nil
|
||||||
|
|||||||
@@ -241,7 +241,7 @@ extension WatchEntry {
|
|||||||
publishedText: nil,
|
publishedText: nil,
|
||||||
viewCount: nil,
|
viewCount: nil,
|
||||||
likeCount: nil,
|
likeCount: nil,
|
||||||
thumbnails: thumbnailURL.map { [Thumbnail(url: $0, quality: .medium)] } ?? [],
|
thumbnails: Thumbnail.fallbackChain(for: thumbnailURL),
|
||||||
isLive: isLive,
|
isLive: isLive,
|
||||||
isUpcoming: false,
|
isUpcoming: false,
|
||||||
scheduledStartTime: nil
|
scheduledStartTime: nil
|
||||||
|
|||||||
@@ -311,6 +311,55 @@ struct Thumbnail: Codable, Hashable, Sendable {
|
|||||||
self.width = width
|
self.width = width
|
||||||
self.height = height
|
self.height = height
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Expands a single stored YouTube-style thumbnail URL (`/vi/<id>/<variant>.jpg`)
|
||||||
|
/// into a best-first fallback chain. Persistence models (playlists, watch history,
|
||||||
|
/// bookmarks) store only the best advertised URL — usually `maxresdefault.jpg`,
|
||||||
|
/// which doesn't exist for many older videos and 404s. Reconstructing the
|
||||||
|
/// lower-quality variants lets thumbnail views fall back the same way they do
|
||||||
|
/// for live API results. Non-matching URLs get a single-entry chain.
|
||||||
|
/// Rewrites a YouTube-style thumbnail URL (`/vi/<id>/<variant>.jpg`) to the
|
||||||
|
/// `hqdefault.jpg` variant, which exists for effectively every video (unlike
|
||||||
|
/// `maxresdefault`/`sddefault`, which 404 for many older uploads). Use where a
|
||||||
|
/// single URL is displayed without fallback, e.g. playlist covers. Non-matching
|
||||||
|
/// URLs are returned unchanged.
|
||||||
|
static func reliableURL(for url: URL?) -> URL? {
|
||||||
|
guard let url else { return nil }
|
||||||
|
let variants = ["maxresdefault.jpg", "sddefault.jpg", "hqdefault.jpg", "mqdefault.jpg", "default.jpg"]
|
||||||
|
let path = url.path
|
||||||
|
guard path.range(of: #"/vi/[^/]+/"#, options: .regularExpression) != nil,
|
||||||
|
let match = variants.first(where: { path.hasSuffix($0) }),
|
||||||
|
var components = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
components.path = String(path.dropLast(match.count)) + "hqdefault.jpg"
|
||||||
|
return components.url ?? url
|
||||||
|
}
|
||||||
|
|
||||||
|
static func fallbackChain(for url: URL?) -> [Thumbnail] {
|
||||||
|
guard let url else { return [] }
|
||||||
|
let variants: [(suffix: String, quality: Quality)] = [
|
||||||
|
("maxresdefault.jpg", .maxres),
|
||||||
|
("sddefault.jpg", .standard),
|
||||||
|
("hqdefault.jpg", .high),
|
||||||
|
("mqdefault.jpg", .medium),
|
||||||
|
("default.jpg", .default),
|
||||||
|
]
|
||||||
|
let path = url.path
|
||||||
|
guard path.range(of: #"/vi/[^/]+/"#, options: .regularExpression) != nil,
|
||||||
|
let current = variants.first(where: { path.hasSuffix($0.suffix) }),
|
||||||
|
let components = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
|
||||||
|
return [Thumbnail(url: url, quality: .medium)]
|
||||||
|
}
|
||||||
|
let basePath = String(path.dropLast(current.suffix.count))
|
||||||
|
return variants.compactMap { variant in
|
||||||
|
guard variant.quality <= current.quality else { return nil }
|
||||||
|
var variantComponents = components
|
||||||
|
variantComponents.path = basePath + variant.suffix
|
||||||
|
guard let variantURL = variantComponents.url else { return nil }
|
||||||
|
return Thumbnail(url: variantURL, quality: variant.quality)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Preview Support
|
// MARK: - Preview Support
|
||||||
|
|||||||
@@ -590,7 +590,7 @@ private struct PipedStreamResponse: Decodable, Sendable {
|
|||||||
let thumbnails: [Thumbnail] = {
|
let thumbnails: [Thumbnail] = {
|
||||||
if !resolvedVideoId.isEmpty,
|
if !resolvedVideoId.isEmpty,
|
||||||
let url = URL(string: "https://i.ytimg.com/vi/\(resolvedVideoId)/maxresdefault.jpg") {
|
let url = URL(string: "https://i.ytimg.com/vi/\(resolvedVideoId)/maxresdefault.jpg") {
|
||||||
return [Thumbnail(url: url, quality: .maxres)]
|
return Thumbnail.fallbackChain(for: url)
|
||||||
}
|
}
|
||||||
// Fallback to proxy URL if video ID not available
|
// Fallback to proxy URL if video ID not available
|
||||||
if let proxyURL = thumbnailUrl.flatMap({ URL(string: $0) }) {
|
if let proxyURL = thumbnailUrl.flatMap({ URL(string: $0) }) {
|
||||||
|
|||||||
@@ -336,7 +336,7 @@ struct Download: Identifiable, Codable, Sendable, Equatable {
|
|||||||
publishedText: publishedText,
|
publishedText: publishedText,
|
||||||
viewCount: viewCount,
|
viewCount: viewCount,
|
||||||
likeCount: likeCount,
|
likeCount: likeCount,
|
||||||
thumbnails: resolvedThumbnailURL.map { [Thumbnail(url: $0, quality: .medium)] } ?? [],
|
thumbnails: Thumbnail.fallbackChain(for: resolvedThumbnailURL),
|
||||||
isLive: false,
|
isLive: false,
|
||||||
isUpcoming: false,
|
isUpcoming: false,
|
||||||
scheduledStartTime: nil
|
scheduledStartTime: nil
|
||||||
|
|||||||
@@ -960,7 +960,7 @@ final class DownloadManager: NSObject {
|
|||||||
publishedText: download.publishedText,
|
publishedText: download.publishedText,
|
||||||
viewCount: download.viewCount,
|
viewCount: download.viewCount,
|
||||||
likeCount: download.likeCount,
|
likeCount: download.likeCount,
|
||||||
thumbnails: download.thumbnailURL.map { [Thumbnail(url: $0, quality: .medium)] } ?? [],
|
thumbnails: Thumbnail.fallbackChain(for: download.thumbnailURL),
|
||||||
isLive: false,
|
isLive: false,
|
||||||
isUpcoming: false,
|
isUpcoming: false,
|
||||||
scheduledStartTime: nil
|
scheduledStartTime: nil
|
||||||
|
|||||||
Reference in New Issue
Block a user