[jwplatform] Improved m3u8 and rtmp support
[youtube-dl] / youtube_dl / extractor / jwplatform.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     int_or_none,
11 )
12
13
14 class JWPlatformBaseIE(InfoExtractor):
15     def _parse_jwplayer_data(self, jwplayer_data, video_id, require_title=True, m3u8_id=None, rtmp_params=None):
16         video_data = jwplayer_data['playlist'][0]
17
18         formats = []
19         for source in video_data['sources']:
20             source_url = self._proto_relative_url(source['file'])
21             source_type = source.get('type') or ''
22             if source_type in ('application/vnd.apple.mpegurl', 'hls') or determine_ext(source_url) == 'm3u8':
23                 formats.extend(self._extract_m3u8_formats(
24                     source_url, video_id, 'mp4', 'm3u8_native', m3u8_id=m3u8_id, fatal=False))
25             elif source_type.startswith('audio'):
26                 formats.append({
27                     'url': source_url,
28                     'vcodec': 'none',
29                 })
30             else:
31                 a_format = {
32                     'url': source_url,
33                     'width': int_or_none(source.get('width')),
34                     'height': int_or_none(source.get('height')),
35                 }
36                 if source_url.startswith('rtmp'):
37                     # See com/longtailvideo/jwplayer/media/RTMPMediaProvider.as
38                     # of jwplayer.flash.swf
39                     rtmp_url, prefix, play_path = re.split(
40                         r'((?:mp4|mp3|flv):)', source_url, 1)
41                     a_format.update({
42                         'url': rtmp_url,
43                         'ext': 'flv',
44                         'play_path': prefix + play_path,
45                     })
46                     if rtmp_params:
47                         a_format.update(rtmp_params)
48                 formats.append(a_format)
49         self._sort_formats(formats)
50
51         subtitles = {}
52         tracks = video_data.get('tracks')
53         if tracks and isinstance(tracks, list):
54             for track in tracks:
55                 if track.get('file') and track.get('kind') == 'captions':
56                     subtitles.setdefault(track.get('label') or 'en', []).append({
57                         'url': self._proto_relative_url(track['file'])
58                     })
59
60         return {
61             'id': video_id,
62             'title': video_data['title'] if require_title else video_data.get('title'),
63             'description': video_data.get('description'),
64             'thumbnail': self._proto_relative_url(video_data.get('image')),
65             'timestamp': int_or_none(video_data.get('pubdate')),
66             'duration': float_or_none(jwplayer_data.get('duration')),
67             'subtitles': subtitles,
68             'formats': formats,
69         }
70
71
72 class JWPlatformIE(JWPlatformBaseIE):
73     _VALID_URL = r'(?:https?://content\.jwplatform\.com/(?:feeds|players|jw6)/|jwplatform:)(?P<id>[a-zA-Z0-9]{8})'
74     _TEST = {
75         'url': 'http://content.jwplatform.com/players/nPripu9l-ALJ3XQCI.js',
76         'md5': 'fa8899fa601eb7c83a64e9d568bdf325',
77         'info_dict': {
78             'id': 'nPripu9l',
79             'ext': 'mov',
80             'title': 'Big Buck Bunny Trailer',
81             'description': 'Big Buck Bunny is a short animated film by the Blender Institute. It is made using free and open source software.',
82             'upload_date': '20081127',
83             'timestamp': 1227796140,
84         }
85     }
86
87     @staticmethod
88     def _extract_url(webpage):
89         mobj = re.search(
90             r'<script[^>]+?src=["\'](?P<url>(?:https?:)?//content.jwplatform.com/players/[a-zA-Z0-9]{8})',
91             webpage)
92         if mobj:
93             return mobj.group('url')
94
95     def _real_extract(self, url):
96         video_id = self._match_id(url)
97         json_data = self._download_json('http://content.jwplatform.com/feeds/%s.json' % video_id, video_id)
98         return self._parse_jwplayer_data(json_data, video_id)