[dispeak] Improve FLV extraction (closes #13513)
[ytdl] / youtube_dl / extractor / dispeak.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     int_or_none,
8     parse_duration,
9     remove_end,
10     xpath_element,
11     xpath_text,
12 )
13
14
15 class DigitallySpeakingIE(InfoExtractor):
16     _VALID_URL = r'https?://(?:s?evt\.dispeak|events\.digitallyspeaking)\.com/(?:[^/]+/)+xml/(?P<id>[^.]+)\.xml'
17
18     _TESTS = [{
19         # From http://gdcvault.com/play/1023460/Tenacious-Design-and-The-Interface
20         'url': 'http://evt.dispeak.com/ubm/gdc/sf16/xml/840376_BQRC.xml',
21         'md5': 'a8efb6c31ed06ca8739294960b2dbabd',
22         'info_dict': {
23             'id': '840376_BQRC',
24             'ext': 'mp4',
25             'title': 'Tenacious Design and The Interface of \'Destiny\'',
26         },
27     }, {
28         # From http://www.gdcvault.com/play/1014631/Classic-Game-Postmortem-PAC
29         'url': 'http://events.digitallyspeaking.com/gdc/sf11/xml/12396_1299111843500GMPX.xml',
30         'only_matching': True,
31     }, {
32         # From http://www.gdcvault.com/play/1013700/Advanced-Material
33         'url': 'http://sevt.dispeak.com/ubm/gdc/eur10/xml/11256_1282118587281VNIT.xml',
34         'only_matching': True,
35     }, {
36         # From https://gdcvault.com/play/1016624
37         'url': 'https://sevt.dispeak.com/ubm/gdc/online12/xml/201210-822101_1349794556671DDDD.xml',
38         'info_dict': {
39             'id': '201210-822101_1349794556671DDDD',
40             'ext': 'flv',
41             'title': 'Pre-launch - Preparing to Take the Plunge',
42         },
43     }]
44
45     def _parse_mp4(self, metadata):
46         video_formats = []
47         video_root = None
48
49         mp4_video = xpath_text(metadata, './mp4video', default=None)
50         if mp4_video is not None:
51             mobj = re.match(r'(?P<root>https?://.*?/).*', mp4_video)
52             video_root = mobj.group('root')
53         if video_root is None:
54             http_host = xpath_text(metadata, 'httpHost', default=None)
55             if http_host:
56                 video_root = 'http://%s/' % http_host
57         if video_root is None:
58             # Hard-coded in http://evt.dispeak.com/ubm/gdc/sf16/custom/player2.js
59             # Works for GPUTechConf, too
60             video_root = 'http://s3-2u.digitallyspeaking.com/'
61
62         formats = metadata.findall('./MBRVideos/MBRVideo')
63         if not formats:
64             return None
65         for a_format in formats:
66             stream_name = xpath_text(a_format, 'streamName', fatal=True)
67             video_path = re.match(r'mp4\:(?P<path>.*)', stream_name).group('path')
68             url = video_root + video_path
69             bitrate = xpath_text(a_format, 'bitrate')
70             tbr = int_or_none(bitrate)
71             vbr = int_or_none(self._search_regex(
72                 r'-(\d+)\.mp4', video_path, 'vbr', default=None))
73             abr = tbr - vbr if tbr and vbr else None
74             video_formats.append({
75                 'format_id': bitrate,
76                 'url': url,
77                 'tbr': tbr,
78                 'vbr': vbr,
79                 'abr': abr,
80             })
81         return video_formats
82
83     def _parse_flv(self, metadata):
84         formats = []
85         akamai_url = xpath_text(metadata, './akamaiHost', fatal=True)
86         audios = metadata.findall('./audios/audio')
87         for audio in audios:
88             formats.append({
89                 'url': 'rtmp://%s/ondemand?ovpfv=1.1' % akamai_url,
90                 'play_path': remove_end(audio.get('url'), '.flv'),
91                 'ext': 'flv',
92                 'vcodec': 'none',
93                 'format_id': audio.get('code'),
94             })
95         slide_video_path = xpath_text(metadata, './slideVideo')
96         if slide_video_path:
97             formats.append({
98                 'url': 'rtmp://%s/ondemand?ovpfv=1.1' % akamai_url,
99                 'play_path': remove_end(slide_video_path, '.flv'),
100                 'ext': 'flv',
101                 'format_note': 'slide deck video',
102                 'quality': -2,
103                 'preference': -2,
104                 'format_id': 'slides',
105             })
106         speaker_video_path = xpath_text(metadata, './speakerVideo')
107         if speaker_video_path:
108             formats.append({
109                 'url': 'rtmp://%s/ondemand?ovpfv=1.1' % akamai_url,
110                 'play_path': remove_end(speaker_video_path, '.flv'),
111                 'ext': 'flv',
112                 'format_note': 'speaker video',
113                 'quality': -1,
114                 'preference': -1,
115                 'format_id': 'speaker',
116             })
117         return formats
118
119     def _real_extract(self, url):
120         video_id = self._match_id(url)
121
122         xml_description = self._download_xml(url, video_id)
123         metadata = xpath_element(xml_description, 'metadata')
124
125         video_formats = self._parse_mp4(metadata)
126         if video_formats is None:
127             video_formats = self._parse_flv(metadata)
128
129         return {
130             'id': video_id,
131             'formats': video_formats,
132             'title': xpath_text(metadata, 'title', fatal=True),
133             'duration': parse_duration(xpath_text(metadata, 'endTime')),
134             'creator': xpath_text(metadata, 'speaker'),
135         }