]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/hbo.py
Implement universal format sorting
[yt-dlp.git] / yt_dlp / extractor / hbo.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5 xpath_text,
6 xpath_element,
7 int_or_none,
8 parse_duration,
9 urljoin,
10 )
11
12
13 class HBOBaseIE(InfoExtractor):
14 _FORMATS_INFO = {
15 'pro7': {
16 'width': 1280,
17 'height': 720,
18 },
19 '1920': {
20 'width': 1280,
21 'height': 720,
22 },
23 'pro6': {
24 'width': 768,
25 'height': 432,
26 },
27 '640': {
28 'width': 768,
29 'height': 432,
30 },
31 'pro5': {
32 'width': 640,
33 'height': 360,
34 },
35 'highwifi': {
36 'width': 640,
37 'height': 360,
38 },
39 'high3g': {
40 'width': 640,
41 'height': 360,
42 },
43 'medwifi': {
44 'width': 400,
45 'height': 224,
46 },
47 'med3g': {
48 'width': 400,
49 'height': 224,
50 },
51 }
52
53 def _extract_info(self, url, display_id):
54 video_data = self._download_xml(url, display_id)
55 video_id = xpath_text(video_data, 'id', fatal=True)
56 episode_title = title = xpath_text(video_data, 'title', fatal=True)
57 series = xpath_text(video_data, 'program')
58 if series:
59 title = '%s - %s' % (series, title)
60
61 formats = []
62 for source in xpath_element(video_data, 'videos', 'sources', True):
63 if source.tag == 'size':
64 path = xpath_text(source, './/path')
65 if not path:
66 continue
67 width = source.attrib.get('width')
68 format_info = self._FORMATS_INFO.get(width, {})
69 height = format_info.get('height')
70 fmt = {
71 'url': path,
72 'format_id': 'http%s' % ('-%dp' % height if height else ''),
73 'width': format_info.get('width'),
74 'height': height,
75 }
76 rtmp = re.search(r'^(?P<url>rtmpe?://[^/]+/(?P<app>.+))/(?P<playpath>mp4:.+)$', path)
77 if rtmp:
78 fmt.update({
79 'url': rtmp.group('url'),
80 'play_path': rtmp.group('playpath'),
81 'app': rtmp.group('app'),
82 'ext': 'flv',
83 'format_id': fmt['format_id'].replace('http', 'rtmp'),
84 })
85 formats.append(fmt)
86 else:
87 video_url = source.text
88 if not video_url:
89 continue
90 if source.tag == 'tarball':
91 formats.extend(self._extract_m3u8_formats(
92 video_url.replace('.tar', '/base_index_w8.m3u8'),
93 video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
94 elif source.tag == 'hls':
95 m3u8_formats = self._extract_m3u8_formats(
96 video_url.replace('.tar', '/base_index.m3u8'),
97 video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
98 for f in m3u8_formats:
99 if f.get('vcodec') == 'none' and not f.get('tbr'):
100 f['tbr'] = int_or_none(self._search_regex(
101 r'-(\d+)k/', f['url'], 'tbr', default=None))
102 formats.extend(m3u8_formats)
103 elif source.tag == 'dash':
104 formats.extend(self._extract_mpd_formats(
105 video_url.replace('.tar', '/manifest.mpd'),
106 video_id, mpd_id='dash', fatal=False))
107 else:
108 format_info = self._FORMATS_INFO.get(source.tag, {})
109 formats.append({
110 'format_id': 'http-%s' % source.tag,
111 'url': video_url,
112 'width': format_info.get('width'),
113 'height': format_info.get('height'),
114 })
115 self._sort_formats(formats)
116
117 thumbnails = []
118 card_sizes = xpath_element(video_data, 'titleCardSizes')
119 if card_sizes is not None:
120 for size in card_sizes:
121 path = xpath_text(size, 'path')
122 if not path:
123 continue
124 width = int_or_none(size.get('width'))
125 thumbnails.append({
126 'id': width,
127 'url': path,
128 'width': width,
129 })
130
131 subtitles = None
132 caption_url = xpath_text(video_data, 'captionUrl')
133 if caption_url:
134 subtitles = {
135 'en': [{
136 'url': caption_url,
137 'ext': 'ttml'
138 }],
139 }
140
141 return {
142 'id': video_id,
143 'title': title,
144 'duration': parse_duration(xpath_text(video_data, 'duration/tv14')),
145 'series': series,
146 'episode': episode_title,
147 'formats': formats,
148 'thumbnails': thumbnails,
149 'subtitles': subtitles,
150 }
151
152
153 class HBOIE(HBOBaseIE):
154 IE_NAME = 'hbo'
155 _VALID_URL = r'https?://(?:www\.)?hbo\.com/(?:video|embed)(?:/[^/]+)*/(?P<id>[^/?#]+)'
156 _TEST = {
157 'url': 'https://www.hbo.com/video/game-of-thrones/seasons/season-8/videos/trailer',
158 'md5': '8126210656f433c452a21367f9ad85b3',
159 'info_dict': {
160 'id': '22113301',
161 'ext': 'mp4',
162 'title': 'Game of Thrones - Trailer',
163 },
164 'expected_warnings': ['Unknown MIME type application/mp4 in DASH manifest'],
165 }
166
167 def _real_extract(self, url):
168 display_id = self._match_id(url)
169 webpage = self._download_webpage(url, display_id)
170 location_path = self._parse_json(self._html_search_regex(
171 r'data-state="({.+?})"', webpage, 'state'), display_id)['video']['locationUrl']
172 return self._extract_info(urljoin(url, location_path), display_id)