class M3U8::SegmentItem

Overview

SegmentItem represents a media segment in an HLS playlist.

It encapsulates the information provided by the EXTINF tag followed by the media segment URI.

According to RFC 8216, Section 4.3.2.1, The EXTINF tag specifies the #duration of a Media Segment and may include an optional title or #comment. It applies only to the next Media Segment.

Its format is:

#EXTINF:<duration>,[<title>]

For example:

#EXTINF:10.991,anything

This indicates that the following media segment has a #duration of 10.991 seconds with an optional #comment "anything".

In addition, a SegmentItem may optionally include:

Properties:

Examples:

Creating a SegmentItem instance:

options = {
  duration:  10.991,
  segment:   "test.ts",
  comment:   "anything",
  byterange: {length: 4500, start: 600},
}
SegmentItem.new(options)
# => #<M3U8::SegmentItem:0x7e310e075cc0
#     @byterange=#<M3U8::ByteRange:0x7e310f201c00 @length=4500, @start=600>,
#     @comment="anything",
#     @duration=10.991,
#     @program_date_time=#<M3U8::TimeItem:0x7e310e0761b0 @time=1970-01-01 00:00:00.0 UTC>,
#     @segment="test.ts">

SegmentItem.new(10.991, "test.ts", "anything", "4500@600", "2010-02-19T14:54:23.031Z")
# => #<M3U8::SegmentItem:0x7e6830943b40
#     @byterange=#<M3U8::ByteRange:0x7e6831acf990 @length=4500, @start=600>,
#     @comment="anything",
#     @duration=10.991,
#     @program_date_time=#<M3U8::TimeItem:0x7e6830949cf0 @time=2010-02-19 14:54:23.031000000 UTC>,
#     @segment="test.ts">

Convert to a string representation of the SegmentItem:

SegmentItem.new(10.991, "test.ts", "anything", "4500@600", "2010-02-19T14:54:23.031Z").to_s
# => "#EXTINF:10.991,anything\n" +
#    "#EXT-X-BYTERANGE:4500@600\n" +
#    "#EXT-X-PROGRAM-DATE-TIME:2010-02-19T14:54:23.031Z\n" +
#    "test.ts"

Included Modules

Extended Modules

Defined in:

m3u8/segment_item.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(params : NamedTuple = NamedTuple.new) #

Constructs a new SegmentItem from a NamedTuple.

The NamedTuple may include the following keys:

Example:

options = {
  duration:          10.991,
  segment:           "test.ts",
  comment:           "anything",
  byterange:         {length: 4500, start: 600},
  program_date_time: "2010-02-19T14:54:23.031Z",
}
SegmentItem.new(options)
# => #<M3U8::SegmentItem:0x7e756d37b7c0
#     @byterange=#<M3U8::ByteRange:0x7e756e5074b0 @length=4500, @start=600>,
#     @comment="anything",
#     @duration=10.991,
#     @program_date_time=#<M3U8::TimeItem:0x7e756d386c60 @time=2010-02-19 14:54:23.031000000 UTC>,
#     @segment="test.ts">

[View source]
def self.new(duration : Float64 | Nil = nil, segment : Nil | String = nil, comment : Nil | String = nil, byterange = nil, program_date_time = nil) #

Initializes a new SegmentItem instance.

The parameters are assigned to the corresponding properties. The #byterange (Byterange) and #program_date_time (TimeItem) parameters are parsed using their respective parsing methods, allowing them to be provided in different forms (e.g. as a NamedTuple, string, or object).

Examples:

SegmentItem.new(10.991, "test.ts", "anything", "4500@600", "2010-02-19T14:54:23.031Z")
# => #<M3U8::SegmentItem:0x7e6830943b40
#     @byterange=#<M3U8::ByteRange:0x7e6831acf990 @length=4500, @start=600>,
#     @comment="anything",
#     @duration=10.991,
#     @program_date_time=#<M3U8::TimeItem:0x7e6830949cf0 @time=2010-02-19 14:54:23.031000000 UTC>,
#     @segment="test.ts">

SegmentItem.new(
  duration: 10.991,
  segment: "test.ts",
  comment: "anything",
  byterange: {length: 4500, start: 600},
  program_date_time: Time.parse_iso8601("2010-02-19T14:54:23.031Z"),
)
# => #<M3U8::SegmentItem:0x7ffa96913700
#     @byterange=#<M3U8::ByteRange:0x7ffa97a9f3f0 @length=4500, @start=600>,
#     @comment="anything",
#     @duration=10.991,
#     @program_date_time=#<M3U8::TimeItem:0x7ffa9691e7b0 @time=2010-02-19 14:54:23.031000000 UTC>,
#     @segment="test.ts">

[View source]

Instance Method Detail

def byterange : ByteRange #

The ByteRange specifying a sub-section of the segment (if provided).


[View source]
def byterange=(byterange) #

Setter for the #byterange property.

Allows setting the byterange attribute using a ByteRange object, a NamedTuple, or a string. The input is parsed using ByteRange.parse.

Example:

item = SegmentItem.new
item.byterange = ByteRange.new(length: 4500, start: 600)
item.byterange = {length: 4500, start: 600}
item.byterange = "4500@600"

item.byterange
# => #<M3U8::ByteRange:0x784ba59ea7e0 @length=4500, @start=600>

[View source]
def comment : String | Nil #

An optional comment from the EXTINF tag.


[View source]
def comment=(comment : String | Nil) #

An optional comment from the EXTINF tag.


[View source]
def duration : Float64 | Nil #

The required #duration from the EXTINF tag.


[View source]
def duration=(duration : Float64 | Nil) #

The required #duration from the EXTINF tag.


[View source]
def program_date_time : TimeItem #

The program date/time (TimeItem) associated with the segment (if provided).


[View source]
def program_date_time=(time) #

Setter for the #program_date_time property.

Allows setting the #program_date_time attribute using a TimeItem, Time, or ISO-8601 string. The input is parsed using TimeItem.parse.

Example:

item = SegmentItem.new
item.program_date_time = TimeItem.new("2010-02-19T14:54:23Z")
item.program_date_time = TimeItem.new(Time.iso8601("2010-02-19T14:54:23.031Z"))
item.program_date_time = Time.parse_iso8601("2010-02-19T14:54:23.031Z")
item.program_date_time = "2010-02-19T14:54:23.031Z"

item.program_date_time
# => #<M3U8::TimeItem:0x7f5e17fedea0 @time=2010-02-19 14:54:23.031000000 UTC>

[View source]
def segment : String | Nil #

The URI of the media segment.


[View source]
def segment=(segment : String | Nil) #

The URI of the media segment.


[View source]
def to_s #

Returns the string representation of the segment, including the EXTINF tag, an optional EXT-X-BYTERANGE tag, an optional EXT-X-PROGRAM-DATE-TIME tag, and the segment URI.

The components are joined with newline characters.

Example:

options = {
  duration:  10.991,
  segment:   "test.ts",
  comment:   "anything",
  byterange: "4500@600",
}
SegmentItem.new(options).to_s
# => "#EXTINF:10.991,anything\n" +
#    "#EXT-X-BYTERANGE:4500@600\n" +
#    "test.ts"

SegmentItem.new(10.991, "test.ts", "anything", "4500@600", "2010-02-19T14:54:23.031Z").to_s
# => "#EXTINF:10.991,anything\n" +
#    "#EXT-X-BYTERANGE:4500@600\n" +
#    "#EXT-X-PROGRAM-DATE-TIME:2010-02-19T14:54:23.031Z\n" +
#    "test.ts"

[View source]