From 900cc92229d6cbae1a72acec06f52a97eac64a2e Mon Sep 17 00:00:00 2001 From: sherl Date: Fri, 21 Nov 2025 22:38:43 +0100 Subject: [PATCH] fix: play other formats than 16x9 on yattee reports factually wrong, but close enough resolution. to be removed when this will be fixed on yattee's end. --- ythdd_struct_parser.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/ythdd_struct_parser.py b/ythdd_struct_parser.py index f5c1a3c..74f9c94 100644 --- a/ythdd_struct_parser.py +++ b/ythdd_struct_parser.py @@ -519,6 +519,32 @@ def extractTextFromSimpleOrRuns(obj: dict, default: str = "") -> str: return text +def findNearestResolution(width: int, height: int) -> int: + # Finds the nearest standard resolution (one of 144p, 240p, ...) + # So far only used for Yattee, as it has trouble playing anything + # without one of the standard resolutions. Playback on other + # clients is unaffected. + + # failsafe behaviour + try: + width = int(width) + height = int(height) + res = min(width, height) + except: + return 360 + + standard_resolutions = [144, 240, 360, 720, 1080, 2160, 4320] + if res in standard_resolutions: + return res + + # calculate relative distance to one of the standard resolutions + res_normalized = [abs(1 - (x / res)) for x in standard_resolutions] + # pick the one where the distance is the smallest + target_index = res_normalized.index(min(res_normalized)) + target_res = standard_resolutions[target_index] + + return target_res + def parseFormatStreams(wdata_fstream: dict, ydata_stream: dict) -> dict: try: @@ -536,7 +562,7 @@ def parseFormatStreams(wdata_fstream: dict, ydata_stream: dict) -> dict: "bitrate": str(wdata_fstream["bitrate"]), "fps": wdata_fstream["fps"], "size": f"{wdata_fstream['width']}x{wdata_fstream['height']}", - "resolution": f"{wdata_fstream['height'] if wdata_fstream['height'] in (144, 240, 360, 720, 1080, 2160) else 360}p", + "resolution": f"{findNearestResolution(wdata_fstream['width'], wdata_fstream['height'])}p", # possibly not really needed here "qualityLabel": wdata_fstream["qualityLabel"], "container": safeTraverse(FORMATS.get(wdata_fstream["itag"]), [ "ext"], default="mp4"), # invidious_formats "encoding": safeTraverse(FORMATS.get(wdata_fstream["itag"]), ["vcodec"], default="mp4") # invidious_formats @@ -577,7 +603,7 @@ def parseAdaptiveStreams(wdata_astream: dict, ydata_stream: dict) -> dict: # video-specific metadata astream["fps"] = wdata_astream["fps"] astream["size"] = f"{wdata_astream['width']}x{wdata_astream['height']}" - astream["resolution"] = f"{wdata_astream['height'] if wdata_astream['height'] in (144, 240, 360, 720, 1080, 2160) else 360}p" + astream["resolution"] = f"{findNearestResolution(wdata_astream['width'], wdata_astream['height'])}p" astream["qualityLabel"] = wdata_astream["qualityLabel"] astream["colorInfo"] = safeTraverse(wdata_astream, ["colorInfo"]) else: