m3u8

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2022 License: MIT Imports: 9 Imported by: 12

Documentation

Overview

Package m3u8 provides utilities for parsing and generating m3u8 playlists

Index

Constants

View Source
const (
	SessionKeyItemTag    = `#EXT-X-SESSION-KEY`
	KeyItemTag           = `#EXT-X-KEY`
	DiscontinuityItemTag = `#EXT-X-DISCONTINUITY`
	TimeItemTag          = `#EXT-X-PROGRAM-DATE-TIME`
	DateRangeItemTag     = `#EXT-X-DATERANGE`
	MapItemTag           = `#EXT-X-MAP`
	SessionDataItemTag   = `#EXT-X-SESSION-DATA`
	SegmentItemTag       = `#EXTINF`
	ByteRangeItemTag     = `#EXT-X-BYTERANGE`
	PlaybackStartTag     = `#EXT-X-START`
	MediaItemTag         = `#EXT-X-MEDIA`
	PlaylistItemTag      = `#EXT-X-STREAM-INF`
	PlaylistIframeTag    = `#EXT-X-I-FRAME-STREAM-INF`

	HeaderTag                = `#EXTM3U`
	FooterTag                = `#EXT-X-ENDLIST`
	TargetDurationTag        = `#EXT-X-TARGETDURATION`
	CacheTag                 = `#EXT-X-ALLOW-CACHE`
	DiscontinuitySequenceTag = `#EXT-X-DISCONTINUITY-SEQUENCE`
	IndependentSegmentsTag   = `#EXT-X-INDEPENDENT-SEGMENTS`
	PlaylistTypeTag          = `#EXT-X-PLAYLIST-TYPE`
	IFramesOnlyTag           = `#EXT-X-I-FRAMES-ONLY`
	MediaSequenceTag         = `#EXT-X-MEDIA-SEQUENCE`
	VersionTag               = `#EXT-X-VERSION`

	ByteRangeTag = "BYTERANGE"

	MethodTag            = "METHOD"
	URITag               = "URI"
	IVTag                = "IV"
	KeyFormatTag         = "KEYFORMAT"
	KeyFormatVersionsTag = "KEYFORMATVERSIONS"

	IDTag              = "ID"
	ClassTag           = "CLASS"
	StartDateTag       = "START-DATE"
	EndDateTag         = "END-DATE"
	DurationTag        = "DURATION"
	PlannedDurationTag = "PLANNED-DURATION"
	Scte35CmdTag       = "SCTE35-CMD"
	Scte35OutTag       = "SCTE35-OUT"
	Scte35InTag        = "SCTE35-IN"
	EndOnNextTag       = "END-ON-NEXT"

	TimeOffsetTag = "TIME-OFFSET"
	PreciseTag    = "PRECISE"

	DataIDTag   = "DATA-ID"
	ValueTag    = "VALUE"
	LanguageTag = "LANGUAGE"

	TypeTag              = "TYPE"
	GroupIDTag           = "GROUP-ID"
	AssocLanguageTag     = "ASSOC-LANGUAGE"
	NameTag              = "NAME"
	AutoSelectTag        = "AUTOSELECT"
	DefaultTag           = "DEFAULT"
	ForcedTag            = "FORCED"
	InStreamIDTag        = "INSTREAM-ID"
	CharacteristicsTag   = "CHARACTERISTICS"
	ChannelsTag          = "CHANNELS"
	StableRenditionIDTag = "STABLE-RENDITION-ID"

	ResolutionTag       = "RESOLUTION"
	ProgramIDTag        = "PROGRAM-ID"
	CodecsTag           = "CODECS"
	BandwidthTag        = "BANDWIDTH"
	AverageBandwidthTag = "AVERAGE-BANDWIDTH"
	FrameRateTag        = "FRAME-RATE"
	VideoTag            = "VIDEO"
	AudioTag            = "AUDIO"
	SubtitlesTag        = "SUBTITLES"
	ClosedCaptionsTag   = "CLOSED-CAPTIONS"
	HDCPLevelTag        = "HDCP-LEVEL"
	StableVariantIDTag  = "STABLE-VARIANT-ID"

	NoneValue = "NONE"
	YesValue  = "YES"
	NoValue   = "NO"
)

Variables

View Source
var (
	// AudioCodecMap maps audio codec to representation
	AudioCodecMap = map[string]string{
		"aac-lc": "mp4a.40.2",
		"he-aac": "mp4a.40.5",
		"mp3":    "mp4a.40.34",
	}

	// BaselineCodecMap maps baseline profile with levels to representation
	BaselineCodecMap = map[string]string{
		"3.0": "avc1.66.30",
		"3.1": "avc1.42001f",
	}

	// MainCodecMap maps main profile with levels to representation
	MainCodecMap = map[string]string{
		"3.0": "avc1.77.30",
		"3.1": "avc1.4d001f",
		"4.0": "avc1.4d0028",
		"4.1": "avc1.4d0029",
	}

	// HighCodecMap maps high profile with levels to representation
	HighCodecMap = map[string]string{
		"3.0": "avc1.64001e",
		"3.1": "avc1.64001f",
		"3.2": "avc1.640020",
		"4.0": "avc1.640028",
		"4.1": "avc1.640029",
		"4.2": "avc1.64002a",
		"5.0": "avc1.640032",
		"5.1": "avc1.640033",
		"5.2": "avc1.640034",
	}
)
View Source
var (
	// ErrPlaylistInvalid represents playlist error when playlist does not start with #EXTM3U
	ErrPlaylistInvalid = errors.New("invalid playlist, must start with #EXTM3U")

	// ErrPlaylistInvalidType represents playlist error when it's mixed between master and media playlist
	ErrPlaylistInvalidType = errors.New("invalid playlist, mixed master and media")

	// ErrResolutionInvalid represents error when a resolution is invalid
	ErrResolutionInvalid = errors.New("invalid resolution")

	// ErrBandwidthMissing represents error when a segment does not have bandwidth
	ErrBandwidthMissing = errors.New("missing bandwidth")

	// ErrBandwidthInvalid represents error when a bandwidth is invalid
	ErrBandwidthInvalid = errors.New("invalid bandwidth")

	// ErrSegmentItemInvalid represents error when a segment item is invalid
	ErrSegmentItemInvalid = errors.New("invalid segment item")

	// ErrPlaylistItemInvalid represents error when a playlist item is invalid
	ErrPlaylistItemInvalid = errors.New("invalid playlist item")
)

Functions

func FormatTime

func FormatTime(time time.Time) string

FormatTime returns a string in default m3u8 date time format

func ParseAttributes

func ParseAttributes(text string) map[string]string

ParseAttributes parses a text line in playlist and returns an attributes map

func ParseTime

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

ParseTime parses a string in default m3u8 date time format and returns time.Time

func Write

func Write(pl *Playlist) (string, error)

Write writes a playlist to a string

Types

type ByteRange

type ByteRange struct {
	Length *int
	Start  *int
}

ByteRange represents sub range of a resource

func NewByteRange

func NewByteRange(text string) (*ByteRange, error)

NewByteRange parses a text line in playlist file and returns a *ByteRange

func (*ByteRange) String

func (br *ByteRange) String() string

type DateRangeItem

type DateRangeItem struct {
	ID               string
	Class            *string
	StartDate        string
	EndDate          *string
	Duration         *float64
	PlannedDuration  *float64
	Scte35Cmd        *string
	Scte35Out        *string
	Scte35In         *string
	EndOnNext        bool
	ClientAttributes map[string]string
}

DateRangeItem represents a #EXT-X-DATERANGE tag

func NewDateRangeItem

func NewDateRangeItem(text string) (*DateRangeItem, error)

NewDateRangeItem parses a text line in playlist and returns a *DateRangeItem

func (*DateRangeItem) String

func (dri *DateRangeItem) String() string

type DiscontinuityItem

type DiscontinuityItem struct{}

DiscontinuityItem represents a EXT-X-DISCONTINUITY tag to indicate a discontinuity between the SegmentItems that proceed and follow it.

func NewDiscontinuityItem

func NewDiscontinuityItem() (*DiscontinuityItem, error)

NewDiscontinuityItem returns a *DiscontinuityItem

func (*DiscontinuityItem) String

func (di *DiscontinuityItem) String() string

type Encryptable

type Encryptable struct {
	Method            string
	URI               *string
	IV                *string
	KeyFormat         *string
	KeyFormatVersions *string
}

Encryptable is common representation for KeyItem and SessionKeyItem

func NewEncryptable

func NewEncryptable(attributes map[string]string) *Encryptable

NewEncryptable takes an attributes map and returns an *Encryptable

func (*Encryptable) String

func (e *Encryptable) String() string

type Item

type Item interface {
	String() string
}

Item represents an item in a playlist

type KeyItem

type KeyItem struct {
	Encryptable *Encryptable
}

KeyItem represents a set of EXT-X-KEY attributes

func NewKeyItem

func NewKeyItem(text string) (*KeyItem, error)

NewKeyItem parses a text line and returns a *KeyItem

func (*KeyItem) String

func (ki *KeyItem) String() string

type MapItem

type MapItem struct {
	URI       string
	ByteRange *ByteRange
}

MapItem represents a EXT-X-MAP tag which specifies how to obtain the Media Initialization Section

func NewMapItem

func NewMapItem(text string) (*MapItem, error)

NewMapItem parses a text line and returns a *MapItem

func (*MapItem) String

func (mi *MapItem) String() string

type MediaItem

type MediaItem struct {
	Type              string
	GroupID           string
	Name              string
	Language          *string
	AssocLanguage     *string
	AutoSelect        *bool
	Default           *bool
	Forced            *bool
	URI               *string
	InStreamID        *string
	Characteristics   *string
	Channels          *string
	StableRenditionId *string
}

MediaItem represents a set of EXT-X-MEDIA attributes

func NewMediaItem

func NewMediaItem(text string) (*MediaItem, error)

NewMediaItem parses a text line and returns a *MediaItem

func (*MediaItem) String

func (mi *MediaItem) String() string

type PlaybackStart

type PlaybackStart struct {
	TimeOffset float64
	Precise    *bool
}

PlaybackStart represents a #EXT-X-START tag and attributes

func NewPlaybackStart

func NewPlaybackStart(text string) (*PlaybackStart, error)

NewPlaybackStart parses a text line and returns a *PlaybackStart

func (*PlaybackStart) String

func (ps *PlaybackStart) String() string

type Playlist

type Playlist struct {
	Items                 []Item
	Version               *int
	Cache                 *bool
	Target                int
	Sequence              int
	DiscontinuitySequence *int
	Type                  *string
	IFramesOnly           bool
	IndependentSegments   bool
	Live                  bool
	Master                *bool
}

Playlist represents an m3u8 playlist, it can be a master playlist or a set of media segments

func NewPlaylist

func NewPlaylist() *Playlist

NewPlaylist returns a playlist with default target 10

func NewPlaylistWithItems

func NewPlaylistWithItems(items []Item) *Playlist

NewPlaylistWithItems returns a playlist with a list of items

func Read

func Read(reader io.Reader) (*Playlist, error)

Read reads text from an io.Reader and returns a playlist

func ReadFile

func ReadFile(path string) (*Playlist, error)

ReadFile reads text from a file and returns a playlist

func ReadString

func ReadString(text string) (*Playlist, error)

ReadString parses a text string and returns a playlist

func (*Playlist) AppendItem

func (pl *Playlist) AppendItem(item Item)

AppendItem appends an item to the playlist

func (*Playlist) Duration

func (pl *Playlist) Duration() float64

Duration returns duration of a media playlist

func (*Playlist) IsLive

func (pl *Playlist) IsLive() bool

IsLive checks if playlist is live (not vod)

func (*Playlist) IsMaster

func (pl *Playlist) IsMaster() bool

IsMaster checks if a playlist is a master playlist

func (*Playlist) IsValid

func (pl *Playlist) IsValid() bool

IsValid checks if a playlist is valid or not

func (*Playlist) ItemSize

func (pl *Playlist) ItemSize() int

ItemSize returns number of items in a playlist

func (*Playlist) PlaylistSize

func (pl *Playlist) PlaylistSize() int

PlaylistSize returns number of playlist items in a playlist

func (*Playlist) Playlists added in v0.1.2

func (pl *Playlist) Playlists() []*PlaylistItem

Playlists returns list of segment items in a playlist

func (*Playlist) SegmentSize

func (pl *Playlist) SegmentSize() int

SegmentSize returns number of segment items in a playlist

func (*Playlist) Segments added in v0.1.2

func (pl *Playlist) Segments() []*SegmentItem

Segments returns list of segment items in a playlist

func (*Playlist) String

func (pl *Playlist) String() string

type PlaylistItem

type PlaylistItem struct {
	Bandwidth int
	URI       string
	IFrame    bool

	Name             *string
	Width            *int
	Height           *int
	AverageBandwidth *int
	ProgramID        *string
	Codecs           *string
	AudioCodec       *string
	Profile          *string
	Level            *string
	Video            *string
	Audio            *string
	Subtitles        *string
	ClosedCaptions   *string
	FrameRate        *float64
	HDCPLevel        *string
	Resolution       *Resolution
	StableVariantID  *string
}

PlaylistItem represents a set of EXT-X-STREAM-INF or EXT-X-I-FRAME-STREAM-INF attributes

func NewPlaylistItem

func NewPlaylistItem(text string) (*PlaylistItem, error)

NewPlaylistItem parses a text line and returns a *PlaylistItem

func (*PlaylistItem) CodecsString

func (pi *PlaylistItem) CodecsString() string

CodecsString returns the string representation of codecs for a playlist item

func (*PlaylistItem) String

func (pi *PlaylistItem) String() string

type Resolution

type Resolution struct {
	Width  int
	Height int
}

Resolution represents a resolution for a playlist item, e.g: 1920x1080

func NewResolution

func NewResolution(text string) (*Resolution, error)

NewResolution parses a string and returns a *Resolution

func (*Resolution) String

func (r *Resolution) String() string

type SegmentItem

type SegmentItem struct {
	Duration        float64
	Segment         string
	Comment         *string
	ProgramDateTime *TimeItem
	ByteRange       *ByteRange
}

SegmentItem represents EXTINF attributes with the URI that follows, optionally allowing an EXT-X-BYTERANGE tag to be set.

func NewSegmentItem

func NewSegmentItem(text string) (*SegmentItem, error)

NewSegmentItem parses a text line and returns a *SegmentItem

func (*SegmentItem) String

func (si *SegmentItem) String() string

type SessionDataItem

type SessionDataItem struct {
	DataID   string
	Value    *string
	URI      *string
	Language *string
}

SessionDataItem represents a set of EXT-X-SESSION-DATA attributes

func NewSessionDataItem

func NewSessionDataItem(text string) (*SessionDataItem, error)

NewSessionDataItem parses a text line and returns a *SessionDataItem

func (*SessionDataItem) String

func (sdi *SessionDataItem) String() string

type SessionKeyItem

type SessionKeyItem struct {
	Encryptable *Encryptable
}

SessionKeyItem represents a set of EXT-X-SESSION-KEY attributes

func NewSessionKeyItem

func NewSessionKeyItem(text string) (*SessionKeyItem, error)

NewSessionKeyItem parses a text line and returns a *SessionKeyItem

func (*SessionKeyItem) String

func (ski *SessionKeyItem) String() string

type TimeItem

type TimeItem struct {
	Time time.Time
}

TimeItem represents EXT-X-PROGRAM-DATE-TIME

func NewTimeItem

func NewTimeItem(text string) (*TimeItem, error)

NewTimeItem parses a text line and returns a *TimeItem

func (*TimeItem) String

func (ti *TimeItem) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL