Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/twitch.py (Python)

Code before:
            'sig': clip['playbackAccessToken']['signature'],
            'token': clip['playbackAccessToken']['value'],
        }
        asset_default = traverse_obj(clip, ('assets', 0, {dict})) or {}
        asset_portrait = traverse_obj(clip, ('assets', 1, {dict})) or {}

        formats = []
        default_aspect_ratio = float_or_none(asset_default.get('aspectRatio'))
        formats.extend(traverse_obj(asset_default, ('videoQualities', lambda _, v: url_or_none(v['sourceURL']), {
            'url': ('sourceURL', {update_url_query(query=access_query)}),
            'format_id': ('quality', {str}),
            'height': ('quality', {int_or_none}),
            'fps': ('frameRate', {float_or_none}),
            'aspect_ratio': {value(default_aspect_ratio)},
        })))
        portrait_aspect_ratio = float_or_none(asset_portrait.get('aspectRatio'))
        for i, quality in enumerate(traverse_obj(asset_portrait, ('videoQualities'), default=[])):
            if source := traverse_obj(quality, ('sourceURL', {url_or_none})):
                formats.append({
                    'url': update_url_query(source, access_query),
                    'format_id': join_nonempty('portrait', 'best' if i == 0 else quality.get('quality')),
                    'height': int_or_none(quality.get('quality')),
                    'fps': float_or_none(quality.get('frameRate')),
                    'aspect_ratio': portrait_aspect_ratio,
                    'quality': -2,
                })

        thumbnails = []
        thumb_default = clip.get('thumbnailURL')
        thumb_asset_default = asset_default.get('thumbnailURL')
        thumbnails.append({
            'id': 'default',
            'url': thumb_asset_default,
        })
        if thumb_asset_default != thumb_default:
            thumbnails.append({
                'id': 'small',
                'url': thumb_default,
            })
        if thumb_asset_portrait := asset_portrait.get('thumbnailURL'):
            thumbnails.append({
                'id': 'portrait',
                'url': thumb_asset_portrait,
            })

        old_id = self._search_regex(r'%7C(\d+)(?:-\d+)?.mp4', formats[-1]['url'], 'old id', default=None)

        return {
            'id': clip.get('id') or slug,
            '_old_archive_ids': [make_archive_id(self, old_id)] if old_id else None,
            'display_id': slug,

Review comment (suggestion):
I don't like having `portrait-best` for the format ID if all of the other formats (including the best default format) are just `(portrait-)?{quality}`
```suggestion
        for traverse_obj(asset_portrait, ('videoQualities', lambda _, v: url_or_none(v['sourceURL'])):
            formats.append({
                'url': update_url_query(source['sourceURL'], access_query),
                'format_id': join_nonempty('portrait', source.get('quality')),
                'height': int_or_none(source.get('quality')),
                'fps': float_or_none(source.get('frameRate')),
                'aspect_ratio': portrait_aspect_ratio,
                'quality': -2,
            })
```

Code after:
            'token': clip['playbackAccessToken']['value'],
        }
        asset_default = traverse_obj(clip, ('assets', 0, {dict})) or {}
        asset_portrait = traverse_obj(clip, ('assets', 1, {dict})) or {}

        formats = []
        default_aspect_ratio = float_or_none(asset_default.get('aspectRatio'))
        formats.extend(traverse_obj(asset_default, ('videoQualities', lambda _, v: url_or_none(v['sourceURL']), {
            'url': ('sourceURL', {update_url_query(query=access_query)}),
            'format_id': ('quality', {str}),
            'height': ('quality', {int_or_none}),
            'fps': ('frameRate', {float_or_none}),
            'aspect_ratio': {value(default_aspect_ratio)},
        })))
        portrait_aspect_ratio = float_or_none(asset_portrait.get('aspectRatio'))
        for source in traverse_obj(asset_portrait, ('videoQualities', lambda _, v: url_or_none(v['sourceURL']))):
            formats.append({
                'url': update_url_query(source['sourceURL'], access_query),
                'format_id': join_nonempty('portrait', source.get('quality')),
                'height': int_or_none(source.get('quality')),
                'fps': float_or_none(source.get('frameRate')),
                'aspect_ratio': portrait_aspect_ratio,
                'quality': -2,
            })

        thumbnails = []
        thumb_asset_default_url = url_or_none(asset_default.get('thumbnailURL'))
        if thumb_asset_default_url:
            thumbnails.append({
                'id': 'default',
                'url': thumb_asset_default_url,
                'preference': 0,
            })
        if thumb_asset_portrait_url := url_or_none(asset_portrait.get('thumbnailURL')):
            thumbnails.append({
                'id': 'portrait',
                'url': thumb_asset_portrait_url,
                'preference': -1,
            })
        thumb_default_url = url_or_none(clip.get('thumbnailURL'))
        if thumb_default_url and thumb_default_url != thumb_asset_default_url:
            thumbnails.append({
                'id': 'small',
                'url': thumb_default_url,
                'preference': -2,
            })

        old_id = self._search_regex(r'%7C(\d+)(?:-\d+)?.mp4', formats[-1]['url'], 'old id', default=None)

        return {
            'id': clip.get('id') or slug,