Skip to content

Commit

Permalink
Merge pull request #77 from ziima/73-commaless-extinf
Browse files Browse the repository at this point in the history
Fixes #73 - Allow missing comma in EXTINF tag
  • Loading branch information
leandromoreira committed Aug 24, 2016
2 parents 1897052 + 8fae3a6 commit 9c5cb8d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
14 changes: 11 additions & 3 deletions m3u8/parser.py
Expand Up @@ -91,7 +91,7 @@ def parse(content, strict=False):
data['key'] = data.get('key', state['current_key'])

elif line.startswith(protocol.extinf):
_parse_extinf(line, data, state)
_parse_extinf(line, data, state, lineno, strict)
state['expect_segment'] = True

elif line.startswith(protocol.ext_x_stream_inf):
Expand Down Expand Up @@ -145,8 +145,16 @@ def _parse_key(line):
key[normalize_attribute(name)] = remove_quotes(value)
return key

def _parse_extinf(line, data, state):
duration, title = line.replace(protocol.extinf + ':', '').split(',')
def _parse_extinf(line, data, state, lineno, strict):
chunks = line.replace(protocol.extinf + ':', '').split(',')
if len(chunks) == 2:
duration, title = chunks
elif len(chunks) == 1:
if strict:
raise ParseError(lineno, line)
else:
duration = chunks[0]
title = ''
if 'segment' not in state:
state['segment'] = {}
state['segment']['duration'] = float(duration)
Expand Down
9 changes: 9 additions & 0 deletions tests/playlists.py
Expand Up @@ -287,6 +287,15 @@
#EXT-X-ENDLIST
'''

# Playlist with EXTINF record not ending with comma
SIMPLE_PLAYLIST_COMMALESS_EXTINF = '''
#EXTM3U
#EXT-X-TARGETDURATION:5220
#EXTINF:5220
http://media.example.com/entire.ts
#EXT-X-ENDLIST
'''

DISCONTINUITY_PLAYLIST_WITH_PROGRAM_DATE_TIME = '''
#EXTM3U
#EXT-X-MEDIA-SEQUENCE:50116
Expand Down
12 changes: 12 additions & 0 deletions tests/test_parser.py
Expand Up @@ -191,3 +191,15 @@ def test_parse_simple_playlist_messy_strict():
with pytest.raises(ParseError) as catch:
m3u8.parse(playlists.SIMPLE_PLAYLIST_MESSY, strict=True)
assert str(catch.value) == 'Syntax error in manifest on line 5: JUNK'

def test_commaless_extinf():
data = m3u8.parse(playlists.SIMPLE_PLAYLIST_COMMALESS_EXTINF)
assert 5220 == data['targetduration']
assert 0 == data['media_sequence']
assert ['http://media.example.com/entire.ts'] == [c['uri'] for c in data['segments']]
assert [5220] == [c['duration'] for c in data['segments']]

def test_commaless_extinf_strict():
with pytest.raises(ParseError) as e:
m3u8.parse(playlists.SIMPLE_PLAYLIST_COMMALESS_EXTINF, strict=True)
assert str(e.value) == 'Syntax error in manifest on line 3: #EXTINF:5220'

0 comments on commit 9c5cb8d

Please sign in to comment.