Implement universal format sorting

Closes #5566
This commit is contained in:
pukkandan 2022-11-17 10:53:05 +05:30
parent d0d74b7197
commit 784320c98c
No known key found for this signature in database
GPG Key ID: 7EEE9E1E817D0A39
2 changed files with 17 additions and 3 deletions

View File

@ -67,6 +67,7 @@
EntryNotInPlaylist, EntryNotInPlaylist,
ExistingVideoReached, ExistingVideoReached,
ExtractorError, ExtractorError,
FormatSorter,
GeoRestrictedError, GeoRestrictedError,
HEADRequest, HEADRequest,
ISO3166Utils, ISO3166Utils,
@ -2461,6 +2462,18 @@ def _raise_pending_errors(self, info):
if err: if err:
self.report_error(err, tb=False) self.report_error(err, tb=False)
def sort_formats(self, info_dict):
formats = self._get_formats(info_dict)
if not formats:
return
# Backward compatibility with InfoExtractor._sort_formats
field_preference = formats[0].pop('__sort_fields', None)
if field_preference:
info_dict['_format_sort_fields'] = field_preference
formats.sort(key=FormatSorter(
self, info_dict.get('_format_sort_fields', [])).calculate_preference)
def process_video_result(self, info_dict, download=True): def process_video_result(self, info_dict, download=True):
assert info_dict.get('_type', 'video') == 'video' assert info_dict.get('_type', 'video') == 'video'
self._num_videos += 1 self._num_videos += 1
@ -2546,6 +2559,7 @@ def sanitize_numeric_fields(info):
info_dict['requested_subtitles'] = self.process_subtitles( info_dict['requested_subtitles'] = self.process_subtitles(
info_dict['id'], subtitles, automatic_captions) info_dict['id'], subtitles, automatic_captions)
self.sort_formats(info_dict)
formats = self._get_formats(info_dict) formats = self._get_formats(info_dict)
# or None ensures --clean-infojson removes it # or None ensures --clean-infojson removes it

View File

@ -344,6 +344,7 @@ class InfoExtractor:
'unlisted' or 'public'. Use 'InfoExtractor._availability' 'unlisted' or 'public'. Use 'InfoExtractor._availability'
to set it to set it
_old_archive_ids: A list of old archive ids needed for backward compatibility _old_archive_ids: A list of old archive ids needed for backward compatibility
_format_sort_fields: A list of fields to use for sorting formats
__post_extractor: A function to be called just before the metadata is __post_extractor: A function to be called just before the metadata is
written to either disk, logger or console. The function written to either disk, logger or console. The function
must return a dict which will be added to the info_dict. must return a dict which will be added to the info_dict.
@ -1698,9 +1699,8 @@ def __init__(ie, *args, **kwargs):
return FormatSort return FormatSort
def _sort_formats(self, formats, field_preference=[]): def _sort_formats(self, formats, field_preference=[]):
if not formats: if formats and field_preference:
return formats[0]['__sort_fields'] = field_preference
formats.sort(key=FormatSorter(self._downloader, field_preference).calculate_preference)
def _check_formats(self, formats, video_id): def _check_formats(self, formats, video_id):
if formats: if formats: