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

Code before:
            dn = os.path.dirname(tmpfilename)
            with open(tmpfilename, 'wb') as dest:
                for i in file_list:
                    if 'decrypt_info' in info_dict:
                        decrypt_info = info_dict['decrypt_info']
                        with open(os.path.join(dn, i), 'rb') as src:
                            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:
                        shutil.copyfileobj(open(os.path.join(dn, i), 'rb'), dest)
            for x in file_list:
                try:
                    file_path = os.path.join(dn, x)
                    os.remove(file_path)
                except OSError as e:
                    print("Error: %s : %s" % (dn, e.strerror))
            os.remove('%s_urls.txt' % tmpfilename)

        return p.returncode


class HttpieFD(ExternalFD):
    @classmethod
    def available(cls):
        return check_executable('http', ['--version'])

    def _make_cmd(self, tmpfilename, info_dict):
        cmd = ['http', '--download', '--output', tmpfilename, info_dict['url']]

        if info_dict.get('http_headers') is not None:
            for key, val in info_dict['http_headers'].items():
                cmd += ['%s:%s' % (key, val)]
        return cmd


class FFmpegFD(ExternalFD):
    @classmethod
    def supports(cls, info_dict):
        return info_dict['protocol'] in ('http', 'https', 'ftp', 'ftps', 'm3u8', 'rtsp', 'rtmp', 'mms')

    @classmethod
    def available(cls):
        return FFmpegPostProcessor().available

    def _call_downloader(self, tmpfilename, info_dict):

Review comment (suggestion):
If/when we extend this feature to other downloaders, this code will be the same, correct? If so, move it to `ExternalFD` or `FileDownloader` whichever is suitable

Code after:
        if 'url_list' in info_dict:
            cmd += verbose_level_args
            cmd += ['--uri-selector', 'inorder', '--download-result=hide']
            url_list_file = '%s.frag.urls' % tmpfilename
            url_list = []
            for [i, url] in enumerate(info_dict['url_list']):
                tmpsegmentname = '%s_%s.frag' % (os.path.basename(tmpfilename), i)
                url_list.append('%s\n\tout=%s' % (url, tmpsegmentname))
            with open(url_list_file, 'w') as f:
                f.write('\n'.join(url_list))

            cmd += ['-i', url_list_file]
        else:
            cmd += ['--', info_dict['url']]
        return cmd


class HttpieFD(ExternalFD):
    @classmethod
    def available(cls):
        return check_executable('http', ['--version'])

    def _make_cmd(self, tmpfilename, info_dict):
        cmd = ['http', '--download', '--output', tmpfilename, info_dict['url']]

        if info_dict.get('http_headers') is not None:
            for key, val in info_dict['http_headers'].items():
                cmd += ['%s:%s' % (key, val)]
        return cmd


class FFmpegFD(ExternalFD):
    SUPPORTED_PROTOCOLS = ('http', 'https', 'ftp', 'ftps', 'm3u8', 'rtsp', 'rtmp', 'mms')

    @classmethod
    def available(cls):
        return FFmpegPostProcessor().available

    def _call_downloader(self, tmpfilename, info_dict):
        url = info_dict['url']
        ffpp = FFmpegPostProcessor(downloader=self)
        if not ffpp.available:
            self.report_error('m3u8 download detected but ffmpeg could not be found. Please install')
            return False
        ffpp.check_version()

        args = [ffpp.executable, '-y']

        for log_level in ('quiet', 'verbose'):
            if self.params.get(log_level, False):
                args += ['-loglevel', log_level]