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

Code before:
            for [i, url] in enumerate(info_dict['url_list']):
                tmpsegmentname = '%s_%s.fragment' % (tn, i)
                file_list.append(tmpsegmentname)
            info_dict['file_list'] = file_list

        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:
            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']]


Review comment (suggestion):
We need to handle what should happen if the file already exists or we get an `OSError`

Code after:
        cmd = [self.exe, '-c']
        dn = os.path.dirname(tmpfilename)
        if 'url_list' not in info_dict:
            cmd += ['--out', os.path.basename(tmpfilename)]
        verbose_level_args = ['--console-log-level=warn', '--summary-interval=0']
        cmd += self._configuration_args(['--file-allocation=none', '-x16', '-j16', '-s16'] + verbose_level_args)
        if dn:
            cmd += ['--dir', dn]
        if info_dict.get('http_headers') is not None:
            for key, val in info_dict['http_headers'].items():
                cmd += ['--header', '%s: %s' % (key, val)]
        cmd += self._option('--interface', 'source_address')
        cmd += self._option('--all-proxy', 'proxy')
        cmd += self._bool_option('--check-certificate', 'nocheckcertificate', 'false', 'true', '=')
        cmd += self._bool_option('--remote-time', 'updatetime', 'true', 'false', '=')
        cmd += ['--auto-file-renaming=false']
        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