[downloader] Pass `info_dict` to `progress_hook`s

This commit is contained in:
pukkandan 2021-07-21 22:58:43 +05:30
parent 29b208f6f9
commit 3ba7740dd8
No known key found for this signature in database
GPG Key ID: 0F00D95A001F4698
13 changed files with 36 additions and 28 deletions

View File

@ -322,6 +322,7 @@ class YoutubeDL(object):
progress, with a dictionary with the entries progress, with a dictionary with the entries
* status: One of "downloading", "error", or "finished". * status: One of "downloading", "error", or "finished".
Check this first and ignore unknown values. Check this first and ignore unknown values.
* info_dict: The extracted info_dict
If status is one of "downloading", or "finished", the If status is one of "downloading", or "finished", the
following properties may also be present: following properties may also be present:

View File

@ -1,5 +1,6 @@
from __future__ import division, unicode_literals from __future__ import division, unicode_literals
import copy
import os import os
import re import re
import sys import sys
@ -360,7 +361,7 @@ class FileDownloader(object):
'filename': filename, 'filename': filename,
'status': 'finished', 'status': 'finished',
'total_bytes': os.path.getsize(encodeFilename(filename)), 'total_bytes': os.path.getsize(encodeFilename(filename)),
}) }, info_dict)
return True, False return True, False
if subtitle is False: if subtitle is False:
@ -388,9 +389,14 @@ class FileDownloader(object):
"""Real download process. Redefine in subclasses.""" """Real download process. Redefine in subclasses."""
raise NotImplementedError('This method must be implemented by subclasses') raise NotImplementedError('This method must be implemented by subclasses')
def _hook_progress(self, status): def _hook_progress(self, status, info_dict):
if not self._progress_hooks:
return
info_dict = dict(info_dict)
for key in ('__original_infodict', '__postprocessors'):
info_dict.pop(key, None)
for ph in self._progress_hooks: for ph in self._progress_hooks:
ph(status) ph({**status, 'info_dict': copy.deepcopy(info_dict)})
def add_progress_hook(self, ph): def add_progress_hook(self, ph):
# See YoutubeDl.py (search for progress_hooks) for a description of # See YoutubeDl.py (search for progress_hooks) for a description of

View File

@ -29,7 +29,7 @@ class DashSegmentsFD(FragmentFD):
if real_downloader: if real_downloader:
self._prepare_external_frag_download(ctx) self._prepare_external_frag_download(ctx)
else: else:
self._prepare_and_start_frag_download(ctx) self._prepare_and_start_frag_download(ctx, info_dict)
fragments_to_download = [] fragments_to_download = []
frag_index = 0 frag_index = 0

View File

@ -67,7 +67,7 @@ class ExternalFD(FileDownloader):
'downloaded_bytes': fsize, 'downloaded_bytes': fsize,
'total_bytes': fsize, 'total_bytes': fsize,
}) })
self._hook_progress(status) self._hook_progress(status, info_dict)
return True return True
else: else:
self.to_stderr('\n') self.to_stderr('\n')

View File

@ -380,7 +380,7 @@ class F4mFD(FragmentFD):
base_url_parsed = compat_urllib_parse_urlparse(base_url) base_url_parsed = compat_urllib_parse_urlparse(base_url)
self._start_frag_download(ctx) self._start_frag_download(ctx, info_dict)
frag_index = 0 frag_index = 0
while fragments_list: while fragments_list:
@ -434,6 +434,6 @@ class F4mFD(FragmentFD):
msg = 'Missed %d fragments' % (fragments_list[0][1] - (frag_i + 1)) msg = 'Missed %d fragments' % (fragments_list[0][1] - (frag_i + 1))
self.report_warning(msg) self.report_warning(msg)
self._finish_frag_download(ctx) self._finish_frag_download(ctx, info_dict)
return True return True

View File

@ -83,9 +83,9 @@ class FragmentFD(FileDownloader):
headers = info_dict.get('http_headers') headers = info_dict.get('http_headers')
return sanitized_Request(url, None, headers) if headers else url return sanitized_Request(url, None, headers) if headers else url
def _prepare_and_start_frag_download(self, ctx): def _prepare_and_start_frag_download(self, ctx, info_dict):
self._prepare_frag_download(ctx) self._prepare_frag_download(ctx)
self._start_frag_download(ctx) self._start_frag_download(ctx, info_dict)
def __do_ytdl_file(self, ctx): def __do_ytdl_file(self, ctx):
return not ctx['live'] and not ctx['tmpfilename'] == '-' and not self.params.get('_no_ytdl_file') return not ctx['live'] and not ctx['tmpfilename'] == '-' and not self.params.get('_no_ytdl_file')
@ -219,7 +219,7 @@ class FragmentFD(FileDownloader):
'complete_frags_downloaded_bytes': resume_len, 'complete_frags_downloaded_bytes': resume_len,
}) })
def _start_frag_download(self, ctx): def _start_frag_download(self, ctx, info_dict):
resume_len = ctx['complete_frags_downloaded_bytes'] resume_len = ctx['complete_frags_downloaded_bytes']
total_frags = ctx['total_frags'] total_frags = ctx['total_frags']
# This dict stores the download progress, it's updated by the progress # This dict stores the download progress, it's updated by the progress
@ -248,6 +248,7 @@ class FragmentFD(FileDownloader):
time_now = time.time() time_now = time.time()
state['elapsed'] = time_now - start state['elapsed'] = time_now - start
frag_total_bytes = s.get('total_bytes') or 0 frag_total_bytes = s.get('total_bytes') or 0
s['fragment_info_dict'] = s.pop('info_dict', {})
if not ctx['live']: if not ctx['live']:
estimated_size = ( estimated_size = (
(ctx['complete_frags_downloaded_bytes'] + frag_total_bytes) (ctx['complete_frags_downloaded_bytes'] + frag_total_bytes)
@ -270,13 +271,13 @@ class FragmentFD(FileDownloader):
state['speed'] = s.get('speed') or ctx.get('speed') state['speed'] = s.get('speed') or ctx.get('speed')
ctx['speed'] = state['speed'] ctx['speed'] = state['speed']
ctx['prev_frag_downloaded_bytes'] = frag_downloaded_bytes ctx['prev_frag_downloaded_bytes'] = frag_downloaded_bytes
self._hook_progress(state) self._hook_progress(state, info_dict)
ctx['dl'].add_progress_hook(frag_progress_hook) ctx['dl'].add_progress_hook(frag_progress_hook)
return start return start
def _finish_frag_download(self, ctx): def _finish_frag_download(self, ctx, info_dict):
ctx['dest_stream'].close() ctx['dest_stream'].close()
if self.__do_ytdl_file(ctx): if self.__do_ytdl_file(ctx):
ytdl_filename = encodeFilename(self.ytdl_filename(ctx['filename'])) ytdl_filename = encodeFilename(self.ytdl_filename(ctx['filename']))
@ -303,7 +304,7 @@ class FragmentFD(FileDownloader):
'filename': ctx['filename'], 'filename': ctx['filename'],
'status': 'finished', 'status': 'finished',
'elapsed': elapsed, 'elapsed': elapsed,
}) }, info_dict)
def _prepare_external_frag_download(self, ctx): def _prepare_external_frag_download(self, ctx):
if 'live' not in ctx: if 'live' not in ctx:
@ -421,5 +422,5 @@ class FragmentFD(FileDownloader):
if not result: if not result:
return False return False
self._finish_frag_download(ctx) self._finish_frag_download(ctx, info_dict)
return True return True

View File

@ -133,7 +133,7 @@ class HlsFD(FragmentFD):
if real_downloader: if real_downloader:
self._prepare_external_frag_download(ctx) self._prepare_external_frag_download(ctx)
else: else:
self._prepare_and_start_frag_download(ctx) self._prepare_and_start_frag_download(ctx, info_dict)
extra_state = ctx.setdefault('extra_state', {}) extra_state = ctx.setdefault('extra_state', {})

View File

@ -177,7 +177,7 @@ class HttpFD(FileDownloader):
'status': 'finished', 'status': 'finished',
'downloaded_bytes': ctx.resume_len, 'downloaded_bytes': ctx.resume_len,
'total_bytes': ctx.resume_len, 'total_bytes': ctx.resume_len,
}) }, info_dict)
raise SucceedDownload() raise SucceedDownload()
else: else:
# The length does not match, we start the download over # The length does not match, we start the download over
@ -310,7 +310,7 @@ class HttpFD(FileDownloader):
'eta': eta, 'eta': eta,
'speed': speed, 'speed': speed,
'elapsed': now - ctx.start_time, 'elapsed': now - ctx.start_time,
}) }, info_dict)
if data_len is not None and byte_counter == data_len: if data_len is not None and byte_counter == data_len:
break break
@ -357,7 +357,7 @@ class HttpFD(FileDownloader):
'filename': ctx.filename, 'filename': ctx.filename,
'status': 'finished', 'status': 'finished',
'elapsed': time.time() - ctx.start_time, 'elapsed': time.time() - ctx.start_time,
}) }, info_dict)
return True return True

View File

@ -246,7 +246,7 @@ class IsmFD(FragmentFD):
'total_frags': len(segments), 'total_frags': len(segments),
} }
self._prepare_and_start_frag_download(ctx) self._prepare_and_start_frag_download(ctx, info_dict)
extra_state = ctx.setdefault('extra_state', { extra_state = ctx.setdefault('extra_state', {
'ism_track_written': False, 'ism_track_written': False,
@ -284,6 +284,6 @@ class IsmFD(FragmentFD):
self.report_error('giving up after %s fragment retries' % fragment_retries) self.report_error('giving up after %s fragment retries' % fragment_retries)
return False return False
self._finish_frag_download(ctx) self._finish_frag_download(ctx, info_dict)
return True return True

View File

@ -122,7 +122,7 @@ body > figure > img {
'total_frags': len(fragments), 'total_frags': len(fragments),
} }
self._prepare_and_start_frag_download(ctx) self._prepare_and_start_frag_download(ctx, info_dict)
extra_state = ctx.setdefault('extra_state', { extra_state = ctx.setdefault('extra_state', {
'header_written': False, 'header_written': False,
@ -198,5 +198,5 @@ body > figure > img {
ctx['dest_stream'].write( ctx['dest_stream'].write(
b'--%b--\r\n\r\n' % frag_boundary.encode('us-ascii')) b'--%b--\r\n\r\n' % frag_boundary.encode('us-ascii'))
self._finish_frag_download(ctx) self._finish_frag_download(ctx, info_dict)
return True return True

View File

@ -66,7 +66,7 @@ class RtmpFD(FileDownloader):
'eta': eta, 'eta': eta,
'elapsed': time_now - start, 'elapsed': time_now - start,
'speed': speed, 'speed': speed,
}) }, info_dict)
cursor_in_new_line = False cursor_in_new_line = False
else: else:
# no percent for live streams # no percent for live streams
@ -82,7 +82,7 @@ class RtmpFD(FileDownloader):
'status': 'downloading', 'status': 'downloading',
'elapsed': time_now - start, 'elapsed': time_now - start,
'speed': speed, 'speed': speed,
}) }, info_dict)
cursor_in_new_line = False cursor_in_new_line = False
elif self.params.get('verbose', False): elif self.params.get('verbose', False):
if not cursor_in_new_line: if not cursor_in_new_line:
@ -208,7 +208,7 @@ class RtmpFD(FileDownloader):
'filename': filename, 'filename': filename,
'status': 'finished', 'status': 'finished',
'elapsed': time.time() - started, 'elapsed': time.time() - started,
}) }, info_dict)
return True return True
else: else:
self.to_stderr('\n') self.to_stderr('\n')

View File

@ -39,7 +39,7 @@ class RtspFD(FileDownloader):
'total_bytes': fsize, 'total_bytes': fsize,
'filename': filename, 'filename': filename,
'status': 'finished', 'status': 'finished',
}) }, info_dict)
return True return True
else: else:
self.to_stderr('\n') self.to_stderr('\n')

View File

@ -140,7 +140,7 @@ class YoutubeLiveChatFD(FragmentFD):
self.report_error('giving up after %s fragment retries' % fragment_retries) self.report_error('giving up after %s fragment retries' % fragment_retries)
return False, None, None, None return False, None, None, None
self._prepare_and_start_frag_download(ctx) self._prepare_and_start_frag_download(ctx, info_dict)
success, raw_fragment = dl_fragment(info_dict['url']) success, raw_fragment = dl_fragment(info_dict['url'])
if not success: if not success:
@ -196,7 +196,7 @@ class YoutubeLiveChatFD(FragmentFD):
if test: if test:
break break
self._finish_frag_download(ctx) self._finish_frag_download(ctx, info_dict)
return True return True
@staticmethod @staticmethod