]> jfr.im git - yt-dlp.git/blame - youtube_dl/downloader/dash.py
[downloader/dash:hls] Respect --fragment-retries and --skip-unavailable-fragments...
[yt-dlp.git] / youtube_dl / downloader / dash.py
CommitLineData
6800d337 1from __future__ import unicode_literals
6800d337 2
c43fe026 3import os
6800d337
YCH
4import re
5
c43fe026 6from .fragment import FragmentFD
e33baba0 7from ..compat import compat_urllib_error
c43fe026 8from ..utils import (
9 sanitize_open,
10 encodeFilename,
11)
453a1617 12
6800d337 13
c43fe026 14class DashSegmentsFD(FragmentFD):
6800d337
YCH
15 """
16 Download segments in a DASH manifest
17 """
6800d337 18
c43fe026 19 FD_NAME = 'dashsegments'
5bf3276e 20
c43fe026 21 def real_download(self, filename, info_dict):
22 base_url = info_dict['url']
23 segment_urls = [info_dict['segment_urls'][0]] if self.params.get('test', False) else info_dict['segment_urls']
24 initialization_url = info_dict.get('initialization_url')
5bf3276e 25
c43fe026 26 ctx = {
27 'filename': filename,
28 'total_frags': len(segment_urls) + (1 if initialization_url else 0),
29 }
5bf3276e 30
c43fe026 31 self._prepare_and_start_frag_download(ctx)
6800d337
YCH
32
33 def combine_url(base_url, target_url):
34 if re.match(r'^https?://', target_url):
35 return target_url
59db9f80 36 return '%s%s%s' % (base_url, '' if base_url.endswith('/') else '/', target_url)
6800d337 37
c43fe026 38 segments_filenames = []
c78c9cd1 39
e33baba0 40 fragment_retries = self.params.get('fragment_retries', 0)
25afc2a7 41 skip_unavailable_fragments = self.params.get('skip_unavailable_fragments', True)
e33baba0
S
42
43 def append_url_to_file(target_url, tmp_filename, segment_name):
44 target_filename = '%s-%s' % (tmp_filename, segment_name)
45 count = 0
46 while count <= fragment_retries:
47 try:
48 success = ctx['dl'].download(target_filename, {'url': combine_url(base_url, target_url)})
49 if not success:
50 return False
51 down, target_sanitized = sanitize_open(target_filename, 'rb')
52 ctx['dest_stream'].write(down.read())
53 down.close()
54 segments_filenames.append(target_sanitized)
55 break
25afc2a7 56 except compat_urllib_error.HTTPError:
e33baba0
S
57 # YouTube may often return 404 HTTP error for a fragment causing the
58 # whole download to fail. However if the same fragment is immediately
59 # retried with the same request data this usually succeeds (1-2 attemps
60 # is usually enough) thus allowing to download the whole file successfully.
25afc2a7
S
61 # To be future-proof we will retry all fragments that fail with any
62 # HTTP error.
e33baba0
S
63 count += 1
64 if count <= fragment_retries:
65 self.report_retry_fragment(segment_name, count, fragment_retries)
66 if count > fragment_retries:
25afc2a7
S
67 if skip_unavailable_fragments:
68 self.report_skip_fragment(segment_name)
69 return
e33baba0 70 self.report_error('giving up after %s fragment retries' % fragment_retries)
c43fe026 71 return False
c43fe026 72
73 if initialization_url:
e33baba0 74 append_url_to_file(initialization_url, ctx['tmpfilename'], 'Init')
c43fe026 75 for i, segment_url in enumerate(segment_urls):
e33baba0 76 append_url_to_file(segment_url, ctx['tmpfilename'], 'Seg%d' % i)
c43fe026 77
78 self._finish_frag_download(ctx)
79
80 for segment_file in segments_filenames:
81 os.remove(encodeFilename(segment_file))
6800d337
YCH
82
83 return True