Merge pull request #8876 from remitamine/html5_media
[youtube-dl] / youtube_dl / extractor / vrt.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     determine_ext,
9     float_or_none,
10 )
11
12
13 class VRTIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:deredactie|sporza|cobra(?:\.canvas)?)\.be/cm/(?:[^/]+/)+(?P<id>[^/]+)/*'
15     _TESTS = [
16         # deredactie.be
17         {
18             'url': 'http://deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/EP_141025_JOL',
19             'md5': '4cebde1eb60a53782d4f3992cbd46ec8',
20             'info_dict': {
21                 'id': '2129880',
22                 'ext': 'flv',
23                 'title': 'Het journaal L - 25/10/14',
24                 'description': None,
25                 'timestamp': 1414271750.949,
26                 'upload_date': '20141025',
27                 'duration': 929,
28             },
29             'skip': 'HTTP Error 404: Not Found',
30         },
31         # sporza.be
32         {
33             'url': 'http://sporza.be/cm/sporza/videozone/programmas/extratime/EP_141020_Extra_time',
34             'md5': '11f53088da9bf8e7cfc42456697953ff',
35             'info_dict': {
36                 'id': '2124639',
37                 'ext': 'flv',
38                 'title': 'Bekijk Extra Time van 20 oktober',
39                 'description': 'md5:83ac5415a4f1816c6a93f8138aef2426',
40                 'timestamp': 1413835980.560,
41                 'upload_date': '20141020',
42                 'duration': 3238,
43             },
44             'skip': 'HTTP Error 404: Not Found',
45         },
46         # cobra.be
47         {
48             'url': 'http://cobra.be/cm/cobra/videozone/rubriek/film-videozone/141022-mv-ellis-cafecorsari',
49             'md5': '78a2b060a5083c4f055449a72477409d',
50             'info_dict': {
51                 'id': '2126050',
52                 'ext': 'flv',
53                 'title': 'Bret Easton Ellis in Café Corsari',
54                 'description': 'md5:f699986e823f32fd6036c1855a724ee9',
55                 'timestamp': 1413967500.494,
56                 'upload_date': '20141022',
57                 'duration': 661,
58             },
59             'skip': 'HTTP Error 404: Not Found',
60         },
61         {
62             # YouTube video
63             'url': 'http://deredactie.be/cm/vrtnieuws/videozone/nieuws/cultuurenmedia/1.2622957',
64             'md5': 'b8b93da1df1cea6c8556255a796b7d61',
65             'info_dict': {
66                 'id': 'Wji-BZ0oCwg',
67                 'ext': 'mp4',
68                 'title': 'ROGUE ONE: A STAR WARS STORY Official Teaser Trailer',
69                 'description': 'md5:8e468944dce15567a786a67f74262583',
70                 'uploader': 'Star Wars',
71                 'uploader_id': 'starwars',
72                 'upload_date': '20160407',
73             },
74             'add_ie': ['Youtube'],
75         },
76         {
77             'url': 'http://cobra.canvas.be/cm/cobra/videozone/rubriek/film-videozone/1.2377055',
78             'md5': '',
79             'info_dict': {
80                 'id': '2377055',
81                 'ext': 'mp4',
82                 'title': 'Cafe Derby',
83                 'description': 'Lenny Van Wesemael debuteert met de langspeelfilm Café Derby. Een waar gebeurd maar ook verzonnen verhaal.',
84                 'upload_date': '20150626',
85                 'timestamp': 1435305240.769,
86             },
87             'params': {
88                 # m3u8 download
89                 'skip_download': True,
90             }
91         }
92     ]
93
94     def _real_extract(self, url):
95         video_id = self._match_id(url)
96
97         webpage = self._download_webpage(url, video_id)
98
99         video_id = self._search_regex(
100             r'data-video-id="([^"]+)_[^"]+"', webpage, 'video id', fatal=False)
101
102         src = self._search_regex(
103             r'data-video-src="([^"]+)"', webpage, 'video src', default=None)
104
105         video_type = self._search_regex(
106             r'data-video-type="([^"]+)"', webpage, 'video type', default=None)
107
108         if video_type == 'YouTubeVideo':
109             return self.url_result(src, 'Youtube')
110
111         formats = []
112
113         mobj = re.search(
114             r'data-video-iphone-server="(?P<server>[^"]+)"\s+data-video-iphone-path="(?P<path>[^"]+)"',
115             webpage)
116         if mobj:
117             formats.extend(self._extract_m3u8_formats(
118                 '%s/%s' % (mobj.group('server'), mobj.group('path')),
119                 video_id, 'mp4', m3u8_id='hls', fatal=False))
120
121         if src:
122             if determine_ext(src) == 'm3u8':
123                 formats.extend(self._extract_m3u8_formats(
124                     src, video_id, 'mp4', entry_protocol='m3u8_native',
125                     m3u8_id='hls', fatal=False))
126                 formats.extend(self._extract_f4m_formats(
127                     src.replace('playlist.m3u8', 'manifest.f4m'),
128                     video_id, f4m_id='hds', fatal=False))
129                 if 'data-video-geoblocking="true"' not in webpage:
130                     rtmp_formats = self._extract_smil_formats(
131                         src.replace('playlist.m3u8', 'jwplayer.smil'),
132                         video_id, fatal=False)
133                     formats.extend(rtmp_formats)
134                     for rtmp_format in rtmp_formats:
135                         rtmp_format_c = rtmp_format.copy()
136                         rtmp_format_c['url'] = '%s/%s' % (rtmp_format['url'], rtmp_format['play_path'])
137                         del rtmp_format_c['play_path']
138                         del rtmp_format_c['ext']
139                         http_format = rtmp_format_c.copy()
140                         http_format.update({
141                             'url': rtmp_format_c['url'].replace('rtmp://', 'http://').replace('vod.', 'download.').replace('/_definst_/', '/').replace('mp4:', ''),
142                             'format_id': rtmp_format['format_id'].replace('rtmp', 'http'),
143                             'protocol': 'http',
144                         })
145                         rtsp_format = rtmp_format_c.copy()
146                         rtsp_format.update({
147                             'url': rtsp_format['url'].replace('rtmp://', 'rtsp://'),
148                             'format_id': rtmp_format['format_id'].replace('rtmp', 'rtsp'),
149                             'protocol': 'rtsp',
150                         })
151                         formats.extend([http_format, rtsp_format])
152             else:
153                 formats.extend(self._extract_f4m_formats(
154                     '%s/manifest.f4m' % src, video_id, f4m_id='hds', fatal=False))
155
156         if not formats and 'data-video-geoblocking="true"' in webpage:
157             self.raise_geo_restricted('This video is only available in Belgium')
158
159         self._sort_formats(formats)
160
161         title = self._og_search_title(webpage)
162         description = self._og_search_description(webpage, default=None)
163         thumbnail = self._og_search_thumbnail(webpage)
164         timestamp = float_or_none(self._search_regex(
165             r'data-video-sitestat-pubdate="(\d+)"', webpage, 'timestamp', fatal=False), 1000)
166         duration = float_or_none(self._search_regex(
167             r'data-video-duration="(\d+)"', webpage, 'duration', fatal=False), 1000)
168
169         return {
170             'id': video_id,
171             'title': title,
172             'description': description,
173             'thumbnail': thumbnail,
174             'timestamp': timestamp,
175             'duration': duration,
176             'formats': formats,
177         }