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 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.tracks import Audio, Subtitle, Tracks, Video
from devine.core.utilities import is_close_match
@ -416,22 +416,16 @@ class DASH:
attempts = 1
while True:
try:
downloader_ = downloader
headers_ = session.headers
if segment_range:
# aria2(c) doesn't support byte ranges, let's use python-requests (likely slower)
res = session.get(
url=segment_uri,
headers={
"Range": f"bytes={segment_range}"
}
)
res.raise_for_status()
segment_save_path.parent.mkdir(parents=True, exist_ok=True)
segment_save_path.write_bytes(res.content)
else:
downloader(
downloader_ = requests_downloader
headers_["Range"] = f"bytes={segment_range}"
downloader_(
uri=segment_uri,
out=segment_save_path,
headers=session.headers,
headers=headers_,
proxy=proxy,
silent=attempts != 5,
segmented=True

View File

@ -23,7 +23,7 @@ from requests import Session
from rich import filesize
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.tracks import Audio, Subtitle, Tracks, Video
from devine.core.utilities import is_close_match
@ -290,27 +290,19 @@ class HLS:
attempts = 1
while True:
try:
downloader_ = downloader
headers_ = session.headers
if segment.byterange:
# 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(
url=segment.uri,
headers={
"Range": f"bytes={byte_range}"
}
)
res.raise_for_status()
segment_save_path.parent.mkdir(parents=True, exist_ok=True)
segment_save_path.write_bytes(res.content)
else:
downloader(
downloader_ = requests_downloader
headers_["Range"] = f"bytes={byte_range}"
downloader_(
uri=segment.uri,
out=segment_save_path,
headers=session.headers,
headers=headers_,
proxy=proxy,
silent=attempts != 5,
segmented=True