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

Code before:
    _TESTS = [{
        'url': 'http://www.rtve.es/alacarta/videos/la-aventura-del-saber/aventuraentornosilla/3088905/',
        'md5': 'a964547824359a5753aef09d79fe984b',
        'info_dict': {
            'id': '3088905',
            'ext': 'mp4',
            'title': 'En torno a la silla',
            'duration': 1216.981,
            'series': 'La aventura del Saber',
            'thumbnail': 'https://img2.rtve.es/v/aventuraentornosilla_3088905.png',
        },
    }, {
        'note': 'Live stream',
        'url': 'http://www.rtve.es/alacarta/videos/television/24h-live/1694255/',
        'info_dict': {
            'id': '1694255',
            'ext': 'mp4',
            'title': 're:^24H LIVE [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
            'is_live': True,
            'live_status': 'is_live',
            'thumbnail': r're:https://img2\.rtve\.es/v/.*\.png',
        },
        'params': {
            'skip_download': 'live stream',
        },
        'expected_warnings': ['Ignoring subtitle tracks found in the HLS manifest'],
    }, {
        'url': 'http://www.rtve.es/alacarta/videos/servir-y-proteger/servir-proteger-capitulo-104/4236788/',
        'md5': 'f3cf0d1902d008c48c793e736706c174',
        'info_dict': {
            'id': '4236788',
            'ext': 'mp4',
            'title': 'Episodio 104',
            'duration': 3222.8,
            'thumbnail': r're:https://img2\.rtve\.es/v/.*\.png',
            'series': 'Servir y proteger',
        },
        'expected_warnings': ['Failed to download MPD manifest', 'Failed to download m3u8 information'],
    }, {
        'url': 'http://www.rtve.es/m/alacarta/videos/cuentame-como-paso/cuentame-como-paso-t16-ultimo-minuto-nuestra-vida-capitulo-276/2969138/?media=tve',
        'only_matching': True,
    }, {
        'url': 'http://www.rtve.es/filmoteca/no-do/not-1-introduccion-primer-noticiario-espanol/1465256/',
        'only_matching': True,
    }, {
        'url': 'https://www.rtve.es/play/videos/saber-vivir/07-07-24/16177116/',
        'md5': '9eebcf6e8d6306c3b7c46e86a0115f55',
        'info_dict': {
            'id': '16177116',
            'ext': 'mp4',
            'title': 'Saber vivir - 07/07/24',

Review comment (refactor):
Please change the format extraction to return both video and subtitle data. So using `self._extract_m3u8_formats_and_subtitles` and `self._extract_mpd_formats_and_subtitles`

Code after:
    def _decrypt_url(png):
        encrypted_data = io.BytesIO(base64.b64decode(png)[8:])
        while True:
            length_data = encrypted_data.read(4)
            length = struct.unpack('!I', length_data)[0]
            chunk_type = encrypted_data.read(4)
            if chunk_type == b'IEND':
                break
            data = encrypted_data.read(length)
            if chunk_type == b'tEXt':
                data = bytes(filter(None, data))
                alphabet_data, _, url_data = data.partition(b'#')
                quality_str, _, url_data = url_data.rpartition(b'%%')
                quality_str = quality_str.decode() or ''
                alphabet = RTVEBaseIE._get_alphabet(alphabet_data)
                url = RTVEBaseIE._get_url(alphabet, url_data)
                yield quality_str, url
            encrypted_data.read(4)  # CRC

    @staticmethod
    def _get_url(alphabet, url_data):
        url = ''
        f = 0
        e = 3
        b = 1
        for char in url_data.decode('iso-8859-1'):
            if f == 0:
                l = int(char) * 10
                f = 1
            else:
                if e == 0:
                    l += int(char)
                    url += alphabet[l]
                    e = (b + 3) % 4
                    f = 0
                    b += 1
                else:
                    e -= 1
        return url

    @staticmethod
    def _get_alphabet(alphabet_data):
        alphabet = []
        e = 0
        d = 0
        for char in alphabet_data.decode('iso-8859-1'):
            if d == 0:
                alphabet.append(char)
                d = e = (e + 1) % 4
            else:
                d -= 1