Python module to generate I-frame playlists for HLS video

pbs pbs Last update: Aug 31, 2023

iframe-playlist-generator

HLS I-frame playlist generator

Documentation

Generate I-frame playlists and an updated variant master playlist from a url:

from iframeplaylistgenerator import update_for_iframes

playlist_data = update_for_iframes('http://devimages.apple.com.edgekey.net/streaming/examples/bipbop_16x9/bipbop_16x9_variant.m3u8')

Here are some sample files that can be generated by this script:

Data Format

The function update_for_iframes returns a standard python dictionary with these keys:

  • master_uri: the uri of the master playlist, ex.: "playlist.m3u8"
  • master_content: the content of the updated master playlist, ex.:

    """    
    #EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=400000,CODECS="avc1.4d001f, mp4a.40.5"\nvideo-400k.m3u8\n#EXT-X-STREAM-INF:BANDWIDTH=150000,CODECS="avc1.4d001f, mp4a.40.5"\nvideo-150k.m3u8\n#EXT-X-STREAM-INF:BANDWIDTH=64000,CODECS="mp4a.40.5"\n
    video-64k.m3u8\n#EXT-X-I-FRAME-STREAM-INF:BANDWIDTH=83598,CODECS="avc1.4d001f",URI="video-400k-iframes.m3u8"\n#EXT-X-I-FRAME-STREAM-INF:BANDWIDTH=38775,CODECS="avc1.4d001f",URI="video-150k-iframes.m3u8"\n
    """
  • iframe_playlists: a list of dictionaries, each with a uri and content for each generated I-frame playlist, ex.:

    [{'uri': 'video-400k-iframes.m3u8', 'content': '#EXTM3U\n...'}, ...]

Using the Data

The returned data can be used as needed, such as uploading the playlists to an s3 bucket:

import boto

AWS_ACCESS_KEY_ID = ''
AWS_SECRET_ACCESS_KEY = ''

s3 = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)

bucket = s3.get_bucket('my_bucket')

master_playlist_key = bucket.new_key(playlist_data['master_uri'])
master_playlist_key.set_metadata('Content-Type', 'application/x-mpegURL')
master_playlist_key.set_contents_from_string(playlist_data['master_content'])
master_playlist_key.set_acl('public-read')

for playlist in playlist_data['iframe_playlists']:
    iframe_playlist_key = bucket.new_key(playlist['uri'])
    iframe_playlist_key.set_metadata('Content-Type', 'application/x-mpegURL')
    iframe_playlist_key.set_contents_from_string(playlist['content'])
    iframe_playlist_key.set_acl('public-read')

Alternate Usage

Alternatively, use the function create_iframe_playlist and pass it an m3u8 Playlist object to generate an I-frame playlist for that specific stream. This function returns a tuple containing an m3u8 IFramePlaylist object pointing to the new I-frame playlist, and a dictionary with these keys:

  • uri: the uri of the I-frame playlist, ex.: "video-400k-iframes.m3u8"
  • content: the content of the I-frame playlist, ex.: "#EXTM3U\n..."

FFmpeg Installation

In order to use the current version of iframe-playlist-generator, you must have a relatively new release of FFmpeg installed. It has only been tested on 2.2.x releases. For instructions, use the FFmpeg compilation guide for your specific OS.

Alternatively, on Mac OS X the Homebrew package manager can be used to install FFmpeg. To install Homebrew on a Mac, run:

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

To install FFmpeg with Homebrew, run:

brew install ffmpeg

To Do

  • Replace FFmpeg dependency with a pure python MPEG transport stream parser

License

This module is Copyright 2014 PBS.org and is available under the Apache License, Version 2.0.

Tags:

Subscribe to our newsletter