Skip to content

Add high resolution thumbnail (if available)#33

Open
timonvanhasselt wants to merge 1 commit intoOpenVoiceOS:devfrom
timonvanhasselt:patch-3
Open

Add high resolution thumbnail (if available)#33
timonvanhasselt wants to merge 1 commit intoOpenVoiceOS:devfrom
timonvanhasselt:patch-3

Conversation

@timonvanhasselt
Copy link

@timonvanhasselt timonvanhasselt commented Nov 17, 2025

Added logic for Google profile pictures next to YouTube thumbnails. Maybe there's a better way to receive the higher res instead of the 120px profile pictures the api returns.

Summary by CodeRabbit

  • Improvements
    • Enhanced thumbnail image quality for playlist tracks and search results by optimizing resolution settings across different image sources.

Added logic for Google profile pictures next to YouTube thumbnails. Maybe there's a better way to receive the higher res instead of the 120px profile pictures the api returns.
@coderabbitai
Copy link

coderabbitai bot commented Nov 17, 2025

Walkthrough

Introduces a helper method _get_high_res_thumbnail() to enhance thumbnail URLs by replacing low-resolution Google and YouTube image parameters with higher-quality alternatives, and applies it when building MediaEntry objects for playlist tracks and search results.

Changes

Cohort / File(s) Summary
Thumbnail Resolution Enhancement
__init__.py
Added _get_high_res_thumbnail(self, url) helper method with regex patterns to upgrade Google profile picture and YouTube thumbnail URLs to higher resolution variants; imported re module; integrated helper into two MediaEntry construction code paths

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Verify regex patterns correctly match and replace intended URL patterns for both Google and YouTube thumbnails
  • Confirm the fallback behavior when URL patterns don't match expected formats
  • Check that the method gracefully handles edge cases (None/empty URLs, malformed URLs)

Poem

🐰 A thumbnail so small, now grows ever tall,
With w800-h800 magic and YouTube's maxres call—
Where pixels dance bright in the high-res light,
This rabbit's refactored to make images right! 📸✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: adding logic to fetch and use high-resolution thumbnails instead of default lower-resolution ones.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 88d965e and 9a2e6f9.

📒 Files selected for processing (1)
  • __init__.py (4 hunks)
🔇 Additional comments (3)
__init__.py (3)

3-3: LGTM!

The re import is necessary for the regex operations in the new _get_high_res_thumbnail method.


137-138: LGTM!

Correct usage of the helper method for playlist track thumbnails. The fallback behavior is maintained since _get_high_res_thumbnail returns the original URL when no transformation applies.


158-159: LGTM!

Correct usage of the helper method for individual result thumbnails, consistent with the playlist track handling.

Comment on lines +36 to +72
# Helper function to try and get max resolution thumbnail
def _get_high_res_thumbnail(self, url):
"""
Replaces a lower resolution thumbnail ID in the URL with 'maxresdefault'.
If no known low-resolution ID is found, the original URL is returned (fallback).
Also updates googleusercontent profile picture size to 800x800 for high resolution.
"""
if not url:
return url

original_url = url
desired_size_param = "=w800-h800-l90-rj"

# NEW LOGIC: Adjust googleusercontent profile pictures for high resolution
if "googleusercontent.com/profile/picture" in original_url:

# 1. Try to replace an existing size parameter (e.g., =w120-h120-l90-rj)
# The pattern looks for "=w<digits>-h<digits>-l90-rj"
new_url = re.sub(r'=w\d+-h\d+-l90-rj', desired_size_param, original_url)

# 2. If replacement didn't happen and the desired parameter is not already present,
# append it to the end of the URL.
if new_url == original_url and desired_size_param not in original_url:
# Check if the URL ends with a number (e.g., '.../picture/2'), which is common
if re.search(r'/\d+$', original_url):
return original_url + desired_size_param

return new_url

# Existing logic: Try high resolution standard YouTube thumbnail
for quality in ["default", "hqdefault", "mqdefault", "sddefault"]:
if f"/{quality}.jpg" in original_url:
# Returns the URL with 'maxresdefault.jpg'
return original_url.replace(f"/{quality}.jpg", "/maxresdefault.jpg")

# Fallback: Return original URL if no replacement was made
return original_url
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

Verify maxresdefault.jpg availability causes graceful degradation.

The helper method looks well-structured, but there's a significant concern: maxresdefault.jpg doesn't exist for all YouTube videos. When a video lacks this high-resolution thumbnail, the image URL will return a 404, potentially causing broken images in the UI.

Consider:

  1. Verifying whether the media player/UI gracefully handles missing thumbnails
  2. Implementing a fallback mechanism or checking thumbnail availability
  3. Starting with hqdefault as a safer default that exists for all videos

For the Google profile picture logic, the assumption that appending =w800-h800-l90-rj after a numeric path segment is valid seems reasonable, but should be validated against actual API behavior.

Run the following script to check if there are any error handling mechanisms for failed image loads:


🏁 Script executed:

#!/bin/bash
# Description: Search for image error handling in the codebase

# Check if MediaEntry or related classes handle image loading errors
rg -n "image.*error|thumbnail.*error|fallback.*image" --type=py

# Look for any error handling around image URLs
rg -n "MediaEntry" -A 10 -B 5 --type=py | rg -i "error|exception|fallback"

Length of output: 185



---

🏁 Script executed:

```shell
#!/bin/bash
# Get context around lines 137 and 158 to understand the fallback mechanism
sed -n '130,165p' __init__.py

Length of output: 1727


🏁 Script executed:

#!/bin/bash
# Search for how thumbnail URLs are actually used (in display/rendering)
rg -n "thumbnail|_get_high_res_thumbnail" -A 5 -B 2 --type=py

Length of output: 2252


🏁 Script executed:

#!/bin/bash
# Check for any image onerror handlers or error recovery mechanisms
rg -n "onerror|onload|image.*failed|src.*error" --type=py --type=js --type=html -C 3

Length of output: 0



---

🌐 Web query:

```web
YouTube video thumbnail maxresdefault.jpg availability which thumbnails always exist

💡 Result:

Short answer: maxresdefault.jpg is NOT guaranteed to exist. The only thumbnail URLs you can generally rely on are the low/medium/high default images (default.jpg, mqdefault.jpg, hqdefault.jpg) and the frame indexes (0.jpg, 1.jpg, 2.jpg, 3.jpg). sddefault and maxres (maxresdefault.jpg) are optional and may 404 for many videos; the YouTube Data API lists standard and maxres as “available for some videos.” Use a fallback chain (maxres → sd → hq → mq → default/0) to ensure an image. [1][2][5]

Sources:

  • YouTube Data API thumbnails docs (maxres is optional). [1]
  • Community/FAQ and developer reports showing maxresdefault may 404; recommend fallbacks. [0][2]

Implement proper thumbnail fallback chain to prevent 404 broken images.

maxresdefault.jpg is not guaranteed to exist; YouTube Data API documentation indicates that maxres thumbnails are optional and available for only some videos. The current implementation at line 69 replaces any low-resolution thumbnail identifier with maxresdefault.jpg, which will fail for the majority of videos, causing broken images with no recovery mechanism.

The only thumbnail URLs you can reliably depend on are default.jpg, mqdefault.jpg, and hqdefault.jpg. Revise the logic to use a fallback chain starting with hqdefault instead of maxresdefault, or implement a complete chain (maxres → sd → hq → mq → default) to ensure graceful degradation.

The googleusercontent profile picture logic is sound.

🤖 Prompt for AI Agents
In __init__.py around lines 36 to 72, the current logic bluntly replaces known
low-res YouTube thumbnail filenames with "maxresdefault.jpg" which leads to 404s
because maxres may not exist; change the replacement to use a fallback chain
instead: when a URL contains a known quality (default, mqdefault, hqdefault,
sddefault), generate replacement candidates in order (maxresdefault → sddefault
→ hqdefault → mqdefault → default) and return the first candidate produced by
replacing the found quality segment; alternatively, prefer starting with
"hqdefault" if you want a simpler safer default, but do not return straight to
maxres without attempting the subsequent fallbacks. Ensure existing
googleusercontent profile picture logic remains unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant