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

Code before:
                    'width': video_res.get('width'),
                    'height': video_res.get('height'),
                    'vbr': video_res.get('bandwidth'),
                    'acodec': 'none',
                    'vcodec': video_res.get('codecs'),
                    'filesize': video_res.get('size'),
                })
        for aud in video_json.get('audio_resource', []):
            if aud.get('url'):
                formats.append({
                    'url': aud['url'],
                    'ext': 'mp4',
                    'abr': aud.get('bandwidth'),
                    'acodec': aud.get('codecs'),
                    'vcodec': 'none',
                    'filesize': aud.get('size'),
                })
        sub_json = self._call_api(f'/m/subtitle?ep_id={ep_id}&platform=web', ep_id)
        subtitles = {}
        for sub in sub_json.get('subtitles', []):
            sub_url = sub.get('url')
            if not sub_url:
                continue
            subtitles.setdefault(sub.get('key', 'en'), []).append({
                'url': sub_url,
            })
        self._sort_formats(formats)
        return formats, subtitles

    def _extract_ep_info(self, episode_data, ep_id):
        formats, subtitles = self._extract_formats_and_subtitles(ep_id)
        return {
            'id': ep_id,
            'title': episode_data['long_title'] or episode_data.get('title'),
            'thumbnail': episode_data.get('cover'),
            'episode_number': str_to_int(episode_data.get('title')),
            'formats': formats,
            'subtitles': subtitles,
        }


class BiliIntlIE(BiliIntlBaseIE):
    _VALID_URL = r'https?://(?:www\.)?bili(?P<type>bili\.tv|intl.com)/(?:[a-z]{2}/)?play/(?P<season_id>\d+)/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.bilibili.tv/en/play/34613/341736',
        'info_dict': {
            'id': '341736',
            'ext': 'mp4',
            'title': 'The First Night',
            'thumbnail': 'https://i0.hdslb.com/bfs/intl/management/91e30e5521235d9b163339a26a0b030ebda54310.png',
            'episode_number': 2,

Review comment (refactor):
formats and subtitle code seems completely seperate. plus, subtitles need an additional request. It is best to delegate this to `self._get_subtitles` and then call `self.extract_subtitles` in `_real_extract`

Code after:
        if not video_json:
            self.raise_login_required(method='cookies')
        video_json = video_json['playurl']
        formats = []
        for vid in video_json.get('video', []):
            video_res = vid.get('video_resource') or {}
            video_info = vid.get('stream_info') or {}
            if not video_res.get('url'):
                continue
            formats.append({
                'url': video_res['url'],
                'ext': 'mp4',
                'format_note': video_info.get('desc_words'),
                'width': video_res.get('width'),
                'height': video_res.get('height'),
                'vbr': video_res.get('bandwidth'),
                'acodec': 'none',
                'vcodec': video_res.get('codecs'),
                'filesize': video_res.get('size'),
            })
        for aud in video_json.get('audio_resource', []):
            if not aud.get('url'):
                continue
            formats.append({
                'url': aud['url'],
                'ext': 'mp4',
                'abr': aud.get('bandwidth'),
                'acodec': aud.get('codecs'),
                'vcodec': 'none',
                'filesize': aud.get('size'),
            })

        self._sort_formats(formats)
        return formats

    def _extract_ep_info(self, type, episode_data, ep_id):
        return {
            'id': ep_id,
            'title': episode_data.get('long_title') or episode_data['title'],
            'thumbnail': episode_data.get('cover'),
            'episode_number': str_to_int(episode_data.get('title')),
            'formats': self._get_formats(type, ep_id),
            'subtitles': self._get_subtitles(type, ep_id),
            'extractor_key': BiliIntlIE.ie_key(),
        }


class BiliIntlIE(BiliIntlBaseIE):
    _VALID_URL = r'https?://(?:www\.)?bili(?P<type>bili\.tv|intl.com)/(?:[a-z]{2}/)?play/(?P<season_id>\d+)/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.bilibili.tv/en/play/34613/341736',