Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented max bandwidth for hls streams #202

Merged
merged 1 commit into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions resources/language/resource.language.en_gb/strings.po
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ msgid "Show subtitles when available"
msgstr "Show subtitles when available"

msgctxt "#30013"
msgid "InputStream Adaptive"
msgstr "InputStream Adaptive"
msgid "Streaming"
msgstr "Streaming"

msgctxt "#30014"
msgid "Use DRM for 720p HD-quality livestreams"
msgstr "Use DRM for 720p HD-quality livestreams"

msgctxt "#30015"
msgid "Maximum bandwidth (kbits/sec)"
msgstr "Maximum bandwidth (kbits/sec)"
msgid "Maximum bandwidth (kbps)"
msgstr "Maximum bandwidth (kbps)"

msgctxt "#30020"
msgid "Interface"
Expand Down Expand Up @@ -103,6 +103,10 @@ msgctxt "#30056"
msgid "Please fill in your password to login."
msgstr "Please fill in your password to login."

msgctxt "#30057"
msgid "Please increase the maximum bandwidth. Maximum bandwidth is set to %s kbps, but this stream needs a minimum of %s kbps."
msgstr "Please increase the maximum bandwidth. Maximum bandwidth is set to %s kbps, but this stream needs a minimum of %s kbps."

msgctxt "#30061"
msgid "Using a SOCKS proxy requires the PySocks library (script.module.pysocks) installed."
msgstr "Using a SOCKS proxy requires the PySocks library (script.module.pysocks) installed."
Expand Down
12 changes: 8 additions & 4 deletions resources/language/resource.language.nl_nl/strings.po
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ msgid "Show subtitles when available"
msgstr "Toon ondertiteling indien beschikbaar"

msgctxt "#30013"
msgid "InputStream Adaptive"
msgstr "InputStream Adaptive"
msgid "Streaming"
msgstr "Streaming"

msgctxt "#30014"
msgid "Use DRM for 720p HD-quality livestreams"
msgstr "Gebruik DRM voor de livestreams in 720p HD-kwaliteit"

msgctxt "#30015"
msgid "Maximum bandwidth (kbits/sec)"
msgstr "Maximale bandbreedte (kbits/sec)"
msgid "Maximum bandwidth (kbps)"
msgstr "Maximale bandbreedte (kbps)"

msgctxt "#30020"
msgid "Interface"
Expand Down Expand Up @@ -112,6 +112,10 @@ msgctxt "#30056"
msgid "Please fill in your password to login."
msgstr "Gelieve je wachtwoord in te vullen om in te loggen."

msgctxt "#30057"
msgid "Please increase the maximum bandwidth. Maximum bandwidth is set to %s kbps, but this stream needs a minimum of %s kbps."
msgstr "Verhoog de maximale bandbreedte alstublieft. De maximale bandbreedte is ingesteld op %s kbps, maar deze stream heeft minimum %s kbps nodig."

msgctxt "#30061"
msgid "Using a SOCKS proxy requires the PySocks library (script.module.pysocks) installed."
msgstr "Het gebruik van SOCKS proxies vereist dat de PySocks library (script.module.pysocks) geïnstalleerd is."
Expand Down
2 changes: 1 addition & 1 deletion resources/lib/kodiwrappers/kodiwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def play(self, video):
self.log_notice('Subtitle URL: ' + video.subtitle_url)
play_item.setSubtitles([video.subtitle_url])

xbmcplugin.setResolvedUrl(self._handle, True, listitem=play_item)
xbmcplugin.setResolvedUrl(self._handle, bool(video.stream_url), listitem=play_item)
while not xbmc.Player().isPlaying() and not xbmc.Monitor().abortRequested():
xbmc.sleep(100)
xbmc.Player().showSubtitles(subtitles_visible)
Expand Down
84 changes: 45 additions & 39 deletions resources/lib/vrtplayer/streamservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import absolute_import, division, unicode_literals
import json
import re

from resources.lib.helperobjects import apidata, streamurls

Expand Down Expand Up @@ -127,7 +128,6 @@ def _webscrape_api_data(self, video_url):
xvrttoken = None
else:
is_live_stream = False
# publication_id += requests.utils.quote('$')
publication_id += quote('$')
xvrttoken = self.token_resolver.get_xvrttoken()

Expand Down Expand Up @@ -224,7 +224,7 @@ def get_stream(self, video, retry=False, api_data=None):
def _try_get_drm_stream(self, stream_dict, vudrm_token):
if self._license_url is None:
self._get_license_url()
protocol = "mpeg_dash"
protocol = 'mpeg_dash'
encryption_json = '{{"token":"{0}","drm_info":[D{{SSM}}],"kid":"{{KID}}"}}'.format(vudrm_token)
license_key = self._get_license_key(key_url=self._license_url,
key_type='D',
Expand Down Expand Up @@ -260,41 +260,47 @@ def _select_stream(self, stream_dict, vudrm_token):

def _select_hls_substreams(self, master_hls_url):
'''Select HLS substreams to speed up Kodi player start, workaround for slower kodi selection'''
direct_audio_url = None
direct_video_url = None
direct_subtitle_url = None

bandwidth_limit = self._kodi_wrapper.get_max_bandwidth()

# Only select urls from Unified Streaming Platform and when no bandwidth limit is set in Kodi
if any(x in master_hls_url for x in ('.ism/', '.isml/')) and bandwidth_limit == 0:
import re
base_url = master_hls_url.split('.m3u8')[0]
self._kodi_wrapper.log_notice('URL get: ' + master_hls_url, 'Verbose')
m3u8 = urlopen(master_hls_url).read()

# Get audio uri
audio_regex = re.compile(r'#EXT-X-MEDIA:TYPE=AUDIO[\w\-=,\.\"\/]+URI=\"([\w\-=]+)\.m3u8\"')
match_audio = re.findall(audio_regex, m3u8)
hls_variant_url = None
subtitle_url = None
hls_audio_id = None
hls_subtitle_id = None
hls_base_url = master_hls_url.split('.m3u8')[0]
self._kodi_wrapper.log_notice('URL get: ' + master_hls_url, 'Verbose')
hls_playlist = urlopen(master_hls_url).read()
max_bandwidth = self._kodi_wrapper.get_max_bandwidth()
stream_bandwidth = None

# Get hls variant url based on max_bandwith setting
hls_variant_regex = re.compile(r'#EXT-X-STREAM-INF[:\w\-=,]*[:,]BANDWIDTH=([0-9]+)[\w\-=,.\"]+AUDIO=\"([\w-]+)\"[\w\-=,.\"]?(?:SUBTITLES=\"([\w]+)\")?[\w\-=,.\"]+[\r\n]([\w:\/\-.=?&]+)')
# reverse sort by bandwidth
for match in sorted(re.finditer(hls_variant_regex, hls_playlist), key=lambda m: int(m.group(1)), reverse=True):
stream_bandwidth = int(match.group(1)) // 1000
if max_bandwidth == 0 or stream_bandwidth < max_bandwidth:
if match.group(4).startswith('http'):
hls_variant_url = match.group(4)
else:
hls_variant_url = hls_base_url + match.group(4)
hls_audio_id = match.group(2)
hls_subtitle_id = match.group(3)
break

if not hls_variant_url:
message = self._kodi_wrapper.get_localized_string(30057) % (str(max_bandwidth), stream_bandwidth)
self._kodi_wrapper.show_ok_dialog('', message)
self._kodi_wrapper.open_settings()

# Get audio url
if hls_audio_id:
audio_regex = re.compile(r'#EXT-X-MEDIA:TYPE=AUDIO[\w\-=,\.\"\/]+GROUP-ID=\"' + hls_audio_id + r'\"[\w\-=,\.\"\/]+URI=\"([\w\-=]+)\.m3u8\"')
match_audio = re.search(audio_regex, hls_playlist)
if match_audio:
direct_audio_url = match_audio[len(match_audio) - 1]

# Get video uri
video_regex = re.compile(r'#EXT-X-STREAM-INF:[\w\-=,\.\"]+[\r\n]{1}([\w\-=]+\.m3u8(\?vbegin=[0-9]{10})?(&vend=[0-9]{10})?)[\r\n]{2}')
match_video = re.search(video_regex, m3u8)
if match_video:
direct_video_url = base_url + match_video.group(1)

# Get subtitle uri
if self._kodi_wrapper.get_setting('showsubtitles') == 'true':
subtitle_regex = re.compile(r'#EXT-X-MEDIA:TYPE=SUBTITLES[\w\-=,\.\"\/]+URI=\"([\w\-=]+)\.m3u8\"')
match_sub = re.search(subtitle_regex, m3u8)
if match_sub and '/live/' not in master_hls_url:
direct_subtitle_url = base_url + match_sub.group(1) + '.webvtt'

# Merge audio and video uri
if direct_audio_url is not None:
direct_video_url = base_url + direct_audio_url + '-' + direct_video_url.split('-')[-1]
else:
direct_video_url = master_hls_url
return direct_video_url, direct_subtitle_url
hls_variant_url = hls_base_url + match_audio.group(1) + '-' + hls_variant_url.split('-')[-1]

# Get subtitle url, works only for on demand streams
if self._kodi_wrapper.get_setting('showsubtitles') == 'true' and '/live/' not in master_hls_url and hls_subtitle_id:
subtitle_regex = re.compile(r'#EXT-X-MEDIA:TYPE=SUBTITLES[\w\-=,\.\"\/]+GROUP-ID=\"' + hls_subtitle_id + r'\"[\w\-=,\.\"\/]+URI=\"([\w\-=]+)\.m3u8\"')
match_subtitle = re.search(subtitle_regex, hls_playlist)
if match_subtitle:
subtitle_url = hls_base_url + match_subtitle.group(1) + '.webvtt'

return hls_variant_url, subtitle_url