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

Code before:
        # to properly set audio-only and video-only formats

        fmts = self._extract_m3u8_formats(
            media_url, video_id, 'mp4', m3u8_id='hls', fatal=False)

        for idx, f in enumerate(fmts):
            man_url = f.get('url')
            if re.search(r'chunklist(_b\d+)*_ao(_|\.)', man_url):  # audio only
                fmts[idx]['vcodec'] = 'none'
                if 'acodec' not in fmts[idx]:
                    fmts[idx]['acodec'] = 'mp4a'
            elif re.search(r'chunklist(_b\d+)*_vo(_|\.)', man_url):  # video only
                fmts[idx]['acodec'] = 'none'
                if 'vcodec' not in fmts[idx]:
                    fmts[idx]['vcodec'] = 'avc1'
            elif re.search(r'chunklist\.m3u8', man_url):  # audio+video
                if 'acodec' in fmts[idx] and fmts[idx]['acodec'] == 'none':
                    fmts[idx]['acodec'] = 'mp4a'
                if 'vcodec' in fmts[idx] and fmts[idx]['vcodec'] == 'none':
                    fmts[idx]['vcodec'] = 'avc1'
            else:  # audio+video
                if 'acodec' not in fmts[idx]:
                    fmts[idx]['acodec'] = 'mp4a'
                if 'vcodec' not in fmts[idx]:
                    fmts[idx]['vcodec'] = 'avc1'
        return fmts

    def _extract_relinker_info(self, relinker_url, video_id, audio_only=False):
        def fix_cdata(s):
            # remove \r\n\t before and after <![CDATA[ ]]> to avoid
            # polluted text with xpath_text
            s = re.sub(r'(\]\]>)[\r\n\t]+(</)', '\\1\\2', s)
            return re.sub(r'(>)[\r\n\t]+(<!\[CDATA\[)', '\\1\\2', s)

        if not re.match(r'https?://', relinker_url):
            return {'formats': [{'url': relinker_url}]}

        # set User-Agent to generic 'Rai' to avoid quality filtering from
        # the media server and get the maximum qualities available
        relinker = self._download_xml(
            relinker_url, video_id, note='Downloading XML metadata',
            transform_source=fix_cdata, query={'output': 64},
            headers={**self.geo_verification_headers(), 'User-Agent': 'Rai'})

        if xpath_text(relinker, './license_url', default='{}') != '{}':
            self.report_drm(video_id)

        is_live = xpath_text(relinker, './is_live', default='N') == 'Y'
        duration = parse_duration(xpath_text(relinker, './duration', default=None))
        media_url = xpath_text(relinker, './url[@type="content"]', default=None)


Review comment (refactor):
could we simplify this a bit like so?
```suggestion
        fmts = self._extract_m3u8_formats(
            media_url, video_id, 'mp4', m3u8_id='hls', fatal=False)

        for f in fmts:
            if not f.get('acodec'):
                f['acodec'] = 'mp4a'
            if not f.get('vcodec'):
                f['vcodec'] = 'avc1'
            man_url = f['url']
            if re.search(r'chunklist(?:_b\d+)*_ao[_.]', man_url):  # audio only
                f['vcodec'] = 'none'
            elif re.search(r'chunklist(?:_b\d+)*_vo[_.]', man_url):  # video only
                f['acodec'] = 'none'
            else:  # video+audio
                if f['acodec'] == 'none':
                    f['acodec'] = 'mp4a'
                if f['vcodec'] == 'none':
                    f['vcodec'] = 'avc1'

        return fmts
```

Code after:

        # Fix malformed m3u8 manifests by setting audio-only/video-only formats
        for f in fmts:
            if not f.get('acodec'):
                f['acodec'] = 'mp4a'
            if not f.get('vcodec'):
                f['vcodec'] = 'avc1'
            man_url = f['url']
            if re.search(r'chunklist(?:_b\d+)*_ao[_.]', man_url):  # audio only
                f['vcodec'] = 'none'
            elif re.search(r'chunklist(?:_b\d+)*_vo[_.]', man_url):  # video only
                f['acodec'] = 'none'
            else:  # video+audio
                if f['acodec'] == 'none':
                    f['acodec'] = 'mp4a'
                if f['vcodec'] == 'none':
                    f['vcodec'] = 'avc1'

        return fmts

    def _extract_relinker_info(self, relinker_url, video_id, audio_only=False):
        def fix_cdata(s):
            # remove \r\n\t before and after <![CDATA[ ]]> to avoid
            # polluted text with xpath_text
            s = re.sub(r'(\]\]>)[\r\n\t]+(</)', '\\1\\2', s)
            return re.sub(r'(>)[\r\n\t]+(<!\[CDATA\[)', '\\1\\2', s)

        if not re.match(r'https?://', relinker_url):
            return {'formats': [{'url': relinker_url}]}

        # set User-Agent to generic 'Rai' to avoid quality filtering from
        # the media server and get the maximum qualities available
        relinker = self._download_xml(
            relinker_url, video_id, note='Downloading XML metadata',
            transform_source=fix_cdata, query={'output': 64},
            headers={**self.geo_verification_headers(), 'User-Agent': 'Rai'})

        if xpath_text(relinker, './license_url', default='{}') != '{}':
            self.report_drm(video_id)

        is_live = xpath_text(relinker, './is_live', default='N') == 'Y'
        duration = parse_duration(xpath_text(relinker, './duration', default=None))
        media_url = xpath_text(relinker, './url[@type="content"]', default=None)

        if not media_url:
            self.raise_no_formats('The relinker returned no media url')

        # geo flag is a bit unreliable and not properly set all the time
        geoprotection = xpath_text(relinker, './geoprotection', default='N') == 'Y'

        ext = determine_ext(media_url)