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

Code before:
                    **parse_resolution(value, lenient=True),
                })
                continue

            for k, img_url in value.items():
                res = parse_resolution(k, lenient=True) or parse_resolution(img_url, lenient=True)
                width, height = res.get('width'), res.get('height')

                thumbnails.append({
                    'id': f'{name}_{width}x{height}',
                    'url': img_url,
                    'ext': traverse_obj(parse_qs(img_url), ('image', 0, {determine_ext(default_ext='jpg')})),
                    **res,
                })

        for cookie in cookies:
            self._set_cookie(
                cookie['domain'], cookie['name'], cookie['value'],
                expire_time=unified_timestamp(cookie.get('expires')), path=cookie['path'], secure=cookie['secure'])

        formats = self._extract_m3u8_formats(m3u8_url, video_id, ext='mp4', live=True)
        qualities = [
            *('audio_high', 'audio_low'),
            *(q for q in qualities[1:] if not q.startswith('audio')),
        ]
        for fmt, q in zip(formats, qualities):
            fmt.update({
                'format_id': q,
                'protocol': 'niconico_live',
                'ws': ws,
                'video_id': video_id,
                'live_latency': 'high',
                'origin': hostname,
            })

        return {
            'id': video_id,
            'title': title,
            **traverse_obj(embedded_data, {
                'view_count': ('program', 'statistics', 'watchCount'),
                'comment_count': ('program', 'statistics', 'commentCount'),
                'uploader': ('program', 'supplier', 'name'),
                'channel': ('socialGroup', 'name'),
                'channel_id': ('socialGroup', 'id'),
                'channel_url': ('socialGroup', 'socialGroupPageUrl'),
            }),
            'description': clean_html(traverse_obj(embedded_data, ('program', 'description'))),
            'timestamp': int_or_none(traverse_obj(embedded_data, ('program', 'openTime'))),
            'is_live': True,
            'thumbnails': thumbnails,
            'formats': formats,

Review comment (performance):
I don't like how we are depending on the audio formats to be delivered in descending audio quality, and I also think this is a bit hard to understand 

What about this?

```py
        common_format_info = {
            'protocol': 'niconico_live',
            'ws': ws,
            'video_id': video_id,
            'live_latency': 'high',
            'origin': hostname,
        }
        video_fmts = [f for f in formats if f.get('acodec') == 'none']
        # qualities[0] is always 'abr' and is not useful
        qualities = [q for q in qualities[1:] if not q.startswith('audio_')]
        for fmt, q in zip(video_fmts, qualities):
            fmt.update({
                **common_format_info,
                'format_id': q,
            })
        for fmt in traverse_obj(formats, lambda _, v: v['vcodec'] == 'none'):
            abr = parse_bitrate(fmt['url'].lower())
            fmt.update({
                **common_format_info,
                'format_id': 'audio_high' if abr == 192 else 'audio_low' if abr == 96 else fmt['format_id'],
                'abr': abr,
            })
```

It's more code but I think it's more readable and more robust (and we get the audio bitrates with it)

Code after:
                    'url': value,
                    **parse_resolution(value, lenient=True),
                })
                continue

            for k, img_url in value.items():
                res = parse_resolution(k, lenient=True) or parse_resolution(img_url, lenient=True)
                width, height = res.get('width'), res.get('height')

                thumbnails.append({
                    'id': f'{name}_{width}x{height}',
                    'url': img_url,
                    'ext': traverse_obj(parse_qs(img_url), ('image', 0, {determine_ext(default_ext='jpg')})),
                    **res,
                })

        for cookie in cookies:
            self._set_cookie(
                cookie['domain'], cookie['name'], cookie['value'],
                expire_time=unified_timestamp(cookie.get('expires')), path=cookie['path'], secure=cookie['secure'])

        fmt_common = {
            'live_latency': 'high',
            'origin': hostname,
            'protocol': 'niconico_live',
            'video_id': video_id,
            'ws': ws,
        }
        q_iter = (q for q in qualities[1:] if not q.startswith('audio_'))  # ignore initial 'abr'
        a_map = {96: 'audio_low', 192: 'audio_high'}

        formats = self._extract_m3u8_formats(m3u8_url, video_id, ext='mp4', live=True)
        for fmt in formats:
            if fmt.get('acodec') == 'none':
                fmt['format_id'] = next(q_iter, fmt['format_id'])
            elif fmt.get('vcodec') == 'none':
                abr = parse_bitrate(fmt['url'].lower())
                fmt.update({
                    'abr': abr,
                    'format_id': a_map.get(abr, fmt['format_id']),
                })
            fmt.update(fmt_common)

        return {
            'id': video_id,
            'title': title,
            **traverse_obj(embedded_data, {
                'view_count': ('program', 'statistics', 'watchCount'),
                'comment_count': ('program', 'statistics', 'commentCount'),
                'uploader': ('program', 'supplier', 'name'),
                'channel': ('socialGroup', 'name'),