def _prepare_frag_download(self, ctx):
    if ('live' not in ctx):
        ctx['live'] = False
    if (not ctx['live']):
        total_frags_str = ('%d' % ctx['total_frags'])
        ad_frags = ctx.get('ad_frags', 0)
        if ad_frags:
            total_frags_str += (' (not including %d ad)' % ad_frags)
    else:
        total_frags_str = 'unknown (live)'
    self.to_screen(f'[{self.FD_NAME}] Total fragments: {total_frags_str}')
    self.report_destination(ctx['filename'])
    dl = HttpQuietDownloader(self.ydl, {**self.params, 'noprogress': True, 'test': False})
    tmpfilename = self.temp_name(ctx['filename'])
    open_mode = 'wb'
    resume_len = 0
    if os.path.isfile(encodeFilename(tmpfilename)):
        open_mode = 'ab'
        resume_len = os.path.getsize(encodeFilename(tmpfilename))
    ctx.update({'tmpfilename': tmpfilename, 'fragment_index': 0})
    if self.__do_ytdl_file(ctx):
        if os.path.isfile(encodeFilename(self.ytdl_filename(ctx['filename']))):
            self._read_ytdl_file(ctx)
            is_corrupt = (ctx.get('ytdl_corrupt') is True)
            is_inconsistent = ((ctx['fragment_index'] > 0) and (resume_len == 0))
            if (is_corrupt or is_inconsistent):
                message = ('.ytdl file is corrupt' if is_corrupt else 'Inconsistent state of incomplete fragment download')
                self.report_warning(('%s. Restarting from the beginning ...' % message))
                ctx['fragment_index'] = resume_len = 0
                if ('ytdl_corrupt' in ctx):
                    del ctx['ytdl_corrupt']
                self._write_ytdl_file(ctx)
        else:
            self._write_ytdl_file(ctx)
            assert (ctx['fragment_index'] == 0)
    (dest_stream, tmpfilename) = self.sanitize_open(tmpfilename, open_mode)
    ctx.update({'dl': dl, 'dest_stream': dest_stream, 'tmpfilename': tmpfilename, 'complete_frags_downloaded_bytes': resume_len})
def _prepare_frag_download(self, ctx):
    if ('live' not in ctx):
        ctx['live'] = False
    if (not ctx['live']):
        total_frags_str = ('%d' % ctx['total_frags'])
        ad_frags = ctx.get('ad_frags', 0)
        if ad_frags:
            total_frags_str += (' (not including %d ad)' % ad_frags)
    else:
        total_frags_str = 'unknown (live)'
    self.to_screen(f'[{self.FD_NAME}] Total fragments: {total_frags_str}')
    self.report_destination(ctx['filename'])
    dl = HttpQuietDownloader(self.ydl, {**self.params, 'noprogress': True, 'test': False})
    tmpfilename = self.temp_name(ctx['filename'])
    open_mode = 'wb'
    resume_len = 0
    if os.path.isfile(encodeFilename(tmpfilename)):
        open_mode = 'ab'
        resume_len = os.path.getsize(encodeFilename(tmpfilename))
    ctx.update({'tmpfilename': tmpfilename, 'fragment_index': 0})
    if self.__do_ytdl_file(ctx):
        if os.path.isfile(encodeFilename(self.ytdl_filename(ctx['filename']))):
            self._read_ytdl_file(ctx)
            is_corrupt = (ctx.get('ytdl_corrupt') is True)
            is_inconsistent = ((ctx['fragment_index'] > 0) and (resume_len == 0))
            if (is_corrupt or is_inconsistent):
                message = ('.ytdl file is corrupt' if is_corrupt else 'Inconsistent state of incomplete fragment download')
                self.report_warning(('%s. Restarting from the beginning ...' % message))
                ctx['fragment_index'] = resume_len = 0
                if ('ytdl_corrupt' in ctx):
                    del ctx['ytdl_corrupt']
                self._write_ytdl_file(ctx)
        else:
            self._write_ytdl_file(ctx)
            assert (ctx['fragment_index'] == 0)
    (dest_stream, tmpfilename) = self.sanitize_open(tmpfilename, open_mode)
    ctx.update({'dl': dl, 'dest_stream': dest_stream, 'tmpfilename': tmpfilename, 'complete_frags_downloaded_bytes': resume_len})