Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion addon.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="script.module.python.twitch" name="python-twitch for Kodi" version="3.0.3" provider-name="Seraph91P">
<addon id="script.module.python.twitch" name="python-twitch for Kodi" version="3.0.4" provider-name="Seraph91P">
<requires>
<import addon="xbmc.python" version="3.0.1"/>
<import addon="script.module.six" version="1.11.0"/>
Expand All @@ -9,6 +9,7 @@
<extension point="xbmc.addon.metadata">
<platform>all</platform>
<news>
[add] Parse HLS codec metadata for stream quality selection
[add] Low latency streaming support
[add] Custom codec support (AV1, H.265, H.264)
[fix] Following Channels
Expand Down
42 changes: 30 additions & 12 deletions resources/lib/twitch/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,21 @@
r'#EXT-X-MEDIA:TYPE=VIDEO.*'
r'GROUP-ID="(?P<group_id>[^"]*)",'
r'NAME="(?P<group_name>[^"]*)"[,=\w]*\n'
r'#EXT-X-STREAM-INF:.*'
r'BANDWIDTH=(?P<bandwidth>[0-9]+),'
r'(?:.*RESOLUTION="*(?P<resolution>[0-9xX]+)"*,)?'
r'(?:.*FRAME-RATE=(?P<fps>[0-9.]+))?.*\n'
r'#EXT-X-STREAM-INF:(?P<stream_info>[^\n]*)\n'
r'(?P<url>http.*)')

_m3u_attribute_pattern = re.compile(r'(?P<key>[A-Z-]+)=(?P<value>"[^"]*"|[^,]*)')

_error_pattern = re.compile(r'.*<tr><td><b>error</b></td><td>(?P<message>.+?)</td></tr>.*', re.IGNORECASE)


def _find_m3u_attribute(stream_info, key):
for match in re.finditer(_m3u_attribute_pattern, stream_info):
if match.group('key') == key:
return match.group('value').strip('"')
return None


def _find_frame_rate(group_id, group_name):
group_id = group_id.lower()

Expand Down Expand Up @@ -88,18 +94,24 @@ def m3u8_to_dict(string):
else:
name = m.group('group_name')

if m.group('fps'):
fps = float(m.group('fps'))
stream_info = m.group('stream_info')
bandwidth = _find_m3u_attribute(stream_info, 'BANDWIDTH') or 0
resolution = _find_m3u_attribute(stream_info, 'RESOLUTION')
codecs = _find_m3u_attribute(stream_info, 'CODECS')
frame_rate = _find_m3u_attribute(stream_info, 'FRAME-RATE')
if frame_rate:
fps = float(frame_rate)
else:
fps = _find_frame_rate(m.group('group_id'), m.group('group_name'))

d[m.group('group_id')] = {
'id': m.group('group_id'),
'name': name,
'url': m.group('url'),
'bandwidth': int(m.group('bandwidth')),
'bandwidth': int(bandwidth),
'fps': fps,
'resolution': m.group('resolution')
'resolution': resolution,
'codecs': codecs
}
log.debug('m3u8_to_dict result:\n{0}'.format(d))
return d
Expand All @@ -117,18 +129,24 @@ def m3u8_to_list(string):
else:
name = m.group('group_name')

if m.group('fps'):
fps = float(m.group('fps'))
stream_info = m.group('stream_info')
bandwidth = _find_m3u_attribute(stream_info, 'BANDWIDTH') or 0
resolution = _find_m3u_attribute(stream_info, 'RESOLUTION')
codecs = _find_m3u_attribute(stream_info, 'CODECS')
frame_rate = _find_m3u_attribute(stream_info, 'FRAME-RATE')
if frame_rate:
fps = float(frame_rate)
else:
fps = _find_frame_rate(m.group('group_id'), m.group('group_name'))

l.append({
'id': m.group('group_id'),
'name': name,
'url': m.group('url'),
'bandwidth': int(m.group('bandwidth')),
'bandwidth': int(bandwidth),
'fps': fps,
'resolution': m.group('resolution')
'resolution': resolution,
'codecs': codecs
})

log.debug('m3u8_to_list result:\n{0}'.format(l))
Expand Down
Loading