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

Code before:

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

        movie_metadata = self._download_json(
            'https://rest.arbeitsagentur.de/infosysbub/berufetv/pc/v1/film-metadata',
            video_id, 'Downloading JSON metadata',
            headers={'X-API-Key': '79089773-4892-4386-86e6-e8503669f426'}, fatal=False)

        meta = next(
            item for item in movie_metadata.get('metadaten') if video_id == item.get('miId')
        ) if movie_metadata else {}

        video = self._download_json(
            'https://d.video-cdn.net/play/player/8YRzUk6pTzmBdrsLe9Y88W/video/%s' % (video_id),
            video_id, 'Downloading video JSON')

        video_sources = traverse_obj(video, ['videoSources', 'html'])

        if not video_sources:
            raise ExtractorError('Failed to obtain video source list')

        formats = []
        subtitles = {}

        for key, source in video_sources.items():
            if key == 'auto':
                m3u8_url = source[0]['source']
                fmts, subs = self._extract_m3u8_formats_and_subtitles(m3u8_url, video_id)
                formats += fmts
                subtitles = subs
            else:
                formats.append({
                    'url': source[0]['source'],
                    'ext': mimetype2ext(source[0]['mimeType']),
                    'format_id': key,
                })

        video_tracks = video.get('videoTracks') or []
        for track in video_tracks:
            if track.get('type') != 'SUBTITLES':
                continue
            subtitles[track['language']] = [{
                'url': track['source'],
                'name': track['label'],
                'ext': 'vtt'
            }]

        return {
            'id': video_id,
            'title': meta.get('titel') or traverse_obj(video, ['videoMetaData', 'title']),

Review comment (bug):
is this error expected?
```suggestion
        video_sources = traverse_obj(video, ['videoSources', 'html'])

        if not video_sources:
            raise ExtractorError('Failed to obtain video source list', expected=True)

        formats = []
        subtitles = {}

        for key, source in video_sources.items():
```
Otherwise, just do
```suggestion
        formats, subtitles = [], {}
        for key, source in video['videoSources']['html'].items():
```

Code after:

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

        movie_metadata = self._download_json(
            'https://rest.arbeitsagentur.de/infosysbub/berufetv/pc/v1/film-metadata',
            video_id, 'Downloading JSON metadata',
            headers={'X-API-Key': '79089773-4892-4386-86e6-e8503669f426'}, fatal=False)

        meta = traverse_obj(
            movie_metadata, ('metadaten', lambda _, i: video_id == i['miId']),
            get_all=False, default={})

        video = self._download_json(
            f'https://d.video-cdn.net/play/player/8YRzUk6pTzmBdrsLe9Y88W/video/{video_id}',
            video_id, 'Downloading video JSON')

        formats, subtitles = [], {}
        for key, source in video['videoSources']['html'].items():
            if key == 'auto':
                fmts, subs = self._extract_m3u8_formats_and_subtitles(source[0]['source'], video_id)
                formats += fmts
                subtitles = subs
            else:
                formats.append({
                    'url': source[0]['source'],
                    'ext': mimetype2ext(source[0]['mimeType']),
                    'format_id': key,
                })

        for track in video.get('videoTracks') or []:
            if track.get('type') != 'SUBTITLES':
                continue
            subtitles.setdefault(track['language'], []).append({
                'url': track['source'],
                'name': track.get('label'),
                'ext': 'vtt'
            })

        return {
            'id': video_id,
            'title': meta.get('titel') or traverse_obj(video, ('videoMetaData', 'title')),
            'description': meta.get('beschreibung'),
            'thumbnail': meta.get('thumbnail') or f'https://asset-out-cdn.video-cdn.net/private/videos/{video_id}/thumbnails/active',
            'duration': float_or_none(video.get('duration'), scale=1000),
            'categories': [meta['kategorie']] if meta.get('kategorie') else None,
            'tags': meta.get('themengebiete'),
            'subtitles': subtitles,
            'formats': formats,
        }