vevo.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import compat_etree_fromstring
  5. from ..utils import (
  6. ExtractorError,
  7. int_or_none,
  8. )
  9. class VevoIE(InfoExtractor):
  10. '''
  11. Accepts urls from vevo.com or in the format 'vevo:{id}'
  12. (currently used by MTVIE and MySpaceIE)
  13. '''
  14. _VALID_URL = r'''(?x)
  15. (?:https?://www\.vevo\.com/watch/(?:[^/]+/(?:[^/]+/)?)?|
  16. https?://cache\.vevo\.com/m/html/embed\.html\?video=|
  17. https?://videoplayer\.vevo\.com/embed/embedded\?videoId=|
  18. vevo:)
  19. (?P<id>[^&?#]+)'''
  20. _TESTS = [{
  21. 'url': 'http://www.vevo.com/watch/hurts/somebody-to-die-for/GB1101300280',
  22. 'md5': '2dbc7e9fd4f1c60436c9aa73a5406193',
  23. 'info_dict': {
  24. 'id': 'Pt1kc_FniKM',
  25. 'ext': 'mp4',
  26. 'title': 'Hurts - Somebody to Die For',
  27. 'description': 'md5:13e925b89af6b01c7e417332bd23c4bf',
  28. 'uploader_id': 'HurtsVEVO',
  29. 'uploader': 'HurtsVEVO',
  30. 'upload_date': '20130624',
  31. 'duration': 230,
  32. },
  33. 'add_ie': ['Youtube'],
  34. }, {
  35. 'note': 'v3 SMIL format',
  36. 'url': 'http://www.vevo.com/watch/cassadee-pope/i-wish-i-could-break-your-heart/USUV71302923',
  37. 'md5': '13d5204f520af905eeffa675040b8e76',
  38. 'info_dict': {
  39. 'id': 'ByGmQn1uxJw',
  40. 'ext': 'mp4',
  41. 'title': 'Cassadee Pope - I Wish I Could Break Your Heart',
  42. 'description': 'md5:5e9721c92ef117a6f69d00e9b42ceba7',
  43. 'uploader_id': 'CassadeeVEVO',
  44. 'uploader': 'CassadeeVEVO',
  45. 'upload_date': '20140219',
  46. 'duration': 226,
  47. 'age_limit': 0,
  48. },
  49. 'add_ie': ['Youtube'],
  50. }, {
  51. 'note': 'Age-limited video',
  52. 'url': 'https://www.vevo.com/watch/justin-timberlake/tunnel-vision-explicit/USRV81300282',
  53. 'info_dict': {
  54. 'id': '07FYdnEawAQ',
  55. 'ext': 'mp4',
  56. 'age_limit': 18,
  57. 'title': 'Justin Timberlake - Tunnel Vision (Explicit)',
  58. 'description': 'md5:64249768eec3bc4276236606ea996373',
  59. 'uploader_id': 'justintimberlakeVEVO',
  60. 'uploader': 'justintimberlakeVEVO',
  61. 'upload_date': '20130703',
  62. },
  63. 'params': {
  64. 'skip_download': 'true',
  65. },
  66. 'add_ie': ['Youtube'],
  67. }, {
  68. 'note': 'No video_info',
  69. 'url': 'http://www.vevo.com/watch/k-camp-1/Till-I-Die/USUV71503000',
  70. 'md5': '8b83cc492d72fc9cf74a02acee7dc1b0',
  71. 'info_dict': {
  72. 'id': 'USUV71503000',
  73. 'ext': 'mp4',
  74. 'title': 'Till I Die - K Camp ft. T.I.',
  75. 'duration': 193,
  76. },
  77. }]
  78. _SMIL_BASE_URL = 'http://smil.lvl3.vevo.com'
  79. _SOURCE_TYPES = {
  80. 0: 'youtube',
  81. 1: 'brightcove',
  82. 2: 'http',
  83. 3: 'hls_ios',
  84. 4: 'hls',
  85. 5: 'smil', # http
  86. 7: 'f4m_cc',
  87. 8: 'f4m_ak',
  88. 9: 'f4m_l3',
  89. 10: 'ism',
  90. 13: 'smil', # rtmp
  91. 18: 'dash',
  92. }
  93. _VERSIONS = {
  94. 0: 'youtube',
  95. 1: 'level3',
  96. 2: 'akamai',
  97. 3: 'level3',
  98. 4: 'amazon',
  99. }
  100. def _parse_smil_formats(self, smil, smil_url, video_id, namespace=None, f4m_params=None, transform_rtmp_url=None):
  101. formats = []
  102. els = smil.findall('.//{http://www.w3.org/2001/SMIL20/Language}video')
  103. for el in els:
  104. src = el.attrib['src']
  105. m = re.match(r'''(?xi)
  106. (?P<ext>[a-z0-9]+):
  107. (?P<path>
  108. [/a-z0-9]+ # The directory and main part of the URL
  109. _(?P<tbr>[0-9]+)k
  110. _(?P<width>[0-9]+)x(?P<height>[0-9]+)
  111. _(?P<vcodec>[a-z0-9]+)
  112. _(?P<vbr>[0-9]+)
  113. _(?P<acodec>[a-z0-9]+)
  114. _(?P<abr>[0-9]+)
  115. \.[a-z0-9]+ # File extension
  116. )''', src)
  117. if not m:
  118. continue
  119. format_url = self._SMIL_BASE_URL + m.group('path')
  120. formats.append({
  121. 'url': format_url,
  122. 'format_id': 'smil_' + m.group('tbr'),
  123. 'vcodec': m.group('vcodec'),
  124. 'acodec': m.group('acodec'),
  125. 'tbr': int(m.group('tbr')),
  126. 'vbr': int(m.group('vbr')),
  127. 'abr': int(m.group('abr')),
  128. 'ext': m.group('ext'),
  129. 'width': int(m.group('width')),
  130. 'height': int(m.group('height')),
  131. })
  132. return formats
  133. def _real_extract(self, url):
  134. video_id = self._match_id(url)
  135. webpage = None
  136. json_url = 'http://videoplayer.vevo.com/VideoService/AuthenticateVideo?isrc=%s' % video_id
  137. response = self._download_json(json_url, video_id)
  138. video_info = response['video'] or {}
  139. if not video_info:
  140. ytid = response.get('errorInfo', {}).get('ytid')
  141. if ytid:
  142. return self.url_result(ytid, 'Youtube', ytid)
  143. if response.get('statusCode') != 909:
  144. if 'statusMessage' in response:
  145. raise ExtractorError('%s said: %s' % (
  146. self.IE_NAME, response['statusMessage']), expected=True)
  147. raise ExtractorError('Unable to extract videos')
  148. if url.startswith('vevo:'):
  149. raise ExtractorError(
  150. 'Please specify full Vevo URL for downloading', expected=True)
  151. webpage = self._download_webpage(url, video_id)
  152. title = video_info.get('title') or self._og_search_title(webpage)
  153. smil_parsed = False
  154. formats = []
  155. for video_version in video_info['videoVersions']:
  156. version = self._VERSIONS.get(video_version['version'])
  157. if version == 'youtube':
  158. return self.url_result(
  159. video_version['id'], 'Youtube', video_version['id'])
  160. else:
  161. source_type = self._SOURCE_TYPES.get(video_version['sourceType'])
  162. renditions = compat_etree_fromstring(video_version['data'])
  163. if source_type == 'http':
  164. for rend in renditions.findall('rendition'):
  165. attr = rend.attrib
  166. formats.append({
  167. 'url': attr['url'],
  168. 'format_id': '%s-%s' % (version, attr['name']),
  169. 'height': int_or_none(attr.get('frameheight')),
  170. 'width': int_or_none(attr.get('frameWidth')),
  171. 'tbr': int_or_none(attr.get('totalBitrate')),
  172. 'vbr': int_or_none(attr.get('videoBitrate')),
  173. 'abr': int_or_none(attr.get('audioBitrate')),
  174. 'vcodec': attr.get('videoCodec'),
  175. 'acodec': attr.get('audioCodec'),
  176. })
  177. elif source_type == 'hls':
  178. formats.extend(self._extract_m3u8_formats(
  179. renditions.find('rendition').attrib['url'], video_id,
  180. 'mp4', 'm3u8_native', m3u8_id='hls-%s' % version, fatal=False))
  181. elif source_type == 'smil' and not smil_parsed:
  182. formats.extend(self._extract_smil_formats(
  183. renditions.find('rendition').attrib['url'], video_id, False))
  184. smil_parsed = True
  185. self._sort_formats(formats)
  186. is_explicit = video_info.get('isExplicit')
  187. if is_explicit is True:
  188. age_limit = 18
  189. elif is_explicit is False:
  190. age_limit = 0
  191. else:
  192. age_limit = None
  193. timestamp = int_or_none(self._search_regex(
  194. r'/Date\((\d+)\)/',
  195. video_info['launchDate'], 'launch date', fatal=False),
  196. scale=1000) if video_info else None
  197. duration = video_info.get('duration') or int_or_none(
  198. self._html_search_meta('video:duration', webpage))
  199. return {
  200. 'id': video_id,
  201. 'title': title,
  202. 'formats': formats,
  203. 'thumbnail': video_info.get('imageUrl'),
  204. 'timestamp': timestamp,
  205. 'uploader': video_info['mainArtists'][0]['artistName'] if video_info else None,
  206. 'duration': duration,
  207. 'age_limit': age_limit,
  208. }