new algorithm for quality youtube-dl

This commit is contained in:
Roy Olav Purser 2021-05-26 19:15:12 +02:00
parent 81a28d0656
commit e9abc7e49e
Signed by: roypur
GPG Key ID: E14D26A036F21656

View File

@ -46,22 +46,26 @@ class YoutubeRunner(StreamProvider):
info = ydl.extract_info(self.upstream, download=False) info = ydl.extract_info(self.upstream, download=False)
vformats = info.get("formats") vformats = info.get("formats")
best_format = {} best_format = {}
best_format["quality"] = 0 best_format["width"] = 10
best_format["height"] = 10
if isinstance(vformats, list): if isinstance(vformats, list):
for vformat in vformats: for vformat in vformats:
acodec = vformat.get("acodec") acodec = vformat.get("acodec")
vcodec = vformat.get("vcodec") vcodec = vformat.get("vcodec")
new_quality = vformat.get("quality") current_width = vformat.get("height")
old_quality = best_format.get("quality") current_height = vformat.get("width")
best_width = best_format.get("width")
best_height = best_format.get("height")
new_url = vformat.get("url") new_url = vformat.get("url")
if (isinstance(acodec, str) and if (isinstance(best_width, int) and
isinstance(vcodec, str) and isinstance(best_height, int) and
isinstance(new_quality, int) and isinstance(current_width, int) and
isinstance(old_quality, int) and isinstance(current_height, int) and
isinstance(new_url, str) and isinstance(new_url, str) and
current_width > best_width and
current_height > best_height and
acodec != "none" and acodec != "none" and
vcodec != "none" and vcodec != "none"):
new_quality > old_quality):
best_format = vformat best_format = vformat
best_url = new_url best_url = new_url
return best_url return best_url
@ -69,13 +73,25 @@ class YoutubeRunner(StreamProvider):
return await asyncio.to_thread(self.stream) return await asyncio.to_thread(self.stream)
async def get_ytdl(upstream, proxy, logger): async def get_ytdl(upstream, proxy, logger):
runner = YoutubeRunner(upstream, proxy) result = None
result = await runner.run() try:
runner = YoutubeRunner(upstream, proxy)
result_temp = await runner.run()
except Exception as e:
logger.info(e)
else:
result = result_temp
return result return result
async def get_streamlink(upstream, proxy, logger): async def get_streamlink(upstream, proxy, logger):
runner = StreamlinkRunner(upstream, proxy) result = None
result = await runner.run() try:
runner = StreamlinkRunner(upstream, proxy)
result_temp = await runner.run()
except Exception as e:
logger.info(e)
else:
result = result_temp
return result return result
async def get_any(upstream, proxy, logger): 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))) tasks.append(asyncio.create_task(get_ytdl(upstream, proxy, logger)))
result = None result = None
for task in asyncio.as_completed(tasks, timeout=5.0): for task in asyncio.as_completed(tasks, timeout=5.0):
try: temp_result = await task
result = await task if isinstance(temp_result, str):
if isinstance(result, str): result = temp_result
break break
except Exception as e:
logger.info(e)
for task in tasks: for task in tasks:
if not task.done(): if not task.done():
task.cancel() task.cancel()