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