From e9abc7e49eed679429399483f04c7f3d50a3e18e Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Wed, 26 May 2021 19:15:12 +0200 Subject: [PATCH] new algorithm for quality youtube-dl --- backend/stream_providers.py | 52 +++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/backend/stream_providers.py b/backend/stream_providers.py index aec38d3..5f26212 100755 --- a/backend/stream_providers.py +++ b/backend/stream_providers.py @@ -46,22 +46,26 @@ class YoutubeRunner(StreamProvider): info = ydl.extract_info(self.upstream, download=False) vformats = info.get("formats") best_format = {} - best_format["quality"] = 0 + best_format["width"] = 10 + best_format["height"] = 10 if isinstance(vformats, list): for vformat in vformats: acodec = vformat.get("acodec") vcodec = vformat.get("vcodec") - new_quality = vformat.get("quality") - old_quality = best_format.get("quality") + current_width = vformat.get("height") + current_height = vformat.get("width") + best_width = best_format.get("width") + best_height = best_format.get("height") new_url = vformat.get("url") - if (isinstance(acodec, str) and - isinstance(vcodec, str) and - isinstance(new_quality, int) and - isinstance(old_quality, int) and + if (isinstance(best_width, int) and + isinstance(best_height, int) and + isinstance(current_width, int) and + isinstance(current_height, int) and isinstance(new_url, str) and + current_width > best_width and + current_height > best_height and acodec != "none" and - vcodec != "none" and - new_quality > old_quality): + vcodec != "none"): best_format = vformat best_url = new_url return best_url @@ -69,13 +73,25 @@ class YoutubeRunner(StreamProvider): return await asyncio.to_thread(self.stream) async def get_ytdl(upstream, proxy, logger): - runner = YoutubeRunner(upstream, proxy) - result = await runner.run() + result = None + try: + runner = YoutubeRunner(upstream, proxy) + result_temp = await runner.run() + except Exception as e: + logger.info(e) + else: + result = result_temp return result async def get_streamlink(upstream, proxy, logger): - runner = StreamlinkRunner(upstream, proxy) - result = await runner.run() + result = None + try: + runner = StreamlinkRunner(upstream, proxy) + result_temp = await runner.run() + except Exception as e: + logger.info(e) + else: + result = result_temp return result async def get_any(upstream, proxy, logger): @@ -84,12 +100,10 @@ async def get_any(upstream, proxy, logger): tasks.append(asyncio.create_task(get_ytdl(upstream, proxy, logger))) result = None for task in asyncio.as_completed(tasks, timeout=5.0): - try: - result = await task - if isinstance(result, str): - break - except Exception as e: - logger.info(e) + temp_result = await task + if isinstance(temp_result, str): + result = temp_result + break for task in tasks: if not task.done(): task.cancel()