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)
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()