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

Code before:

        if api_json.get('is_drm_protected'):
            self.report_drm(video_id)

        formats, subtitles = [], {}
        for manifest_type in traverse_obj(api_json, ('sources', {dict.keys}, ...)):
            for manifest_url in traverse_obj(api_json, ('sources', manifest_type, ..., 'src', {url_or_none})):
                if manifest_type == 'hls':
                    fmts, subs = self._extract_m3u8_formats_and_subtitles(
                        manifest_url, video_id, fatal=False, m3u8_id='hls')
                elif manifest_type == 'dash':
                    fmts, subs = self._extract_mpd_formats_and_subtitles(
                        manifest_url, video_id, fatal=False, mpd_id='dash')
                else:
                    continue
                formats.extend(fmts)
                self._merge_subtitles(subs, target=subtitles)

        for sub_url in traverse_obj(api_json, ('_embedded', 'subtitle', lambda k, _: k.endswith('_url'), {url_or_none})):
            subtitle_type = sub_url.split('.')[-1]
            self._merge_subtitles({'de': [{'url': sub_url, 'ext': subtitle_type}]}, target=subtitles)
        if 'de' in subtitles:
            subtitles['de'].sort(
                # sort that vtt is default so `--embed-subs` is working out of the box
                key=lambda sub: 1 if sub['ext'] == 'vtt' else 0
            )

        return {
            'id': video_id,
            'formats': formats,
            'subtitles': subtitles,
            **traverse_obj(api_json, {
                'age_limit': ('age_classification', {parse_age_limit}),
                'duration': ('duration_second', {float_or_none}),
                'title': (('title', 'headline'), {str}),
                'description': (('description', 'teaser_text'), {str}),
                'media_type': ('video_type', {str}),
            }, get_all=False),
        }

    def _real_extract(self, url):
        video_id = self._match_valid_url(url).group('id')
        webpage = self._download_webpage(url, video_id)

        return {
            'id': video_id,
            'title': self._html_search_meta(['og:title', 'twitter:title'], webpage, default=None),
            'description': self._html_search_meta(
                ['description', 'og:description', 'twitter:description'], webpage, default=None),
            **self._search_json_ld(webpage, video_id, fatal=False),
            **self._extract_video(video_id),

Review comment (suggestion):
I think this is a bit cleaner:
```suggestion
        for sub_url in traverse_obj(api_json, (
                '_embedded', 'subtitle',
                ('xml_url', 'sami_url', 'stl_url', 'ttml_url', 'srt_url', 'vtt_url'), {url_or_none})):
            self._merge_subtitles({'de': [{'url': sub_url}]}, target=subtitles)
```

If you disagree, we can keep this open for the other reviewer to decide/maybe come up with an alternative.

Code after:

        if traverse_obj(api_json, 'is_drm_protected'):
            self.report_drm(video_id)

        formats, subtitles = [], {}
        for manifest_type in traverse_obj(api_json, ('sources', {dict.keys}, ...)):
            for manifest_url in traverse_obj(api_json, ('sources', manifest_type, ..., 'src', {url_or_none})):
                if manifest_type == 'hls':
                    fmts, subs = self._extract_m3u8_formats_and_subtitles(
                        manifest_url, video_id, fatal=False, m3u8_id='hls')
                elif manifest_type == 'dash':
                    fmts, subs = self._extract_mpd_formats_and_subtitles(
                        manifest_url, video_id, fatal=False, mpd_id='dash')
                else:
                    continue
                formats.extend(fmts)
                self._merge_subtitles(subs, target=subtitles)

        for sub_url in traverse_obj(api_json, (
                '_embedded', 'subtitle',
                ('xml_url', 'sami_url', 'stl_url', 'ttml_url', 'srt_url', 'vtt_url'), {url_or_none})):
            self._merge_subtitles({'de': [{'url': sub_url}]}, target=subtitles)

        return {
            'id': video_id,
            'formats': formats,
            'subtitles': subtitles,
            **traverse_obj(api_json, {
                'age_limit': ('age_classification', {parse_age_limit}),
                'duration': ('duration_second', {float_or_none}),
                'title': (('title', 'headline'), {str}),
                'description': (('description', 'teaser_text'), {str}),
                'media_type': ('video_type', {str}),
            }, get_all=False),
        }

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

        return {
            'id': video_id,
            'title': self._html_search_meta(['og:title', 'twitter:title'], webpage, default=None),
            'description': self._html_search_meta(
                ['description', 'og:description', 'twitter:description'], webpage, default=None),
            **self._search_json_ld(webpage, video_id, fatal=False),
            **self._extract_video(video_id),
        }