You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

354 lines
12 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. from __future__ import unicode_literals
  2. import base64
  3. import io
  4. import itertools
  5. import os
  6. import time
  7. import xml.etree.ElementTree as etree
  8. from .common import FileDownloader
  9. from .http import HttpFD
  10. from ..compat import (
  11. compat_urlparse,
  12. )
  13. from ..utils import (
  14. struct_pack,
  15. struct_unpack,
  16. format_bytes,
  17. encodeFilename,
  18. sanitize_open,
  19. xpath_text,
  20. )
  21. class FlvReader(io.BytesIO):
  22. """
  23. Reader for Flv files
  24. The file format is documented in https://www.adobe.com/devnet/f4v.html
  25. """
  26. # Utility functions for reading numbers and strings
  27. def read_unsigned_long_long(self):
  28. return struct_unpack('!Q', self.read(8))[0]
  29. def read_unsigned_int(self):
  30. return struct_unpack('!I', self.read(4))[0]
  31. def read_unsigned_char(self):
  32. return struct_unpack('!B', self.read(1))[0]
  33. def read_string(self):
  34. res = b''
  35. while True:
  36. char = self.read(1)
  37. if char == b'\x00':
  38. break
  39. res += char
  40. return res
  41. def read_box_info(self):
  42. """
  43. Read a box and return the info as a tuple: (box_size, box_type, box_data)
  44. """
  45. real_size = size = self.read_unsigned_int()
  46. box_type = self.read(4)
  47. header_end = 8
  48. if size == 1:
  49. real_size = self.read_unsigned_long_long()
  50. header_end = 16
  51. return real_size, box_type, self.read(real_size - header_end)
  52. def read_asrt(self):
  53. # version
  54. self.read_unsigned_char()
  55. # flags
  56. self.read(3)
  57. quality_entry_count = self.read_unsigned_char()
  58. # QualityEntryCount
  59. for i in range(quality_entry_count):
  60. self.read_string()
  61. segment_run_count = self.read_unsigned_int()
  62. segments = []
  63. for i in range(segment_run_count):
  64. first_segment = self.read_unsigned_int()
  65. fragments_per_segment = self.read_unsigned_int()
  66. segments.append((first_segment, fragments_per_segment))
  67. return {
  68. 'segment_run': segments,
  69. }
  70. def read_afrt(self):
  71. # version
  72. self.read_unsigned_char()
  73. # flags
  74. self.read(3)
  75. # time scale
  76. self.read_unsigned_int()
  77. quality_entry_count = self.read_unsigned_char()
  78. # QualitySegmentUrlModifiers
  79. for i in range(quality_entry_count):
  80. self.read_string()
  81. fragments_count = self.read_unsigned_int()
  82. fragments = []
  83. for i in range(fragments_count):
  84. first = self.read_unsigned_int()
  85. first_ts = self.read_unsigned_long_long()
  86. duration = self.read_unsigned_int()
  87. if duration == 0:
  88. discontinuity_indicator = self.read_unsigned_char()
  89. else:
  90. discontinuity_indicator = None
  91. fragments.append({
  92. 'first': first,
  93. 'ts': first_ts,
  94. 'duration': duration,
  95. 'discontinuity_indicator': discontinuity_indicator,
  96. })
  97. return {
  98. 'fragments': fragments,
  99. }
  100. def read_abst(self):
  101. # version
  102. self.read_unsigned_char()
  103. # flags
  104. self.read(3)
  105. self.read_unsigned_int() # BootstrapinfoVersion
  106. # Profile,Live,Update,Reserved
  107. self.read(1)
  108. # time scale
  109. self.read_unsigned_int()
  110. # CurrentMediaTime
  111. self.read_unsigned_long_long()
  112. # SmpteTimeCodeOffset
  113. self.read_unsigned_long_long()
  114. self.read_string() # MovieIdentifier
  115. server_count = self.read_unsigned_char()
  116. # ServerEntryTable
  117. for i in range(server_count):
  118. self.read_string()
  119. quality_count = self.read_unsigned_char()
  120. # QualityEntryTable
  121. for i in range(quality_count):
  122. self.read_string()
  123. # DrmData
  124. self.read_string()
  125. # MetaData
  126. self.read_string()
  127. segments_count = self.read_unsigned_char()
  128. segments = []
  129. for i in range(segments_count):
  130. box_size, box_type, box_data = self.read_box_info()
  131. assert box_type == b'asrt'
  132. segment = FlvReader(box_data).read_asrt()
  133. segments.append(segment)
  134. fragments_run_count = self.read_unsigned_char()
  135. fragments = []
  136. for i in range(fragments_run_count):
  137. box_size, box_type, box_data = self.read_box_info()
  138. assert box_type == b'afrt'
  139. fragments.append(FlvReader(box_data).read_afrt())
  140. return {
  141. 'segments': segments,
  142. 'fragments': fragments,
  143. }
  144. def read_bootstrap_info(self):
  145. total_size, box_type, box_data = self.read_box_info()
  146. assert box_type == b'abst'
  147. return FlvReader(box_data).read_abst()
  148. def read_bootstrap_info(bootstrap_bytes):
  149. return FlvReader(bootstrap_bytes).read_bootstrap_info()
  150. def build_fragments_list(boot_info):
  151. """ Return a list of (segment, fragment) for each fragment in the video """
  152. res = []
  153. segment_run_table = boot_info['segments'][0]
  154. # I've only found videos with one segment
  155. segment_run_entry = segment_run_table['segment_run'][0]
  156. n_frags = segment_run_entry[1]
  157. fragment_run_entry_table = boot_info['fragments'][0]['fragments']
  158. first_frag_number = fragment_run_entry_table[0]['first']
  159. for (i, frag_number) in zip(range(1, n_frags + 1), itertools.count(first_frag_number)):
  160. res.append((1, frag_number))
  161. return res
  162. def write_unsigned_int(stream, val):
  163. stream.write(struct_pack('!I', val))
  164. def write_unsigned_int_24(stream, val):
  165. stream.write(struct_pack('!I', val)[1:])
  166. def write_flv_header(stream):
  167. """Writes the FLV header to stream"""
  168. # FLV header
  169. stream.write(b'FLV\x01')
  170. stream.write(b'\x05')
  171. stream.write(b'\x00\x00\x00\x09')
  172. stream.write(b'\x00\x00\x00\x00')
  173. def write_metadata_tag(stream, metadata):
  174. """Writes optional metadata tag to stream"""
  175. SCRIPT_TAG = b'\x12'
  176. FLV_TAG_HEADER_LEN = 11
  177. if metadata:
  178. stream.write(SCRIPT_TAG)
  179. write_unsigned_int_24(stream, len(metadata))
  180. stream.write(b'\x00\x00\x00\x00\x00\x00\x00')
  181. stream.write(metadata)
  182. write_unsigned_int(stream, FLV_TAG_HEADER_LEN + len(metadata))
  183. def _add_ns(prop):
  184. return '{http://ns.adobe.com/f4m/1.0}%s' % prop
  185. class HttpQuietDownloader(HttpFD):
  186. def to_screen(self, *args, **kargs):
  187. pass
  188. class F4mFD(FileDownloader):
  189. """
  190. A downloader for f4m manifests or AdobeHDS.
  191. """
  192. def real_download(self, filename, info_dict):
  193. man_url = info_dict['url']
  194. requested_bitrate = info_dict.get('tbr')
  195. self.to_screen('[download] Downloading f4m manifest')
  196. manifest = self.ydl.urlopen(man_url).read()
  197. self.report_destination(filename)
  198. http_dl = HttpQuietDownloader(
  199. self.ydl,
  200. {
  201. 'continuedl': True,
  202. 'quiet': True,
  203. 'noprogress': True,
  204. 'ratelimit': self.params.get('ratelimit', None),
  205. 'test': self.params.get('test', False),
  206. }
  207. )
  208. doc = etree.fromstring(manifest)
  209. formats = [(int(f.attrib.get('bitrate', -1)), f) for f in doc.findall(_add_ns('media'))]
  210. if requested_bitrate is None:
  211. # get the best format
  212. formats = sorted(formats, key=lambda f: f[0])
  213. rate, media = formats[-1]
  214. else:
  215. rate, media = list(filter(
  216. lambda f: int(f[0]) == requested_bitrate, formats))[0]
  217. base_url = compat_urlparse.urljoin(man_url, media.attrib['url'])
  218. bootstrap_node = doc.find(_add_ns('bootstrapInfo'))
  219. if bootstrap_node.text is None:
  220. bootstrap_url = compat_urlparse.urljoin(
  221. base_url, bootstrap_node.attrib['url'])
  222. bootstrap = self.ydl.urlopen(bootstrap_url).read()
  223. else:
  224. bootstrap = base64.b64decode(bootstrap_node.text)
  225. metadata_node = media.find(_add_ns('metadata'))
  226. if metadata_node is not None:
  227. metadata = base64.b64decode(metadata_node.text)
  228. else:
  229. metadata = None
  230. boot_info = read_bootstrap_info(bootstrap)
  231. fragments_list = build_fragments_list(boot_info)
  232. if self.params.get('test', False):
  233. # We only download the first fragment
  234. fragments_list = fragments_list[:1]
  235. total_frags = len(fragments_list)
  236. # For some akamai manifests we'll need to add a query to the fragment url
  237. akamai_pv = xpath_text(doc, _add_ns('pv-2.0'))
  238. tmpfilename = self.temp_name(filename)
  239. (dest_stream, tmpfilename) = sanitize_open(tmpfilename, 'wb')
  240. write_flv_header(dest_stream)
  241. write_metadata_tag(dest_stream, metadata)
  242. # This dict stores the download progress, it's updated by the progress
  243. # hook
  244. state = {
  245. 'downloaded_bytes': 0,
  246. 'frag_counter': 0,
  247. }
  248. start = time.time()
  249. def frag_progress_hook(status):
  250. frag_total_bytes = status.get('total_bytes', 0)
  251. estimated_size = (state['downloaded_bytes'] +
  252. (total_frags - state['frag_counter']) * frag_total_bytes)
  253. if status['status'] == 'finished':
  254. state['downloaded_bytes'] += frag_total_bytes
  255. state['frag_counter'] += 1
  256. progress = self.calc_percent(state['frag_counter'], total_frags)
  257. byte_counter = state['downloaded_bytes']
  258. else:
  259. frag_downloaded_bytes = status['downloaded_bytes']
  260. byte_counter = state['downloaded_bytes'] + frag_downloaded_bytes
  261. frag_progress = self.calc_percent(frag_downloaded_bytes,
  262. frag_total_bytes)
  263. progress = self.calc_percent(state['frag_counter'], total_frags)
  264. progress += frag_progress / float(total_frags)
  265. eta = self.calc_eta(start, time.time(), estimated_size, byte_counter)
  266. self.report_progress(progress, format_bytes(estimated_size),
  267. status.get('speed'), eta)
  268. http_dl.add_progress_hook(frag_progress_hook)
  269. frags_filenames = []
  270. for (seg_i, frag_i) in fragments_list:
  271. name = 'Seg%d-Frag%d' % (seg_i, frag_i)
  272. url = base_url + name
  273. if akamai_pv:
  274. url += '?' + akamai_pv.strip(';')
  275. frag_filename = '%s-%s' % (tmpfilename, name)
  276. success = http_dl.download(frag_filename, {'url': url})
  277. if not success:
  278. return False
  279. with open(frag_filename, 'rb') as down:
  280. down_data = down.read()
  281. reader = FlvReader(down_data)
  282. while True:
  283. _, box_type, box_data = reader.read_box_info()
  284. if box_type == b'mdat':
  285. dest_stream.write(box_data)
  286. break
  287. frags_filenames.append(frag_filename)
  288. dest_stream.close()
  289. self.report_finish(format_bytes(state['downloaded_bytes']), time.time() - start)
  290. self.try_rename(tmpfilename, filename)
  291. for frag_file in frags_filenames:
  292. os.remove(frag_file)
  293. fsize = os.path.getsize(encodeFilename(filename))
  294. self._hook_progress({
  295. 'downloaded_bytes': fsize,
  296. 'total_bytes': fsize,
  297. 'filename': filename,
  298. 'status': 'finished',
  299. })
  300. return True