]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/vevo.py
[vevo] Add suppot for v3 SMIL URLs (Fixes #2409)
[yt-dlp.git] / youtube_dl / extractor / vevo.py
CommitLineData
45d7bc2f
JMF
1from __future__ import unicode_literals
2
70d1924f 3import re
88bd97e3
JMF
4import xml.etree.ElementTree
5import datetime
70d1924f
JMF
6
7from .common import InfoExtractor
8from ..utils import (
72321ead 9 compat_HTTPError,
70d1924f
JMF
10 ExtractorError,
11)
12
88bd97e3 13
70d1924f 14class VevoIE(InfoExtractor):
1c251cd9 15 """
0577177e 16 Accepts urls from vevo.com or in the format 'vevo:{id}'
1c251cd9
JMF
17 (currently used by MTVIE)
18 """
f25571ff
PH
19 _VALID_URL = r'''(?x)
20 (?:https?://www\.vevo\.com/watch/(?:[^/]+/[^/]+/)?|
21 https?://cache\.vevo\.com/m/html/embed\.html\?video=|
ebce53b3 22 https?://videoplayer\.vevo\.com/embed/embedded\?videoId=|
f25571ff
PH
23 vevo:)
24 (?P<id>[^&?#]+)'''
72321ead 25 _TESTS = [{
45d7bc2f 26 'url': 'http://www.vevo.com/watch/hurts/somebody-to-die-for/GB1101300280',
45d7bc2f
JMF
27 "md5": "06bea460acb744eab74a9d7dcb4bfd61",
28 'info_dict': {
27579b9e
PH
29 'id': 'GB1101300280',
30 'ext': 'mp4',
45d7bc2f
JMF
31 "upload_date": "20130624",
32 "uploader": "Hurts",
33 "title": "Somebody to Die For",
34 "duration": 230.12,
35 "width": 1920,
36 "height": 1080,
6f5ac90c 37 }
27579b9e
PH
38 }, {
39 'note': 'v3 SMIL format',
40 'url': 'http://www.vevo.com/watch/cassadee-pope/i-wish-i-could-break-your-heart/USUV71302923',
41 'md5': '893ec0e0d4426a1d96c01de8f2bdff58',
42 'info_dict': {
43 'id': 'USUV71302923',
44 'ext': 'mp4',
45 'upload_date': '20140219',
46 'uploader': 'Cassadee Pope',
47 'title': 'I Wish I Could Break Your Heart',
48 'duration': 226.101,
49 }
72321ead
PH
50 }]
51 _SMIL_BASE_URL = 'http://smil.lvl3.vevo.com/'
70d1924f 52
72321ead 53 def _formats_from_json(self, video_info):
88bd97e3
JMF
54 last_version = {'version': -1}
55 for version in video_info['videoVersions']:
56 # These are the HTTP downloads, other types are for different manifests
57 if version['sourceType'] == 2:
58 if version['version'] > last_version['version']:
59 last_version = version
60 if last_version['version'] == -1:
45d7bc2f 61 raise ExtractorError('Unable to extract last version of the video')
88bd97e3
JMF
62
63 renditions = xml.etree.ElementTree.fromstring(last_version['data'])
64 formats = []
65 # Already sorted from worst to best quality
66 for rend in renditions.findall('rendition'):
67 attr = rend.attrib
72321ead 68 format_note = '%(videoCodec)s@%(videoBitrate)4sk, %(audioCodec)s@%(audioBitrate)3sk' % attr
88bd97e3 69 formats.append({
e54fd4b2
PH
70 'url': attr['url'],
71 'format_id': attr['name'],
72 'format_note': format_note,
88bd97e3
JMF
73 'height': int(attr['frameheight']),
74 'width': int(attr['frameWidth']),
75 })
72321ead
PH
76 return formats
77
78 def _formats_from_smil(self, smil_xml):
79 formats = []
80 smil_doc = xml.etree.ElementTree.fromstring(smil_xml.encode('utf-8'))
81 els = smil_doc.findall('.//{http://www.w3.org/2001/SMIL20/Language}video')
82 for el in els:
83 src = el.attrib['src']
84 m = re.match(r'''(?xi)
85 (?P<ext>[a-z0-9]+):
86 (?P<path>
87 [/a-z0-9]+ # The directory and main part of the URL
88 _(?P<cbr>[0-9]+)k
89 _(?P<width>[0-9]+)x(?P<height>[0-9]+)
90 _(?P<vcodec>[a-z0-9]+)
91 _(?P<vbr>[0-9]+)
92 _(?P<acodec>[a-z0-9]+)
93 _(?P<abr>[0-9]+)
94 \.[a-z0-9]+ # File extension
95 )''', src)
96 if not m:
97 continue
98
99 format_url = self._SMIL_BASE_URL + m.group('path')
72321ead
PH
100 formats.append({
101 'url': format_url,
45d7bc2f 102 'format_id': 'SMIL_' + m.group('cbr'),
91c7271a
PH
103 'vcodec': m.group('vcodec'),
104 'acodec': m.group('acodec'),
105 'vbr': int(m.group('vbr')),
106 'abr': int(m.group('abr')),
72321ead
PH
107 'ext': m.group('ext'),
108 'width': int(m.group('width')),
109 'height': int(m.group('height')),
110 })
111 return formats
112
113 def _real_extract(self, url):
114 mobj = re.match(self._VALID_URL, url)
115 video_id = mobj.group('id')
116
117 json_url = 'http://videoplayer.vevo.com/VideoService/AuthenticateVideo?isrc=%s' % video_id
45d7bc2f 118 video_info = self._download_json(json_url, video_id)['video']
72321ead
PH
119
120 formats = self._formats_from_json(video_info)
27579b9e
PH
121
122 # Download SMIL
123 smil_blocks = sorted((
124 f for f in video_info['videoVersions']
125 if f['sourceType'] == 13),
126 key=lambda f: f['version'])
127
128 smil_url = '%s/Video/V2/VFILE/%s/%sr.smil' % (
129 self._SMIL_BASE_URL, video_id, video_id.lower())
130 if smil_blocks:
131 smil_url_m = self._search_regex(
132 r'url="([^"]+)"', smil_blocks[-1]['data'], 'SMIL URL',
133 fatal=False)
134 if smil_url_m is not None:
135 smil_url = smil_url_m
136
72321ead 137 try:
72321ead 138 smil_xml = self._download_webpage(smil_url, video_id,
45d7bc2f 139 'Downloading SMIL info')
72321ead
PH
140 formats.extend(self._formats_from_smil(smil_xml))
141 except ExtractorError as ee:
142 if not isinstance(ee.cause, compat_HTTPError):
143 raise
144 self._downloader.report_warning(
45d7bc2f 145 'Cannot download SMIL information, falling back to JSON ..')
88bd97e3 146
912cbf5d 147 timestamp_ms = int(self._search_regex(
45d7bc2f 148 r'/Date\((\d+)\)/', video_info['launchDate'], 'launch date'))
912cbf5d 149 upload_date = datetime.datetime.fromtimestamp(timestamp_ms // 1000)
45d7bc2f 150 return {
88bd97e3
JMF
151 'id': video_id,
152 'title': video_info['title'],
153 'formats': formats,
154 'thumbnail': video_info['imageUrl'],
155 'upload_date': upload_date.strftime('%Y%m%d'),
156 'uploader': video_info['mainArtists'][0]['artistName'],
157 'duration': video_info['duration'],
158 }