]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/downloader/hls.py
Update changelog.
[youtubedl] / youtube_dl / downloader / hls.py
1 from __future__ import unicode_literals
2
3 import os.path
4 import re
5 import binascii
6 try:
7 from Crypto.Cipher import AES
8 can_decrypt_frag = True
9 except ImportError:
10 can_decrypt_frag = False
11
12 from .fragment import FragmentFD
13 from .external import FFmpegFD
14
15 from ..compat import (
16 compat_urlparse,
17 compat_struct_pack,
18 )
19 from ..utils import (
20 encodeFilename,
21 sanitize_open,
22 parse_m3u8_attributes,
23 )
24
25
26 class HlsFD(FragmentFD):
27 """ A limited implementation that does not require ffmpeg """
28
29 FD_NAME = 'hlsnative'
30
31 @staticmethod
32 def can_download(manifest):
33 UNSUPPORTED_FEATURES = (
34 r'#EXT-X-KEY:METHOD=(?!NONE|AES-128)', # encrypted streams [1]
35 r'#EXT-X-BYTERANGE', # playlists composed of byte ranges of media files [2]
36
37 # Live streams heuristic does not always work (e.g. geo restricted to Germany
38 # http://hls-geo.daserste.de/i/videoportal/Film/c_620000/622873/format,716451,716457,716450,716458,716459,.mp4.csmil/index_4_av.m3u8?null=0)
39 # r'#EXT-X-MEDIA-SEQUENCE:(?!0$)', # live streams [3]
40
41 # This heuristic also is not correct since segments may not be appended as well.
42 # Twitch vods of finished streams have EXT-X-PLAYLIST-TYPE:EVENT despite
43 # no segments will definitely be appended to the end of the playlist.
44 # r'#EXT-X-PLAYLIST-TYPE:EVENT', # media segments may be appended to the end of
45 # # event media playlists [4]
46
47 # 1. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.4
48 # 2. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.2
49 # 3. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.3.2
50 # 4. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.3.5
51 )
52 check_results = [not re.search(feature, manifest) for feature in UNSUPPORTED_FEATURES]
53 check_results.append(can_decrypt_frag or '#EXT-X-KEY:METHOD=AES-128' not in manifest)
54 return all(check_results)
55
56 def real_download(self, filename, info_dict):
57 man_url = info_dict['url']
58 self.to_screen('[%s] Downloading m3u8 manifest' % self.FD_NAME)
59 manifest = self.ydl.urlopen(man_url).read()
60
61 s = manifest.decode('utf-8', 'ignore')
62
63 if not self.can_download(s):
64 self.report_warning(
65 'hlsnative has detected features it does not support, '
66 'extraction will be delegated to ffmpeg')
67 fd = FFmpegFD(self.ydl, self.params)
68 for ph in self._progress_hooks:
69 fd.add_progress_hook(ph)
70 return fd.real_download(filename, info_dict)
71
72 total_frags = 0
73 for line in s.splitlines():
74 line = line.strip()
75 if line and not line.startswith('#'):
76 total_frags += 1
77
78 ctx = {
79 'filename': filename,
80 'total_frags': total_frags,
81 }
82
83 self._prepare_and_start_frag_download(ctx)
84
85 i = 0
86 media_sequence = 0
87 decrypt_info = {'METHOD': 'NONE'}
88 frags_filenames = []
89 for line in s.splitlines():
90 line = line.strip()
91 if line:
92 if not line.startswith('#'):
93 frag_url = (
94 line
95 if re.match(r'^https?://', line)
96 else compat_urlparse.urljoin(man_url, line))
97 frag_filename = '%s-Frag%d' % (ctx['tmpfilename'], i)
98 success = ctx['dl'].download(frag_filename, {'url': frag_url})
99 if not success:
100 return False
101 down, frag_sanitized = sanitize_open(frag_filename, 'rb')
102 frag_content = down.read()
103 down.close()
104 if decrypt_info['METHOD'] == 'AES-128':
105 iv = decrypt_info.get('IV') or compat_struct_pack('>8xq', media_sequence)
106 frag_content = AES.new(
107 decrypt_info['KEY'], AES.MODE_CBC, iv).decrypt(frag_content)
108 ctx['dest_stream'].write(frag_content)
109 frags_filenames.append(frag_sanitized)
110 # We only download the first fragment during the test
111 if self.params.get('test', False):
112 break
113 i += 1
114 media_sequence += 1
115 elif line.startswith('#EXT-X-KEY'):
116 decrypt_info = parse_m3u8_attributes(line[11:])
117 if decrypt_info['METHOD'] == 'AES-128':
118 if 'IV' in decrypt_info:
119 decrypt_info['IV'] = binascii.unhexlify(decrypt_info['IV'][2:])
120 if not re.match(r'^https?://', decrypt_info['URI']):
121 decrypt_info['URI'] = compat_urlparse.urljoin(
122 man_url, decrypt_info['URI'])
123 decrypt_info['KEY'] = self.ydl.urlopen(decrypt_info['URI']).read()
124 elif line.startswith('#EXT-X-MEDIA-SEQUENCE'):
125 media_sequence = int(line[22:])
126
127 self._finish_frag_download(ctx)
128
129 for frag_file in frags_filenames:
130 os.remove(encodeFilename(frag_file))
131
132 return True