m3u8 v0.3.0: Playlist Parsing

I just released a new version of my m3u8 gem, this new version provides parsing of m3u8 playlists used in HTTP Live Streaming (HLS). It represents a significant refactoring since the original scope of the project was limited to only generation of playlists.

You can now read a playlist into the newly extended object model, modify or add segments/child playlists, and generate a new m3u8 output file. There is a new items array made up of either PlaylistItem instances for master playlists or SegmentItem instances for regular playlists containing MPEG transport stream files (MPEG-TS). New validation has been added to support modifying and adding items in this array.

require 'm3u8'
require 'tempfile'

file = File.open 'master.m3u8'
playlist = M3u8::Playlist.read file

# individual playlist items are accessed via this array:
playlist.items

# create a new playlist item with attributes:
params = { program_id: 1, width: 1920, height: 1080, profile: 'high',
           level: 4.1, audio: 'aac-lc', bitrate: 92000,
           playlist: 'http://test.url/veryhigh.m3u8' }
item = M3u8::PlaylistItem.new(params)

# insert at top of the master playlist:
playlist.items.insert 0, item

# generate new playlist output file with the modifications:
tmp = Tempfile.new 'master1.m3u8'
playlist.write tmp
tmp.close

You end up with a master playlist that looks something like this, with the new adaptive bitrate streaming quality level on top:

#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,RESOLUTION=1920x1080,CODECS="avc1.640028,mp4a.40.2",BANDWIDTH=92000
http://test.url/veryhigh.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,RESOLUTION=1920x1080,CODECS="avc1.640028,mp4a.40.2",BANDWIDTH=5042000
hls/1080-7mbps/1080-7mbps.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,RESOLUTION=1920x1080,CODECS="avc1.640028,mp4a.40.2",BANDWIDTH=4853000
hls/1080/1080.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,RESOLUTION=1280x720,CODECS="avc1.4d001f,mp4a.40.2",BANDWIDTH=2387000
hls/720/720.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,RESOLUTION=896x504,CODECS="avc1.4d001f,mp4a.40.2",BANDWIDTH=1365000
hls/504/504.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,RESOLUTION=640x360,CODECS="avc1.66.30,mp4a.40.2",BANDWIDTH=861000
hls/360/360.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,CODECS="mp4a.40.2",BANDWIDTH=6400
hls/64k/64k.m3u8

Regular playlists containing MPEG-TS segments (the kind of playlists each entry in the above master playlist would point to) work the same way except you use the SegmentItem class instead for the individual items that make up the playlist.

Feel free to take a look at the specs to get a better idea of how the API works.

Headshot

Hi there, I'm Seth Deckard, a software developer with years of experience working in Ruby and Rails. I co-founded WarningAware and have authored several open source projects on GitHub. You can reach me on Twitter.