Skip to content

Commit

Permalink
Refactor according to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas0907 committed May 11, 2020
1 parent fbfd54d commit 2508a4b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
18 changes: 10 additions & 8 deletions test/test_InfoExtractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,8 @@ def test_parse_mpd_formats_subtitles(self):
'height': 1080,
}],
{},
), (
),
(
# https://github.com/ytdl-org/youtube-dl/pull/14844
'urls_only',
'http://unknown/manifest.mpd', # mpd_url
Expand Down Expand Up @@ -915,7 +916,8 @@ def test_parse_mpd_formats_subtitles(self):
'height': 1080,
}],
{},
), (
),
(
# https://github.com/ytdl-org/youtube-dl/issues/20346
# Media considered unfragmented even though it contains
# Initialization tag
Expand All @@ -933,7 +935,6 @@ def test_parse_mpd_formats_subtitles(self):
'vcodec': 'none',
'tbr': 129.87,
'asr': 48000,

}, {
'url': 'https://v.redd.it/hw1x7rcg7zl21/DASH_240',
'manifest_url': 'https://v.redd.it/hw1x7rcg7zl21/DASHPlaylist.mpd',
Expand Down Expand Up @@ -962,7 +963,8 @@ def test_parse_mpd_formats_subtitles(self):
'fps': 30,
}],
{},
), (
),
(
'subtitles',
'https://example.com/streams/1/playlist/playlist.mpd', # mpd_url
'https://example.com/streams/1/playlist', # mpd_base_url
Expand Down Expand Up @@ -1064,12 +1066,12 @@ def test_parse_mpd_formats_subtitles(self):
for mpd_file, mpd_url, mpd_base_url, expected_formats, expected_subtitles in _TEST_CASES:
with io.open('./test/testdata/mpd/%s.mpd' % mpd_file,
mode='r', encoding='utf-8') as f:
formats, subtitles = self.ie._parse_mpd_formats_subtitles(
info_dict = self.ie._parse_mpd_formats_subtitles(
compat_etree_fromstring(f.read().encode('utf-8')),
mpd_base_url=mpd_base_url, mpd_url=mpd_url)
self.ie._sort_formats(formats)
expect_value(self, formats, expected_formats, None)
expect_value(self, subtitles, expected_subtitles, None)
self.ie._sort_formats(info_dict['formats'])
expect_value(self, info_dict['formats'], expected_formats, None)
expect_value(self, info_dict['subtitles'], expected_subtitles, None)

def test_parse_f4m_formats(self):
_TEST_CASES = [
Expand Down
20 changes: 12 additions & 8 deletions youtube_dl/extractor/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2023,8 +2023,10 @@ def _parse_xspf(self, xspf_doc, playlist_id, xspf_url=None, xspf_base_url=None):
})
return entries

def _extract_mpd_formats(self, *args, **kwargs):
return self._extract_mpd_formats_subtitles(*args, **kwargs)[0]
def _extract_mpd_formats(self, mpd_url, video_id, mpd_id=None, note=None, errnote=None, fatal=True, formats_dict={}, data=None, headers={}, query={}):
return self._extract_mpd_formats_subtitles(
mpd_url, video_id, mpd_id=mpd_id, note=note, errnote=errnote, fatal=fatal,
formats_dict=formats_dict, data=data, headers=headers, query=query)['formats']

def _extract_mpd_formats_subtitles(self, mpd_url, video_id, mpd_id=None, note=None, errnote=None, fatal=True, formats_dict={}, data=None, headers={}, query={}):
res = self._download_xml_handle(
Expand All @@ -2040,11 +2042,13 @@ def _extract_mpd_formats_subtitles(self, mpd_url, video_id, mpd_id=None, note=No
mpd_base_url = base_url(urlh.geturl())

return self._parse_mpd_formats_subtitles(
mpd_doc, mpd_id=mpd_id, mpd_base_url=mpd_base_url,
formats_dict=formats_dict, mpd_url=mpd_url)
mpd_doc, mpd_id=mpd_id, mpd_base_url=mpd_base_url, formats_dict=formats_dict,
mpd_url=mpd_url)

def _parse_mpd_formats(self, *args, **kwargs):
return self._parse_mpd_formats_subtitles(*args, **kwargs)[0]
def _parse_mpd_formats(self, mpd_doc, mpd_id=None, mpd_base_url='', formats_dict={}, mpd_url=None):
return self._parse_mpd_formats_subtitles(
mpd_doc, mpd_id=mpd_id, mpd_base_url=mpd_base_url, formats_dict=formats_dict,
mpd_url=mpd_url)['formats']

def _parse_mpd_formats_subtitles(self, mpd_doc, mpd_id=None, mpd_base_url='', formats_dict={}, mpd_url=None):
"""
Expand All @@ -2055,7 +2059,7 @@ def _parse_mpd_formats_subtitles(self, mpd_doc, mpd_id=None, mpd_base_url='', fo
2. https://en.wikipedia.org/wiki/Dynamic_Adaptive_Streaming_over_HTTP
"""
if mpd_doc.get('type') == 'dynamic':
return []
return {'formats': [], 'subtitles': {}}

namespace = self._search_regex(r'(?i)^{([^}]+)?}MPD$', mpd_doc.tag, 'namespace', default=None)

Expand Down Expand Up @@ -2337,7 +2341,7 @@ def add_segment_url():
formats.append(full_info)
else:
self.report_warning('Unknown MIME type %s in DASH manifest' % mime_type)
return formats, subtitles
return {'formats': formats, 'subtitles': subtitles}

def _extract_ism_formats(self, ism_url, video_id, ism_id=None, note=None, errnote=None, fatal=True, data=None, headers={}, query={}):
res = self._download_xml_handle(
Expand Down
13 changes: 5 additions & 8 deletions youtube_dl/extractor/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2390,14 +2390,11 @@ def _real_extract(self, url):
xspf_base_url=full_response.geturl()),
video_id)
elif re.match(r'(?i)^(?:{[^}]+})?MPD$', doc.tag):
formats, subtitles = self._parse_mpd_formats_subtitles(
doc,
mpd_base_url=full_response.geturl().rpartition('/')[0],
mpd_url=url)
self._sort_formats(formats)
info_dict['formats'] = formats
if subtitles:
info_dict['subtitles'] = subtitles
info_dict.update(
self._parse_mpd_formats_subtitles(
doc, mpd_base_url=full_response.geturl().rpartition('/')[0],
mpd_url=url))
self._sort_formats(info_dict['formats'])
return info_dict
elif re.match(r'^{http://ns\.adobe\.com/f4m/[12]\.0}manifest$', doc.tag):
info_dict['formats'] = self._parse_f4m_formats(doc, url, video_id)
Expand Down

0 comments on commit 2508a4b

Please sign in to comment.