Skip to content

Commit

Permalink
Add format selection criteria longside, shortside
Browse files Browse the repository at this point in the history
  • Loading branch information
mk-pmb committed Mar 13, 2022
1 parent 6508688 commit 31af229
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion youtube_dl/YoutubeDL.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,11 @@ class YoutubeDL(object):
'playlist_index',
))

_NUMERIC_FIELDS |= set(('longside', 'shortside',))
# ^-- Adding those late is a bad hack to try and make this patch be
# applicable far in the future even as long as maintainers don't
# agree that this list should be easy to maintain (issue 30737).

params = None
_ies = []
_pps = []
Expand Down Expand Up @@ -1088,7 +1093,7 @@ def _build_format_filter(self, filter_spec):
'!=': operator.ne,
}
operator_rex = re.compile(r'''(?x)\s*
(?P<key>width|height|tbr|abr|vbr|asr|filesize|filesize_approx|fps)
(?P<key>width|height|shortside|longside|tbr|abr|vbr|asr|filesize|filesize_approx|fps)
\s*(?P<op>%s)(?P<none_inclusive>\s*\?)?\s*
(?P<value>[0-9.]+(?:[kKmMgGtTpPeEzZyY]i?[Bb]?)?)
$
Expand Down Expand Up @@ -1376,6 +1381,8 @@ def _merge(formats_info):
formats_info[1].get('format_id')),
'width': formats_info[0].get('width'),
'height': formats_info[0].get('height'),
'longside': formats_info[0].get('longside'),
'shortside': formats_info[0].get('shortside'),
'resolution': formats_info[0].get('resolution'),
'fps': formats_info[0].get('fps'),
'vcodec': formats_info[0].get('vcodec'),
Expand Down Expand Up @@ -1484,6 +1491,17 @@ def sanitize_numeric_fields(info):
sanitize_string_field(info_dict, 'id')
sanitize_numeric_fields(info_dict)

def add_calculated_video_proprties(fmt):
dimensions = [fmt.get(side) for side in ('width', 'height',)]
dimensions = [n for n in dimensions if n is not None]
if len(dimensions):
fmt.update({
'shortside': min(*dimensions),
'longside': max(*dimensions),
})
for fmt in info_dict['formats']:
add_calculated_video_proprties(fmt)

if 'playlist' not in info_dict:
# It isn't part of a playlist
info_dict['playlist'] = None
Expand Down

0 comments on commit 31af229

Please sign in to comment.