Add a 5-attempt retry system to DASH & HLS downloads

This commit is contained in:
rlaphoenix 2023-03-11 19:26:51 +00:00
parent 111dac9264
commit 055bc927f5
2 changed files with 59 additions and 41 deletions

View File

@ -455,25 +455,34 @@ class DASH:
segment_uri, segment_range = segment segment_uri, segment_range = segment
if segment_range: attempts = 1
# aria2(c) doesn't support byte ranges, let's use python-requests (likely slower) while True:
r = session.get( try:
url=segment_uri, if segment_range:
headers={ # aria2(c) doesn't support byte ranges, let's use python-requests (likely slower)
"Range": f"bytes={segment_range}" r = session.get(
} url=segment_uri,
) headers={
r.raise_for_status() "Range": f"bytes={segment_range}"
segment_save_path.parent.mkdir(parents=True, exist_ok=True) }
segment_save_path.write_bytes(res.content) )
else: r.raise_for_status()
asyncio.run(aria2c( segment_save_path.parent.mkdir(parents=True, exist_ok=True)
uri=segment_uri, segment_save_path.write_bytes(res.content)
out=segment_save_path, else:
headers=session.headers, asyncio.run(aria2c(
proxy=proxy, uri=segment_uri,
segmented=True out=segment_save_path,
)) headers=session.headers,
proxy=proxy,
segmented=True
))
break
except Exception as ee:
if stop_event.is_set() or attempts == 5:
raise ee
time.sleep(2)
attempts += 1
data_size = segment_save_path.stat().st_size data_size = segment_save_path.stat().st_size

View File

@ -283,30 +283,39 @@ class HLS:
if not segment.uri.startswith(segment.base_uri): if not segment.uri.startswith(segment.base_uri):
segment.uri = segment.base_uri + segment.uri segment.uri = segment.base_uri + segment.uri
if segment.byterange: attempts = 1
# aria2(c) doesn't support byte ranges, let's use python-requests (likely slower) while True:
previous_range_offset = range_offset.get() try:
byte_range = HLS.calculate_byte_range(segment.byterange, previous_range_offset) if segment.byterange:
range_offset.put(byte_range.split("-")[0]) # aria2(c) doesn't support byte ranges, let's use python-requests (likely slower)
previous_range_offset = range_offset.get()
byte_range = HLS.calculate_byte_range(segment.byterange, previous_range_offset)
range_offset.put(byte_range.split("-")[0])
res = session.get( res = session.get(
url=segment.uri, url=segment.uri,
headers={ headers={
"Range": f"bytes={byte_range}" "Range": f"bytes={byte_range}"
} }
) )
res.raise_for_status() res.raise_for_status()
segment_save_path.parent.mkdir(parents=True, exist_ok=True) segment_save_path.parent.mkdir(parents=True, exist_ok=True)
segment_save_path.write_bytes(res.content) segment_save_path.write_bytes(res.content)
else: else:
asyncio.run(aria2c( asyncio.run(aria2c(
uri=segment.uri, uri=segment.uri,
out=segment_save_path, out=segment_save_path,
headers=session.headers, headers=session.headers,
proxy=proxy, proxy=proxy,
segmented=True segmented=True
)) ))
break
except Exception as ee:
if stop_event.is_set() or attempts == 5:
raise ee
time.sleep(2)
attempts += 1
data_size = segment_save_path.stat().st_size data_size = segment_save_path.stat().st_size