1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
package m3u8
import (
"fmt"
"strconv"
"strings"
)
// SegmentItem represents EXTINF attributes with the URI that follows,
// optionally allowing an EXT-X-BYTERANGE tag to be set.
type SegmentItem struct {
Duration float64
Segment string
Comment *string
ProgramDateTime *TimeItem
ByteRange *ByteRange
}
// NewSegmentItem parses a text line and returns a *SegmentItem
func NewSegmentItem(text string) (*SegmentItem, error) {
var si SegmentItem
line := strings.Replace(text, SegmentItemTag+":", "", -1)
line = strings.Replace(line, "\n", "", -1)
values := strings.Split(line, ",")
d, err := strconv.ParseFloat(values[0], 64)
if err != nil {
return nil, err
}
si.Duration = d
if len(values) > 1 && values[1] != "" {
si.Comment = &values[1]
}
return &si, nil
}
func (si *SegmentItem) String() string {
date := ""
if si.ProgramDateTime != nil {
date = fmt.Sprintf("%v\n", si.ProgramDateTime)
}
byteRange := ""
if si.ByteRange != nil {
byteRange = fmt.Sprintf("\n%s:%v", ByteRangeItemTag, si.ByteRange.String())
}
comment := ""
if si.Comment != nil {
comment = *si.Comment
}
return fmt.Sprintf("%s:%v,%s%s\n%s%s", SegmentItemTag, si.Duration, comment, byteRange, date, si.Segment)
}
|