Override the downloader more efficiently in DASH/HLS when Range is used

This commit is contained in:
rlaphoenix 2023-05-17 01:31:03 +01:00
parent 6d4be8620c
commit 6cdde3efb0
2 changed files with 26 additions and 40 deletions

View File

@ -26,7 +26,7 @@ from requests import Session
from rich import filesize from rich import filesize
from devine.core.constants import AnyTrack from devine.core.constants import AnyTrack
from devine.core.downloaders import downloader from devine.core.downloaders import downloader, requests as requests_downloader
from devine.core.drm import Widevine from devine.core.drm import Widevine
from devine.core.tracks import Audio, Subtitle, Tracks, Video from devine.core.tracks import Audio, Subtitle, Tracks, Video
from devine.core.utilities import is_close_match from devine.core.utilities import is_close_match
@ -416,26 +416,20 @@ class DASH:
attempts = 1 attempts = 1
while True: while True:
try: try:
downloader_ = downloader
headers_ = session.headers
if segment_range: if segment_range:
# aria2(c) doesn't support byte ranges, let's use python-requests (likely slower) # aria2(c) doesn't support byte ranges, let's use python-requests (likely slower)
res = session.get( downloader_ = requests_downloader
url=segment_uri, headers_["Range"] = f"bytes={segment_range}"
headers={ downloader_(
"Range": f"bytes={segment_range}" uri=segment_uri,
} out=segment_save_path,
) headers=headers_,
res.raise_for_status() proxy=proxy,
segment_save_path.parent.mkdir(parents=True, exist_ok=True) silent=attempts != 5,
segment_save_path.write_bytes(res.content) segmented=True
else: )
downloader(
uri=segment_uri,
out=segment_save_path,
headers=session.headers,
proxy=proxy,
silent=attempts != 5,
segmented=True
)
break break
except Exception as ee: except Exception as ee:
if stop_event.is_set() or attempts == 5: if stop_event.is_set() or attempts == 5:

View File

@ -23,7 +23,7 @@ from requests import Session
from rich import filesize from rich import filesize
from devine.core.constants import AnyTrack from devine.core.constants import AnyTrack
from devine.core.downloaders import downloader from devine.core.downloaders import downloader, requests as requests_downloader
from devine.core.drm import DRM_T, ClearKey, Widevine from devine.core.drm import DRM_T, ClearKey, Widevine
from devine.core.tracks import Audio, Subtitle, Tracks, Video from devine.core.tracks import Audio, Subtitle, Tracks, Video
from devine.core.utilities import is_close_match from devine.core.utilities import is_close_match
@ -290,31 +290,23 @@ class HLS:
attempts = 1 attempts = 1
while True: while True:
try: try:
downloader_ = downloader
headers_ = session.headers
if segment.byterange: if segment.byterange:
# aria2(c) doesn't support byte ranges, let's use python-requests (likely slower) # aria2(c) doesn't support byte ranges, let's use python-requests (likely slower)
previous_range_offset = range_offset.get() previous_range_offset = range_offset.get()
byte_range = HLS.calculate_byte_range(segment.byterange, previous_range_offset) byte_range = HLS.calculate_byte_range(segment.byterange, previous_range_offset)
range_offset.put(byte_range.split("-")[0]) range_offset.put(byte_range.split("-")[0])
downloader_ = requests_downloader
res = session.get( headers_["Range"] = f"bytes={byte_range}"
url=segment.uri, downloader_(
headers={ uri=segment.uri,
"Range": f"bytes={byte_range}" out=segment_save_path,
} headers=headers_,
) proxy=proxy,
res.raise_for_status() silent=attempts != 5,
segmented=True
segment_save_path.parent.mkdir(parents=True, exist_ok=True) )
segment_save_path.write_bytes(res.content)
else:
downloader(
uri=segment.uri,
out=segment_save_path,
headers=session.headers,
proxy=proxy,
silent=attempts != 5,
segmented=True
)
break break
except Exception as ee: except Exception as ee:
if stop_event.is_set() or attempts == 5: if stop_event.is_set() or attempts == 5: