mirror of https://github.com/yt-dlp/yt-dlp
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
3.1 KiB
Python
81 lines
3.1 KiB
Python
10 years ago
|
from __future__ import unicode_literals
|
||
|
|
||
9 years ago
|
from .fragment import FragmentFD
|
||
9 years ago
|
from ..compat import compat_urllib_error
|
||
6 years ago
|
from ..utils import (
|
||
|
DownloadError,
|
||
|
urljoin,
|
||
|
)
|
||
10 years ago
|
|
||
10 years ago
|
|
||
9 years ago
|
class DashSegmentsFD(FragmentFD):
|
||
10 years ago
|
"""
|
||
|
Download segments in a DASH manifest
|
||
|
"""
|
||
|
|
||
9 years ago
|
FD_NAME = 'dashsegments'
|
||
10 years ago
|
|
||
9 years ago
|
def real_download(self, filename, info_dict):
|
||
7 years ago
|
fragment_base_url = info_dict.get('fragment_base_url')
|
||
|
fragments = info_dict['fragments'][:1] if self.params.get(
|
||
8 years ago
|
'test', False) else info_dict['fragments']
|
||
10 years ago
|
|
||
9 years ago
|
ctx = {
|
||
|
'filename': filename,
|
||
7 years ago
|
'total_frags': len(fragments),
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
self._prepare_and_start_frag_download(ctx)
|
||
10 years ago
|
|
||
9 years ago
|
fragment_retries = self.params.get('fragment_retries', 0)
|
||
8 years ago
|
skip_unavailable_fragments = self.params.get('skip_unavailable_fragments', True)
|
||
9 years ago
|
|
||
9 years ago
|
frag_index = 0
|
||
7 years ago
|
for i, fragment in enumerate(fragments):
|
||
9 years ago
|
frag_index += 1
|
||
8 years ago
|
if frag_index <= ctx['fragment_index']:
|
||
9 years ago
|
continue
|
||
8 years ago
|
# In DASH, the first segment contains necessary headers to
|
||
|
# generate a valid MP4 file, so always abort for the first segment
|
||
9 years ago
|
fatal = i == 0 or not skip_unavailable_fragments
|
||
9 years ago
|
count = 0
|
||
|
while count <= fragment_retries:
|
||
|
try:
|
||
7 years ago
|
fragment_url = fragment.get('url')
|
||
|
if not fragment_url:
|
||
|
assert fragment_base_url
|
||
|
fragment_url = urljoin(fragment_base_url, fragment['path'])
|
||
|
success, frag_content = self._download_fragment(ctx, fragment_url, info_dict)
|
||
9 years ago
|
if not success:
|
||
|
return False
|
||
9 years ago
|
self._append_fragment(ctx, frag_content)
|
||
9 years ago
|
break
|
||
8 years ago
|
except compat_urllib_error.HTTPError as err:
|
||
9 years ago
|
# YouTube may often return 404 HTTP error for a fragment causing the
|
||
|
# whole download to fail. However if the same fragment is immediately
|
||
5 years ago
|
# retried with the same request data this usually succeeds (1-2 attempts
|
||
9 years ago
|
# is usually enough) thus allowing to download the whole file successfully.
|
||
8 years ago
|
# To be future-proof we will retry all fragments that fail with any
|
||
|
# HTTP error.
|
||
9 years ago
|
count += 1
|
||
|
if count <= fragment_retries:
|
||
9 years ago
|
self.report_retry_fragment(err, frag_index, count, fragment_retries)
|
||
6 years ago
|
except DownloadError:
|
||
|
# Don't retry fragment if error occurred during HTTP downloading
|
||
|
# itself since it has own retry settings
|
||
|
if not fatal:
|
||
|
self.report_skip_fragment(frag_index)
|
||
|
break
|
||
|
raise
|
||
|
|
||
9 years ago
|
if count > fragment_retries:
|
||
8 years ago
|
if not fatal:
|
||
9 years ago
|
self.report_skip_fragment(frag_index)
|
||
|
continue
|
||
9 years ago
|
self.report_error('giving up after %s fragment retries' % fragment_retries)
|
||
9 years ago
|
return False
|
||
|
|
||
|
self._finish_frag_download(ctx)
|
||
|
|
||
10 years ago
|
return True
|