]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/downloader/dash.py
Prepare to upload.
[youtubedl] / youtube_dl / downloader / dash.py
1 from __future__ import unicode_literals
2
3 import os
4 import re
5
6 from .fragment import FragmentFD
7 from ..utils import (
8 sanitize_open,
9 encodeFilename,
10 )
11
12
13 class DashSegmentsFD(FragmentFD):
14 """
15 Download segments in a DASH manifest
16 """
17
18 FD_NAME = 'dashsegments'
19
20 def real_download(self, filename, info_dict):
21 base_url = info_dict['url']
22 segment_urls = [info_dict['segment_urls'][0]] if self.params.get('test', False) else info_dict['segment_urls']
23 initialization_url = info_dict.get('initialization_url')
24
25 ctx = {
26 'filename': filename,
27 'total_frags': len(segment_urls) + (1 if initialization_url else 0),
28 }
29
30 self._prepare_and_start_frag_download(ctx)
31
32 def combine_url(base_url, target_url):
33 if re.match(r'^https?://', target_url):
34 return target_url
35 return '%s%s%s' % (base_url, '' if base_url.endswith('/') else '/', target_url)
36
37 segments_filenames = []
38
39 def append_url_to_file(target_url, target_filename):
40 success = ctx['dl'].download(target_filename, {'url': combine_url(base_url, target_url)})
41 if not success:
42 return False
43 down, target_sanitized = sanitize_open(target_filename, 'rb')
44 ctx['dest_stream'].write(down.read())
45 down.close()
46 segments_filenames.append(target_sanitized)
47
48 if initialization_url:
49 append_url_to_file(initialization_url, ctx['tmpfilename'] + '-Init')
50 for i, segment_url in enumerate(segment_urls):
51 segment_filename = '%s-Seg%d' % (ctx['tmpfilename'], i)
52 append_url_to_file(segment_url, segment_filename)
53
54 self._finish_frag_download(ctx)
55
56 for segment_file in segments_filenames:
57 os.remove(encodeFilename(segment_file))
58
59 return True