]>
Commit | Line | Data |
---|---|---|
6800d337 | 1 | from __future__ import unicode_literals |
6800d337 | 2 | |
c43fe026 | 3 | from .fragment import FragmentFD |
e33baba0 | 4 | from ..compat import compat_urllib_error |
e06632e3 S |
5 | from ..utils import ( |
6 | DownloadError, | |
7 | urljoin, | |
8 | ) | |
453a1617 | 9 | |
6800d337 | 10 | |
c43fe026 | 11 | class DashSegmentsFD(FragmentFD): |
6800d337 YCH |
12 | """ |
13 | Download segments in a DASH manifest | |
14 | """ | |
6800d337 | 15 | |
c43fe026 | 16 | FD_NAME = 'dashsegments' |
5bf3276e | 17 | |
c43fe026 | 18 | def real_download(self, filename, info_dict): |
1141e910 S |
19 | fragment_base_url = info_dict.get('fragment_base_url') |
20 | fragments = info_dict['fragments'][:1] if self.params.get( | |
86f4d14f | 21 | 'test', False) else info_dict['fragments'] |
5bf3276e | 22 | |
c43fe026 | 23 | ctx = { |
24 | 'filename': filename, | |
1141e910 | 25 | 'total_frags': len(fragments), |
c43fe026 | 26 | } |
5bf3276e | 27 | |
c43fe026 | 28 | self._prepare_and_start_frag_download(ctx) |
6800d337 | 29 | |
e33baba0 | 30 | fragment_retries = self.params.get('fragment_retries', 0) |
25afc2a7 | 31 | skip_unavailable_fragments = self.params.get('skip_unavailable_fragments', True) |
e33baba0 | 32 | |
75a24854 | 33 | frag_index = 0 |
1141e910 | 34 | for i, fragment in enumerate(fragments): |
75a24854 | 35 | frag_index += 1 |
3e0304fe | 36 | if frag_index <= ctx['fragment_index']: |
75a24854 | 37 | continue |
86f4d14f S |
38 | # In DASH, the first segment contains necessary headers to |
39 | # generate a valid MP4 file, so always abort for the first segment | |
75a24854 | 40 | fatal = i == 0 or not skip_unavailable_fragments |
e33baba0 S |
41 | count = 0 |
42 | while count <= fragment_retries: | |
43 | try: | |
1141e910 S |
44 | fragment_url = fragment.get('url') |
45 | if not fragment_url: | |
46 | assert fragment_base_url | |
47 | fragment_url = urljoin(fragment_base_url, fragment['path']) | |
48 | success, frag_content = self._download_fragment(ctx, fragment_url, info_dict) | |
e33baba0 S |
49 | if not success: |
50 | return False | |
75a24854 | 51 | self._append_fragment(ctx, frag_content) |
e33baba0 | 52 | break |
2e99cd30 | 53 | except compat_urllib_error.HTTPError as err: |
e33baba0 S |
54 | # YouTube may often return 404 HTTP error for a fragment causing the |
55 | # whole download to fail. However if the same fragment is immediately | |
843ad179 | 56 | # retried with the same request data this usually succeeds (1-2 attempts |
e33baba0 | 57 | # is usually enough) thus allowing to download the whole file successfully. |
25afc2a7 S |
58 | # To be future-proof we will retry all fragments that fail with any |
59 | # HTTP error. | |
e33baba0 S |
60 | count += 1 |
61 | if count <= fragment_retries: | |
75a24854 | 62 | self.report_retry_fragment(err, frag_index, count, fragment_retries) |
e06632e3 S |
63 | except DownloadError: |
64 | # Don't retry fragment if error occurred during HTTP downloading | |
65 | # itself since it has own retry settings | |
66 | if not fatal: | |
67 | self.report_skip_fragment(frag_index) | |
68 | break | |
69 | raise | |
70 | ||
e33baba0 | 71 | if count > fragment_retries: |
919cf1a6 | 72 | if not fatal: |
75a24854 RA |
73 | self.report_skip_fragment(frag_index) |
74 | continue | |
e33baba0 | 75 | self.report_error('giving up after %s fragment retries' % fragment_retries) |
c43fe026 | 76 | return False |
c43fe026 | 77 | |
78 | self._finish_frag_download(ctx) | |
79 | ||
6800d337 | 80 | return True |