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

Code before:
        }

        return info_dict, heartbeat_info_dict

    def _extract_format_for_quality(self, video_id, audio_quality, video_quality, dmc_protocol):

        if not audio_quality.get('isAvailable') or not video_quality.get('isAvailable'):
            return None

        format_id = '-'.join(
            [remove_start(s['id'], 'archive_') for s in (video_quality, audio_quality)] + [dmc_protocol])

        vid_qual_label = traverse_obj(video_quality, ('metadata', 'label'))

        return {
            'url': 'niconico_dmc:%s/%s/%s' % (video_id, video_quality['id'], audio_quality['id']),
            'format_id': format_id,
            'format_note': join_nonempty('DMC', vid_qual_label, dmc_protocol.upper(), delim=' '),
            'ext': 'mp4',  # Session API are used in HTML5, which always serves mp4
            'acodec': 'aac',
            'vcodec': 'h264',
            'abr': float_or_none(traverse_obj(audio_quality, ('metadata', 'bitrate')), scale=1000),
            'vbr': float_or_none(traverse_obj(video_quality, ('metadata', 'bitrate')), scale=1000),
            'asr': int_or_none(traverse_obj(audio_quality, ('metadata', 'samplingRate'))),
            'height': traverse_obj(video_quality, ('metadata', 'resolution', 'height')),
            'width': traverse_obj(video_quality, ('metadata', 'resolution', 'width')),
            'quality': -2 if 'low' in video_quality['id'] else None,
            'protocol': 'niconico_dmc',
            'expected_protocol': dmc_protocol,  # XXX: This is not a documented field
            'http_headers': {
                'Origin': 'https://www.nicovideo.jp',
                'Referer': 'https://www.nicovideo.jp/watch/' + video_id,
            }
        }

    def _real_extract(self, url):
        video_id = self._match_id(url)

        try:
            webpage, handle = self._download_webpage_handle(
                'https://www.nicovideo.jp/watch/' + video_id, video_id)
            if video_id.startswith('so'):
                video_id = self._match_id(handle.url)

            api_data = self._parse_json(self._html_search_regex(
                'data-api-data="([^"]+)"', webpage,
                'API data', default='{}'), video_id)
        except ExtractorError as e:
            try:
                api_data = self._download_json(
                    'https://www.nicovideo.jp/api/watch/v3/%s?_frontendId=6&_frontendVersion=0&actionTrackId=AAAAAAAAAA_%d' % (video_id, round(time.time() * 1000)), video_id,

Review comment (suggestion):
```suggestion
            **traverse_obj(audio_quality, ('metadata', {
                'abr': ('bitrate', {functools.partial(float_or_none, scale=1000)}),
                'asr': ('samplingRate', {int_or_none}),
            }),
            **traverse_obj(video_quality, ('metadata', {
                'vbr': ('bitrate', {functools.partial(float_or_none, scale=1000)}),
                'height': ('resolution', 'height', {int_or_none}),
                'width': ('resolution', 'width', {int_or_none}),
            }),
```

Code after:
        return info_dict, heartbeat_info_dict

    def _extract_format_for_quality(self, video_id, audio_quality, video_quality, dmc_protocol):

        if not audio_quality.get('isAvailable') or not video_quality.get('isAvailable'):
            return None

        format_id = '-'.join(
            [remove_start(s['id'], 'archive_') for s in (video_quality, audio_quality)] + [dmc_protocol])

        vid_qual_label = traverse_obj(video_quality, ('metadata', 'label'))

        return {
            'url': 'niconico_dmc:%s/%s/%s' % (video_id, video_quality['id'], audio_quality['id']),
            'format_id': format_id,
            'format_note': join_nonempty('DMC', vid_qual_label, dmc_protocol.upper(), delim=' '),
            'ext': 'mp4',  # Session API are used in HTML5, which always serves mp4
            'acodec': 'aac',
            'vcodec': 'h264',
            **traverse_obj(audio_quality, ('metadata', {
                'abr': ('bitrate', {functools.partial(float_or_none, scale=1000)}),
                'asr': ('samplingRate', {int_or_none}),
            })),
            **traverse_obj(video_quality, ('metadata', {
                'vbr': ('bitrate', {functools.partial(float_or_none, scale=1000)}),
                'height': ('resolution', 'height', {int_or_none}),
                'width': ('resolution', 'width', {int_or_none}),
            })),
            'quality': -2 if 'low' in video_quality['id'] else None,
            'protocol': 'niconico_dmc',
            'expected_protocol': dmc_protocol,  # XXX: This is not a documented field
            'http_headers': {
                'Origin': 'https://www.nicovideo.jp',
                'Referer': 'https://www.nicovideo.jp/watch/' + video_id,
            }
        }

    def _yield_dmc_formats(self, api_data, video_id):
        dmc_data = traverse_obj(api_data, ('media', 'delivery', 'movie'))
        audios = traverse_obj(dmc_data, ('audios', ..., {dict}))
        videos = traverse_obj(dmc_data, ('videos', ..., {dict}))
        protocols = traverse_obj(dmc_data, ('session', 'protocols', ..., {str}))
        if not all((audios, videos, protocols)):
            return

        for audio_quality, video_quality, protocol in itertools.product(audios, videos, protocols):
            if fmt := self._extract_format_for_quality(video_id, audio_quality, video_quality, protocol):
                yield fmt

    def _yield_dms_formats(self, api_data, video_id):
        fmt_filter = lambda _, v: v['isAvailable'] and v['id']