Use new multiple key sorting

This commit is contained in:
tcely 2024-12-02 10:56:19 -05:00
parent 4b60073ef0
commit ede9dff4e6

View File

@ -5,6 +5,7 @@
''' '''
from .utils import multi_key_sort
from django.conf import settings from django.conf import settings
@ -49,6 +50,7 @@ def get_best_audio_format(media):
''' '''
# Order all audio-only formats by bitrate # Order all audio-only formats by bitrate
audio_formats = [] audio_formats = []
sort_keys = [('abr', True)] # key, reverse
for fmt in media.iter_formats(): for fmt in media.iter_formats():
# If the format has a video stream, skip it # If the format has a video stream, skip it
if fmt['vcodec'] is not None: if fmt['vcodec'] is not None:
@ -56,7 +58,7 @@ def get_best_audio_format(media):
if not fmt['acodec']: if not fmt['acodec']:
continue continue
audio_formats.append(fmt) audio_formats.append(fmt)
audio_formats = list(reversed(sorted(audio_formats, key=lambda k: k['abr']))) audio_formats = list(multi_key_sort(audio_formats, sort_keys))
if not audio_formats: if not audio_formats:
# Media has no audio formats at all # Media has no audio formats at all
return False, False return False, False
@ -86,6 +88,7 @@ def get_best_video_format(media):
return False, False return False, False
# Filter video-only formats by resolution that matches the source # Filter video-only formats by resolution that matches the source
video_formats = [] video_formats = []
sort_keys = [('height', True), ('id', True)] # key, reverse
for fmt in media.iter_formats(): for fmt in media.iter_formats():
# If the format has an audio stream, skip it # If the format has an audio stream, skip it
if fmt['acodec'] is not None: if fmt['acodec'] is not None:
@ -109,7 +112,7 @@ def get_best_video_format(media):
else: else:
# Can't fallback # Can't fallback
return False, False return False, False
video_formats = list(reversed(sorted(video_formats, key=lambda k: k['height']))) video_formats = list(multi_key_sort(video_formats, sort_keys))
source_resolution = media.source.source_resolution.strip().upper() source_resolution = media.source.source_resolution.strip().upper()
source_vcodec = media.source.source_vcodec source_vcodec = media.source.source_vcodec
if not video_formats: if not video_formats: