]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/vevo.py
[mooshare] Add support for mooshare.biz (Closes #2149)
[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
JMF
26 'url': 'http://www.vevo.com/watch/hurts/somebody-to-die-for/GB1101300280',
27 'file': 'GB1101300280.mp4',
28 "md5": "06bea460acb744eab74a9d7dcb4bfd61",
29 'info_dict': {
30 "upload_date": "20130624",
31 "uploader": "Hurts",
32 "title": "Somebody to Die For",
33 "duration": 230.12,
34 "width": 1920,
35 "height": 1080,
6f5ac90c 36 }
72321ead
PH
37 }]
38 _SMIL_BASE_URL = 'http://smil.lvl3.vevo.com/'
70d1924f 39
72321ead 40 def _formats_from_json(self, video_info):
88bd97e3
JMF
41 last_version = {'version': -1}
42 for version in video_info['videoVersions']:
43 # These are the HTTP downloads, other types are for different manifests
44 if version['sourceType'] == 2:
45 if version['version'] > last_version['version']:
46 last_version = version
47 if last_version['version'] == -1:
45d7bc2f 48 raise ExtractorError('Unable to extract last version of the video')
88bd97e3
JMF
49
50 renditions = xml.etree.ElementTree.fromstring(last_version['data'])
51 formats = []
52 # Already sorted from worst to best quality
53 for rend in renditions.findall('rendition'):
54 attr = rend.attrib
72321ead 55 format_note = '%(videoCodec)s@%(videoBitrate)4sk, %(audioCodec)s@%(audioBitrate)3sk' % attr
88bd97e3 56 formats.append({
e54fd4b2
PH
57 'url': attr['url'],
58 'format_id': attr['name'],
59 'format_note': format_note,
88bd97e3
JMF
60 'height': int(attr['frameheight']),
61 'width': int(attr['frameWidth']),
62 })
72321ead
PH
63 return formats
64
65 def _formats_from_smil(self, smil_xml):
66 formats = []
67 smil_doc = xml.etree.ElementTree.fromstring(smil_xml.encode('utf-8'))
68 els = smil_doc.findall('.//{http://www.w3.org/2001/SMIL20/Language}video')
69 for el in els:
70 src = el.attrib['src']
71 m = re.match(r'''(?xi)
72 (?P<ext>[a-z0-9]+):
73 (?P<path>
74 [/a-z0-9]+ # The directory and main part of the URL
75 _(?P<cbr>[0-9]+)k
76 _(?P<width>[0-9]+)x(?P<height>[0-9]+)
77 _(?P<vcodec>[a-z0-9]+)
78 _(?P<vbr>[0-9]+)
79 _(?P<acodec>[a-z0-9]+)
80 _(?P<abr>[0-9]+)
81 \.[a-z0-9]+ # File extension
82 )''', src)
83 if not m:
84 continue
85
86 format_url = self._SMIL_BASE_URL + m.group('path')
72321ead
PH
87 formats.append({
88 'url': format_url,
45d7bc2f 89 'format_id': 'SMIL_' + m.group('cbr'),
91c7271a
PH
90 'vcodec': m.group('vcodec'),
91 'acodec': m.group('acodec'),
92 'vbr': int(m.group('vbr')),
93 'abr': int(m.group('abr')),
72321ead
PH
94 'ext': m.group('ext'),
95 'width': int(m.group('width')),
96 'height': int(m.group('height')),
97 })
98 return formats
99
100 def _real_extract(self, url):
101 mobj = re.match(self._VALID_URL, url)
102 video_id = mobj.group('id')
103
104 json_url = 'http://videoplayer.vevo.com/VideoService/AuthenticateVideo?isrc=%s' % video_id
45d7bc2f 105 video_info = self._download_json(json_url, video_id)['video']
72321ead
PH
106
107 formats = self._formats_from_json(video_info)
108 try:
109 smil_url = '%s/Video/V2/VFILE/%s/%sr.smil' % (
110 self._SMIL_BASE_URL, video_id, video_id.lower())
111 smil_xml = self._download_webpage(smil_url, video_id,
45d7bc2f 112 'Downloading SMIL info')
72321ead
PH
113 formats.extend(self._formats_from_smil(smil_xml))
114 except ExtractorError as ee:
115 if not isinstance(ee.cause, compat_HTTPError):
116 raise
117 self._downloader.report_warning(
45d7bc2f 118 'Cannot download SMIL information, falling back to JSON ..')
88bd97e3 119
912cbf5d 120 timestamp_ms = int(self._search_regex(
45d7bc2f 121 r'/Date\((\d+)\)/', video_info['launchDate'], 'launch date'))
912cbf5d 122 upload_date = datetime.datetime.fromtimestamp(timestamp_ms // 1000)
45d7bc2f 123 return {
88bd97e3
JMF
124 'id': video_id,
125 'title': video_info['title'],
126 'formats': formats,
127 'thumbnail': video_info['imageUrl'],
128 'upload_date': upload_date.strftime('%Y%m%d'),
129 'uploader': video_info['mainArtists'][0]['artistName'],
130 'duration': video_info['duration'],
131 }