rtss: github.com/osrtss/rtss/m3u8 Index | Examples | Files

package m3u8

import "github.com/osrtss/rtss/m3u8"

Index

Examples

Package Files

doc.go reader.go structure.go writer.go

Constants

const (

    // DATETIME time format
    DATETIME = time.RFC3339Nano // Format for EXT-X-PROGRAM-DATE-TIME
)

Variables

var (
    // ErrPlaylistFull playlist is full
    ErrPlaylistFull = errors.New("playlist is full")
)
var TimeParse func(value string) (time.Time, error) = FullTimeParse

Allow globally apply and/or override Time Parser function. Available variants:

* FullTimeParse - implements full featured ISO/IEC 8601:2004
* StrictTimeParse - implements only RFC3339 Nanoseconds format

func FullTimeParse Uses

func FullTimeParse(value string) (time.Time, error)

FullTimeParse implements ISO/IEC 8601:2004.

func StrictTimeParse Uses

func StrictTimeParse(value string) (time.Time, error)

StrictTimeParse implements RFC3339 with Nanoseconds accuracy.

type Alternative Uses

type Alternative struct {
    GroupID         string
    URI             string
    Type            string
    Language        string
    Name            string
    Default         bool
    Autoselect      string
    Forced          string
    Characteristics string
    Subtitles       string
}

Alternative This structure represents EXT-X-MEDIA tag in variants.

type Key Uses

type Key struct {
    Method            string
    URI               string
    IV                string
    Keyformat         string
    Keyformatversions string
}

Key This structure represents information about stream encryption.

Realizes EXT-X-KEY tag.

type ListType Uses

type ListType uint

ListType list type

const (

    // MASTER type
    MASTER ListType = iota + 1
    // MEDIA type
    MEDIA
)

type Map Uses

type Map struct {
    URI    string
    Limit  int64 // <n> is length in bytes for the file under URI
    Offset int64 // [@o] is offset from the start of the file under URI
}

Map This structure represents specifies how to obtain the Media Initialization Section required to parse the applicable Media Segments. It applies to every Media Segment that appears after it in the Playlist until the next EXT-X-MAP tag or until the end of the playlist.

Realizes EXT-MAP tag.

type MasterPlaylist Uses

type MasterPlaylist struct {
    Variants      []*Variant
    Args          string // optional arguments placed after URI (URI?Args)
    CypherVersion string // non-standard tag for Widevine (see also WV struct)

    Ver uint8
    // contains filtered or unexported fields
}

MasterPlaylist This structure represents a master playlist which combines media playlists for multiple bitrates. URI lines in the playlist identify media playlists. Sample of Master Playlist file:

#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1280000
http://example.com/low.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2560000
http://example.com/mid.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=7680000
http://example.com/hi.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=65000,CODECS="mp4a.40.5"
http://example.com/audio-only.m3u8

func NewMasterPlaylist Uses

func NewMasterPlaylist() *MasterPlaylist

NewMasterPlaylist Create new empty master playlist. Master playlist consists of variants.

func (*MasterPlaylist) Append Uses

func (p *MasterPlaylist) Append(uri string, chunklist *MediaPlaylist, params VariantParams)

Append variant to master playlist. This operation does reset playlist cache.

func (*MasterPlaylist) Decode Uses

func (p *MasterPlaylist) Decode(data bytes.Buffer, strict bool) error

Decode parses a master playlist passed from the buffer. If `strict` parameter is true then it returns first syntax error.

func (*MasterPlaylist) DecodeFrom Uses

func (p *MasterPlaylist) DecodeFrom(reader io.Reader, strict bool) error

DecodeFrom parses a master playlist passed from the io.Reader stream. If `strict` parameter is true then it returns first syntax error.

func (*MasterPlaylist) Encode Uses

func (p *MasterPlaylist) Encode() *bytes.Buffer

Encode Generate output in M3U8 format.

func (*MasterPlaylist) ResetCache Uses

func (p *MasterPlaylist) ResetCache()

ResetCache reset cache

func (*MasterPlaylist) SetVersion Uses

func (p *MasterPlaylist) SetVersion(ver uint8)

SetVersion sets the playlist version number, note the version maybe changed automatically by other Set methods.

func (*MasterPlaylist) String Uses

func (p *MasterPlaylist) String() string

String For compatibility with Stringer interface For example fmt.Printf("%s", sampleMediaList) will encode playist and print its string representation.

Create new master playlist Add media playlist Encode structures to HLS

Code:

m := m3u8.NewMasterPlaylist()
p, _ := m3u8.NewMediaPlaylist(3, 5)
for i := 0; i < 5; i++ {
    p.Append(fmt.Sprintf("test%d.ts", i), 5.0, "")
}
m.Append("chunklist1.m3u8", p, m3u8.VariantParams{ProgramID: 123, Bandwidth: 1500000, Resolution: "576x480"})
m.Append("chunklist2.m3u8", p, m3u8.VariantParams{ProgramID: 123, Bandwidth: 1500000, Resolution: "576x480"})
fmt.Printf("%s", m)

Output:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-STREAM-INF:PROGRAM-ID=123,BANDWIDTH=1500000,RESOLUTION=576x480
chunklist1.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=123,BANDWIDTH=1500000,RESOLUTION=576x480
chunklist2.m3u8

func (*MasterPlaylist) Version Uses

func (p *MasterPlaylist) Version() uint8

Version returns the current playlist version number

type MediaPlaylist Uses

type MediaPlaylist struct {
    Segments  []*MediaSegment
    MediaType MediaType

    Key            *Key   // EXT-X-KEY is optional encryption key displayed before any segments (default key for the playlist)
    Map            *Map   // EXT-X-MAP is optional tag specifies how to obtain the Media Initialization Section (default map for the playlist)
    WV             *WV    // Widevine related tags outside of M3U8 specs
    Args           string // optional arguments placed after URIs (URI?Args)
    TargetDuration float64
    Winsize        uint // max number of segments displayed in an encoded playlist; need set to zero for VOD playlists

    SeqNo  uint64 // EXT-X-MEDIA-SEQUENCE
    Ver    uint8
    Iframe bool // EXT-X-I-FRAMES-ONLY
    Closed bool // is this VOD (closed) or Live (sliding) playlist?
    // contains filtered or unexported fields
}

MediaPlaylist This structure represents a single bitrate playlist aka media playlist. It is related to both a simple media playlist and a sliding window media playlist. URI lines in the Playlist point to media segments.

Simple Media Playlist file sample:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:5220
#EXTINF:5219.2,
http://media.example.com/entire.ts
#EXT-X-ENDLIST

Sample of Sliding Window Media Playlist, using HTTPS:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:8
#EXT-X-MEDIA-SEQUENCE:2680

#EXTINF:7.975,
https://priv.example.com/fileSequence2680.ts
#EXTINF:7.941,
https://priv.example.com/fileSequence2681.ts
#EXTINF:7.975,
https://priv.example.com/fileSequence2682.ts

Create new media playlist Add two segments to media playlist Print it

Code:

p, _ := m3u8.NewMediaPlaylist(0, 2)
p.Append("test01.ts", 5.0, "")
p.Append("test02.ts", 6.0, "")
fmt.Printf("%s\n", p)

Output:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-TARGETDURATION:6
#EXTINF:5.000,
test01.ts
#EXTINF:6.000,
test02.ts

Create new media playlist Add two segments to media playlist Print it

Code:

p, _ := m3u8.NewMediaPlaylist(0, 2)
p.Append("test01.ts", 5.0, "")
p.Append("test02.ts", 6.0, "")
p.Close()
fmt.Printf("%s\n", p)

Output:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-TARGETDURATION:6
#EXTINF:5.000,
test01.ts
#EXTINF:6.000,
test02.ts
#EXT-X-ENDLIST

func NewMediaPlaylist Uses

func NewMediaPlaylist(winsize uint, capacity uint) (*MediaPlaylist, error)

NewMediaPlaylist Creates new media playlist structure. Winsize defines how much items will displayed on playlist generation. Capacity is total size of a playlist.

func (*MediaPlaylist) Append Uses

func (p *MediaPlaylist) Append(uri string, duration float64, title string) error

Append general chunk to the tail of chunk slice for a media playlist. This operation does reset playlist cache.

func (*MediaPlaylist) AppendSegment Uses

func (p *MediaPlaylist) AppendSegment(seg *MediaSegment) error

AppendSegment appends a MediaSegment to the tail of chunk slice for a media playlist. This operation does reset playlist cache.

func (*MediaPlaylist) Close Uses

func (p *MediaPlaylist) Close()

Close sliding playlist and make them fixed.

func (*MediaPlaylist) Count Uses

func (p *MediaPlaylist) Count() uint

Count tells us the number of items that are currently in the media playlist

func (*MediaPlaylist) Decode Uses

func (p *MediaPlaylist) Decode(data bytes.Buffer, strict bool) error

Decode parses a media playlist passed from the buffer. If `strict` parameter is true then return first syntax error.

func (*MediaPlaylist) DecodeFrom Uses

func (p *MediaPlaylist) DecodeFrom(reader io.Reader, strict bool) error

DecodeFrom parses a media playlist passed from the io.Reader stream. If `strict` parameter is true then it returns first syntax error.

func (*MediaPlaylist) DurationAsInt Uses

func (p *MediaPlaylist) DurationAsInt(yes bool)

DurationAsInt TargetDuration will be int on Encode

Example of parsing a playlist with EXT-X-DISCONTINIUTY tag and output it with integer segment durations.

Code:

f, _ := os.Open("example/playlist/media-playlist-with-discontinuity.m3u8")
p, _, _ := m3u8.DecodeFrom(bufio.NewReader(f), true)
pp := p.(*m3u8.MediaPlaylist)
pp.DurationAsInt(true)
fmt.Printf("%s", pp)

Output:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-TARGETDURATION:10
#EXTINF:10,
ad0.ts
#EXTINF:8,
ad1.ts
#EXT-X-DISCONTINUITY
#EXTINF:10,
movieA.ts
#EXTINF:10,
movieB.ts

func (*MediaPlaylist) Encode Uses

func (p *MediaPlaylist) Encode() *bytes.Buffer

Encode Generate output in M3U8 format. Marshal `winsize` elements from bottom of the `segments` queue.

func (*MediaPlaylist) Last Uses

func (p *MediaPlaylist) Last() uint

Last returns the previously written segment's index

func (*MediaPlaylist) Remove Uses

func (p *MediaPlaylist) Remove() (err error)

Remove current segment from the head of chunk slice form a media playlist. Useful for sliding playlists. This operation does reset playlist cache.

func (*MediaPlaylist) ResetCache Uses

func (p *MediaPlaylist) ResetCache()

ResetCache Reset playlist cache. Next called Encode() will regenerate playlist from the chunk slice.

func (*MediaPlaylist) SetCapacity Uses

func (p *MediaPlaylist) SetCapacity(capacity uint) error

SetCapacity resize the capacity of playlist's segments slice.

func (*MediaPlaylist) SetDefaultKey Uses

func (p *MediaPlaylist) SetDefaultKey(method, uri, iv, keyformat, keyformatversions string) error

SetDefaultKey Set encryption key appeared once in header of the playlist (pointer to MediaPlaylist.Key). It useful when keys not changed during playback. Set tag for the whole list.

func (*MediaPlaylist) SetDefaultMap Uses

func (p *MediaPlaylist) SetDefaultMap(uri string, limit, offset int64)

SetDefaultMap Set map appeared once in header of the playlist (pointer to MediaPlaylist.Key). It useful when map not changed during playback. Set tag for the whole list.

func (*MediaPlaylist) SetDiscontinuity Uses

func (p *MediaPlaylist) SetDiscontinuity() error

SetDiscontinuity Set discontinuity flag for the current media segment. EXT-X-DISCONTINUITY indicates an encoding discontinuity between the media segment that follows it and the one that preceded it (i.e. file format, number and type of tracks, encoding parameters, encoding sequence, timestamp sequence).

func (*MediaPlaylist) SetIframeOnly Uses

func (p *MediaPlaylist) SetIframeOnly()

SetIframeOnly Mark medialist as consists of only I-frames (Intra frames). Set tag for the whole list.

func (*MediaPlaylist) SetKey Uses

func (p *MediaPlaylist) SetKey(method, uri, iv, keyformat, keyformatversions string) error

SetKey Set encryption key for the current segment of media playlist (pointer to Segment.Key)

func (*MediaPlaylist) SetMap Uses

func (p *MediaPlaylist) SetMap(uri string, limit, offset int64) error

SetMap Set encryption key for the current segment of media playlist (pointer to Segment.Key)

func (*MediaPlaylist) SetProgramDateTime Uses

func (p *MediaPlaylist) SetProgramDateTime(value time.Time) error

SetProgramDateTime Set program date and time for the current media segment. EXT-X-PROGRAM-DATE-TIME tag associates the first sample of a media segment with an absolute date and/or time. It applies only to the current media segment. Date/time format is YYYY-MM-DDThh:mm:ssZ (ISO8601) and includes time zone.

func (*MediaPlaylist) SetRange Uses

func (p *MediaPlaylist) SetRange(limit, offset int64) error

SetRange Set limit and offset for the current media segment (EXT-X-BYTERANGE support for protocol version 4).

func (*MediaPlaylist) SetSCTE Uses

func (p *MediaPlaylist) SetSCTE(cue string, id string, time float64) error

SetSCTE set SCTE

func (*MediaPlaylist) SetVersion Uses

func (p *MediaPlaylist) SetVersion(ver uint8)

SetVersion sets the playlist version number, note the version maybe changed automatically by other Set methods.

func (*MediaPlaylist) SetWinSize Uses

func (p *MediaPlaylist) SetWinSize(winsize uint) error

SetWinSize overwrites the playlist's window size.

func (*MediaPlaylist) Slide Uses

func (p *MediaPlaylist) Slide(uri string, duration float64, title string)

Slide Combines two operations: firstly it removes one chunk from the head of chunk slice and move pointer to next chunk. Secondly it appends one chunk to the tail of chunk slice. Useful for sliding playlists. This operation does reset cache.

func (*MediaPlaylist) String Uses

func (p *MediaPlaylist) String() string

String For compatibility with Stringer interface For example fmt.Printf("%s", sampleMediaList) will encode playist and print its string representation.

Create new media playlist Add two segments to media playlist Print it

Code:

p, _ := m3u8.NewMediaPlaylist(1, 2)
p.Append("test01.ts", 5.0, "")
p.Append("test02.ts", 6.0, "")
fmt.Printf("%s\n", p)

Output:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-TARGETDURATION:6
#EXTINF:5.000,
test01.ts

func (*MediaPlaylist) Version Uses

func (p *MediaPlaylist) Version() uint8

Version returns the current playlist version number

func (*MediaPlaylist) WinSize Uses

func (p *MediaPlaylist) WinSize() uint

WinSize returns the playlist's window size.

type MediaSegment Uses

type MediaSegment struct {
    SeqID           uint64
    Title           string // optional second parameter for EXTINF tag
    URI             string
    Duration        float64   // first parameter for EXTINF tag; duration must be integers if protocol version is less than 3 but we are always keep them float
    Limit           int64     // EXT-X-BYTERANGE <n> is length in bytes for the file under URI
    Offset          int64     // EXT-X-BYTERANGE [@o] is offset from the start of the file under URI
    Key             *Key      // EXT-X-KEY displayed before the segment and means changing of encryption key (in theory each segment may have own key)
    Map             *Map      // EXT-X-MAP displayed before the segment
    Discontinuity   bool      // EXT-X-DISCONTINUITY indicates an encoding discontinuity between the media segment that follows it and the one that preceded it (i.e. file format, number and type of tracks, encoding parameters, encoding sequence, timestamp sequence)
    SCTE            *SCTE     // EXT-SCTE35 used for Ad signaling in HLS
    ProgramDateTime time.Time // EXT-X-PROGRAM-DATE-TIME tag associates the first sample of a media segment with an absolute date and/or time
}

MediaSegment This structure represents a media segment included in a media playlist. Media segment may be encrypted. Widevine supports own tags for encryption metadata.

type MediaType Uses

type MediaType uint

MediaType for EXT-X-PLAYLIST-TYPE tag

const (

    // EVENT type
    EVENT MediaType = iota + 1
    // VOD type
    VOD
)

type Playlist Uses

type Playlist interface {
    Encode() *bytes.Buffer
    Decode(bytes.Buffer, bool) error
    DecodeFrom(reader io.Reader, strict bool) error
    String() string
}

Playlist Interface applied to various playlist types.

func Decode Uses

func Decode(data bytes.Buffer, strict bool) (Playlist, ListType, error)

Decode detects type of playlist and decodes it. It accepts bytes buffer as input.

func DecodeFrom Uses

func DecodeFrom(reader io.Reader, strict bool) (Playlist, ListType, error)

DecodeFrom detects type of playlist and decodes it. It accepts data conformed with io.Reader.

type SCTE Uses

type SCTE struct {
    Cue  string
    ID   string
    Time float64
}

SCTE SCTE struct

type Variant Uses

type Variant struct {
    URI       string
    Chunklist *MediaPlaylist
    VariantParams
}

Variant This structure represents variants for master playlist. Variants included in a master playlist and point to media playlists.

type VariantParams Uses

type VariantParams struct {
    ProgramID    uint32
    Bandwidth    uint32
    Codecs       string
    Resolution   string
    Audio        string // EXT-X-STREAM-INF only
    Video        string
    Subtitles    string         // EXT-X-STREAM-INF only
    Captions     string         // EXT-X-STREAM-INF only
    Name         string         // EXT-X-STREAM-INF only (non standard Wowza/JWPlayer extension to name the variant/quality in UA)
    Iframe       bool           // EXT-X-I-FRAME-STREAM-INF
    Alternatives []*Alternative // EXT-X-MEDIA
}

VariantParams This structure represents additional parameters for a variant used in EXT-X-STREAM-INF and EXT-X-I-FRAME-STREAM-INF

type WV Uses

type WV struct {
    AudioChannels          uint
    AudioFormat            uint
    AudioProfileIDC        uint
    AudioSampleSize        uint
    AudioSamplingFrequency uint
    CypherVersion          string
    ECM                    string
    VideoFormat            uint
    VideoFrameRate         uint
    VideoLevelIDC          uint
    VideoProfileIDC        uint
    VideoResolution        string
    VideoSAR               string
}

WV This structure represents metadata for Google Widevine playlists. This format not described in IETF draft but provied by Widevine Live Packager as additional tags with #WV-prefix.

Package m3u8 imports 9 packages (graph). Updated 2017-03-22. Refresh now. Tools for package owners.