Repository: yt-dlp/yt-dlp
File: youtube_dlc/downloader/external.py (Python)

Code before:

    def _configuration_args(self, default=[]):
        return cli_configuration_args(
            self.params, 'external_downloader_args', self.get_basename(), default)[0]

    def _call_downloader(self, tmpfilename, info_dict):
        """ Either overwrite this or implement _make_cmd """
        cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)]

        self._debug_cmd(cmd)

        p = subprocess.Popen(
            cmd, stderr=subprocess.PIPE)
        _, stderr = process_communicate_or_kill(p)
        if p.returncode != 0:
            self.to_stderr(stderr.decode('utf-8', 'replace'))

        if 'url_list' in info_dict:
            file_list = []
            for [i, url] in enumerate(info_dict['url_list']):
                tmpsegmentname = '%s_%s.frag' % (tmpfilename, i)
                file_list.append(tmpsegmentname)
            if 'key_list' in info_dict:
                key_list = info_dict['key_list']
            else:
                key_list = None
            decrypt_info = None
            dest, _ = sanitize_open(tmpfilename, 'wb')
            for [i, file] in enumerate(file_list):
                src, _ = sanitize_open(file, 'rb')
                if key_list:
                    decrypt_info = next((x for x in key_list if x['INDEX'] == i), decrypt_info)
                    if decrypt_info['METHOD'] == 'AES-128':
                        iv = decrypt_info.get('IV')
                        decrypt_info['KEY'] = decrypt_info.get('KEY') or self.ydl.urlopen(
                            self._prepare_url(info_dict, info_dict.get('_decryption_key_url') or decrypt_info['URI'])).read()
                        encrypted_data = src.read()
                        decrypted_data = AES.new(
                            decrypt_info['KEY'], AES.MODE_CBC, iv).decrypt(encrypted_data)
                        dest.write(decrypted_data)
                    else:
                        fragment_data = src.read()
                        dest.write(fragment_data)
                else:
                    fragment_data = src.read()
                    dest.write(fragment_data)
                src.close()
            dest.close()
            if not self.params.get('keep_fragments', False):
                for file_path in file_list:
                    try:

Review comment (suggestion):
```suggestion
            key_list = info_dict.get('key_list')
```

Code after:

    def _configuration_args(self, default=[]):
        return cli_configuration_args(
            self.params, 'external_downloader_args', self.get_basename(), default)[0]

    def _call_downloader(self, tmpfilename, info_dict):
        """ Either overwrite this or implement _make_cmd """
        cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)]

        self._debug_cmd(cmd)

        p = subprocess.Popen(
            cmd, stderr=subprocess.PIPE)
        _, stderr = process_communicate_or_kill(p)
        if p.returncode != 0:
            self.to_stderr(stderr.decode('utf-8', 'replace'))

        if 'url_list' in info_dict:
            file_list = []
            for [i, url] in enumerate(info_dict['url_list']):
                tmpsegmentname = '%s_%s.frag' % (tmpfilename, i)
                file_list.append(tmpsegmentname)
            key_list = info_dict.get('key_list')
            decrypt_info = None
            dest, _ = sanitize_open(tmpfilename, 'wb')
            for i, file in enumerate(file_list):
                src, _ = sanitize_open(file, 'rb')
                if key_list:
                    decrypt_info = next((x for x in key_list if x['INDEX'] == i), decrypt_info)
                    if decrypt_info['METHOD'] == 'AES-128':
                        iv = decrypt_info.get('IV')
                        decrypt_info['KEY'] = decrypt_info.get('KEY') or self.ydl.urlopen(
                            self._prepare_url(info_dict, info_dict.get('_decryption_key_url') or decrypt_info['URI'])).read()
                        encrypted_data = src.read()
                        decrypted_data = AES.new(
                            decrypt_info['KEY'], AES.MODE_CBC, iv).decrypt(encrypted_data)
                        dest.write(decrypted_data)
                    else:
                        fragment_data = src.read()
                        dest.write(fragment_data)
                else:
                    fragment_data = src.read()
                    dest.write(fragment_data)
                src.close()
            dest.close()
            if not self.params.get('keep_fragments', False):
                for file_path in file_list:
                    try:
                        os.remove(file_path)
                    except OSError as ose:
                        self.report_error("Unable to delete file %s; %s" % (file_path, error_to_compat_str(ose)))