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.

132 lines
4.9 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. import json
  4. import xml.etree.ElementTree
  5. import datetime
  6. from .common import InfoExtractor
  7. from ..utils import (
  8. compat_HTTPError,
  9. ExtractorError,
  10. )
  11. class VevoIE(InfoExtractor):
  12. """
  13. Accepts urls from vevo.com or in the format 'vevo:{id}'
  14. (currently used by MTVIE)
  15. """
  16. _VALID_URL = r'''(?x)
  17. (?:https?://www\.vevo\.com/watch/(?:[^/]+/[^/]+/)?|
  18. https?://cache\.vevo\.com/m/html/embed\.html\?video=|
  19. https?://videoplayer\.vevo\.com/embed/embedded\?videoId=|
  20. vevo:)
  21. (?P<id>[^&?#]+)'''
  22. _TESTS = [{
  23. 'url': 'http://www.vevo.com/watch/hurts/somebody-to-die-for/GB1101300280',
  24. 'file': 'GB1101300280.mp4',
  25. "md5": "06bea460acb744eab74a9d7dcb4bfd61",
  26. 'info_dict': {
  27. "upload_date": "20130624",
  28. "uploader": "Hurts",
  29. "title": "Somebody to Die For",
  30. "duration": 230.12,
  31. "width": 1920,
  32. "height": 1080,
  33. }
  34. }]
  35. _SMIL_BASE_URL = 'http://smil.lvl3.vevo.com/'
  36. def _formats_from_json(self, video_info):
  37. last_version = {'version': -1}
  38. for version in video_info['videoVersions']:
  39. # These are the HTTP downloads, other types are for different manifests
  40. if version['sourceType'] == 2:
  41. if version['version'] > last_version['version']:
  42. last_version = version
  43. if last_version['version'] == -1:
  44. raise ExtractorError('Unable to extract last version of the video')
  45. renditions = xml.etree.ElementTree.fromstring(last_version['data'])
  46. formats = []
  47. # Already sorted from worst to best quality
  48. for rend in renditions.findall('rendition'):
  49. attr = rend.attrib
  50. format_note = '%(videoCodec)s@%(videoBitrate)4sk, %(audioCodec)s@%(audioBitrate)3sk' % attr
  51. formats.append({
  52. 'url': attr['url'],
  53. 'format_id': attr['name'],
  54. 'format_note': format_note,
  55. 'height': int(attr['frameheight']),
  56. 'width': int(attr['frameWidth']),
  57. })
  58. return formats
  59. def _formats_from_smil(self, smil_xml):
  60. formats = []
  61. smil_doc = xml.etree.ElementTree.fromstring(smil_xml.encode('utf-8'))
  62. els = smil_doc.findall('.//{http://www.w3.org/2001/SMIL20/Language}video')
  63. for el in els:
  64. src = el.attrib['src']
  65. m = re.match(r'''(?xi)
  66. (?P<ext>[a-z0-9]+):
  67. (?P<path>
  68. [/a-z0-9]+ # The directory and main part of the URL
  69. _(?P<cbr>[0-9]+)k
  70. _(?P<width>[0-9]+)x(?P<height>[0-9]+)
  71. _(?P<vcodec>[a-z0-9]+)
  72. _(?P<vbr>[0-9]+)
  73. _(?P<acodec>[a-z0-9]+)
  74. _(?P<abr>[0-9]+)
  75. \.[a-z0-9]+ # File extension
  76. )''', src)
  77. if not m:
  78. continue
  79. format_url = self._SMIL_BASE_URL + m.group('path')
  80. formats.append({
  81. 'url': format_url,
  82. 'format_id': 'SMIL_' + m.group('cbr'),
  83. 'vcodec': m.group('vcodec'),
  84. 'acodec': m.group('acodec'),
  85. 'vbr': int(m.group('vbr')),
  86. 'abr': int(m.group('abr')),
  87. 'ext': m.group('ext'),
  88. 'width': int(m.group('width')),
  89. 'height': int(m.group('height')),
  90. })
  91. return formats
  92. def _real_extract(self, url):
  93. mobj = re.match(self._VALID_URL, url)
  94. video_id = mobj.group('id')
  95. json_url = 'http://videoplayer.vevo.com/VideoService/AuthenticateVideo?isrc=%s' % video_id
  96. video_info = self._download_json(json_url, video_id)['video']
  97. formats = self._formats_from_json(video_info)
  98. try:
  99. smil_url = '%s/Video/V2/VFILE/%s/%sr.smil' % (
  100. self._SMIL_BASE_URL, video_id, video_id.lower())
  101. smil_xml = self._download_webpage(smil_url, video_id,
  102. 'Downloading SMIL info')
  103. formats.extend(self._formats_from_smil(smil_xml))
  104. except ExtractorError as ee:
  105. if not isinstance(ee.cause, compat_HTTPError):
  106. raise
  107. self._downloader.report_warning(
  108. 'Cannot download SMIL information, falling back to JSON ..')
  109. timestamp_ms = int(self._search_regex(
  110. r'/Date\((\d+)\)/', video_info['launchDate'], 'launch date'))
  111. upload_date = datetime.datetime.fromtimestamp(timestamp_ms // 1000)
  112. return {
  113. 'id': video_id,
  114. 'title': video_info['title'],
  115. 'formats': formats,
  116. 'thumbnail': video_info['imageUrl'],
  117. 'upload_date': upload_date.strftime('%Y%m%d'),
  118. 'uploader': video_info['mainArtists'][0]['artistName'],
  119. 'duration': video_info['duration'],
  120. }