mirror of https://github.com/yt-dlp/yt-dlp.git
[aria2c] Support retry/abort unavailable fragments (#173)
Co-authored by: Damiano Amatruda <damiano.amatruda@outlook.com>
This commit is contained in:
parent
2b3bf01c90
commit
fe845284c4
|
@ -24,7 +24,6 @@ from ..utils import (
|
||||||
cli_bool_option,
|
cli_bool_option,
|
||||||
cli_configuration_args,
|
cli_configuration_args,
|
||||||
encodeFilename,
|
encodeFilename,
|
||||||
error_to_compat_str,
|
|
||||||
encodeArgument,
|
encodeArgument,
|
||||||
handle_youtubedl_headers,
|
handle_youtubedl_headers,
|
||||||
check_executable,
|
check_executable,
|
||||||
|
@ -117,19 +116,42 @@ class ExternalFD(FileDownloader):
|
||||||
|
|
||||||
self._debug_cmd(cmd)
|
self._debug_cmd(cmd)
|
||||||
|
|
||||||
|
if 'fragments' in info_dict:
|
||||||
|
fragment_retries = self.params.get('fragment_retries', 0)
|
||||||
|
skip_unavailable_fragments = self.params.get('skip_unavailable_fragments', True)
|
||||||
|
|
||||||
|
count = 0
|
||||||
|
while count <= fragment_retries:
|
||||||
p = subprocess.Popen(
|
p = subprocess.Popen(
|
||||||
cmd, stderr=subprocess.PIPE)
|
cmd, stderr=subprocess.PIPE)
|
||||||
_, stderr = process_communicate_or_kill(p)
|
_, stderr = process_communicate_or_kill(p)
|
||||||
if p.returncode != 0:
|
if p.returncode == 0:
|
||||||
|
break
|
||||||
|
# TODO: Decide whether to retry based on error code
|
||||||
|
# https://aria2.github.io/manual/en/html/aria2c.html#exit-status
|
||||||
self.to_stderr(stderr.decode('utf-8', 'replace'))
|
self.to_stderr(stderr.decode('utf-8', 'replace'))
|
||||||
|
count += 1
|
||||||
|
if count <= fragment_retries:
|
||||||
|
self.to_screen(
|
||||||
|
'[%s] Got error. Retrying fragments (attempt %d of %s)...'
|
||||||
|
% (self.get_basename(), count, self.format_retries(fragment_retries)))
|
||||||
|
if count > fragment_retries:
|
||||||
|
if not skip_unavailable_fragments:
|
||||||
|
self.report_error('Giving up after %s fragment retries' % fragment_retries)
|
||||||
|
return -1
|
||||||
|
|
||||||
if 'fragments' in info_dict:
|
|
||||||
file_list = []
|
|
||||||
dest, _ = sanitize_open(tmpfilename, 'wb')
|
dest, _ = sanitize_open(tmpfilename, 'wb')
|
||||||
for i, fragment in enumerate(info_dict['fragments']):
|
for frag_index, fragment in enumerate(info_dict['fragments']):
|
||||||
file = '%s-Frag%d' % (tmpfilename, i)
|
fragment_filename = '%s-Frag%d' % (tmpfilename, frag_index)
|
||||||
|
try:
|
||||||
|
src, _ = sanitize_open(fragment_filename, 'rb')
|
||||||
|
except IOError:
|
||||||
|
if skip_unavailable_fragments and frag_index > 1:
|
||||||
|
self.to_screen('[%s] Skipping fragment %d ...' % (self.get_basename(), frag_index))
|
||||||
|
continue
|
||||||
|
self.report_error('Unable to open fragment %d' % frag_index)
|
||||||
|
return -1
|
||||||
decrypt_info = fragment.get('decrypt_info')
|
decrypt_info = fragment.get('decrypt_info')
|
||||||
src, _ = sanitize_open(file, 'rb')
|
|
||||||
if decrypt_info:
|
if decrypt_info:
|
||||||
if decrypt_info['METHOD'] == 'AES-128':
|
if decrypt_info['METHOD'] == 'AES-128':
|
||||||
iv = decrypt_info.get('IV')
|
iv = decrypt_info.get('IV')
|
||||||
|
@ -146,20 +168,16 @@ class ExternalFD(FileDownloader):
|
||||||
fragment_data = src.read()
|
fragment_data = src.read()
|
||||||
dest.write(fragment_data)
|
dest.write(fragment_data)
|
||||||
src.close()
|
src.close()
|
||||||
file_list.append(file)
|
|
||||||
dest.close()
|
|
||||||
if not self.params.get('keep_fragments', False):
|
if not self.params.get('keep_fragments', False):
|
||||||
for file_path in file_list:
|
os.remove(encodeFilename(fragment_filename))
|
||||||
try:
|
dest.close()
|
||||||
os.remove(file_path)
|
os.remove(encodeFilename('%s.frag.urls' % tmpfilename))
|
||||||
except OSError as ose:
|
else:
|
||||||
self.report_error("Unable to delete file %s; %s" % (file_path, error_to_compat_str(ose)))
|
p = subprocess.Popen(
|
||||||
try:
|
cmd, stderr=subprocess.PIPE)
|
||||||
file_path = '%s.frag.urls' % tmpfilename
|
_, stderr = process_communicate_or_kill(p)
|
||||||
os.remove(file_path)
|
if p.returncode != 0:
|
||||||
except OSError as ose:
|
self.to_stderr(stderr.decode('utf-8', 'replace'))
|
||||||
self.report_error("Unable to delete file %s; %s" % (file_path, error_to_compat_str(ose)))
|
|
||||||
|
|
||||||
return p.returncode
|
return p.returncode
|
||||||
|
|
||||||
def _prepare_url(self, info_dict, url):
|
def _prepare_url(self, info_dict, url):
|
||||||
|
@ -276,17 +294,15 @@ class Aria2cFD(ExternalFD):
|
||||||
cmd += ['--auto-file-renaming=false']
|
cmd += ['--auto-file-renaming=false']
|
||||||
|
|
||||||
if 'fragments' in info_dict:
|
if 'fragments' in info_dict:
|
||||||
cmd += verbose_level_args
|
cmd += ['--file-allocation=none', '--uri-selector=inorder']
|
||||||
cmd += ['--uri-selector', 'inorder', '--download-result=hide']
|
|
||||||
url_list_file = '%s.frag.urls' % tmpfilename
|
url_list_file = '%s.frag.urls' % tmpfilename
|
||||||
url_list = []
|
url_list = []
|
||||||
for i, fragment in enumerate(info_dict['fragments']):
|
for frag_index, fragment in enumerate(info_dict['fragments']):
|
||||||
tmpsegmentname = '%s-Frag%d' % (os.path.basename(tmpfilename), i)
|
fragment_filename = '%s-Frag%d' % (os.path.basename(tmpfilename), frag_index)
|
||||||
url_list.append('%s\n\tout=%s' % (fragment['url'], tmpsegmentname))
|
url_list.append('%s\n\tout=%s' % (fragment['url'], fragment_filename))
|
||||||
stream, _ = sanitize_open(url_list_file, 'wb')
|
stream, _ = sanitize_open(url_list_file, 'wb')
|
||||||
stream.write('\n'.join(url_list).encode('utf-8'))
|
stream.write('\n'.join(url_list).encode('utf-8'))
|
||||||
stream.close()
|
stream.close()
|
||||||
|
|
||||||
cmd += ['-i', url_list_file]
|
cmd += ['-i', url_list_file]
|
||||||
else:
|
else:
|
||||||
cmd += ['--', info_dict['url']]
|
cmd += ['--', info_dict['url']]
|
||||||
|
|
Loading…
Reference in New Issue