M3U8::Codecs
Codecs represents the CODECS attribute used in HTTP Live Streaming (HLS).
In HLS (RFC 8216), the CODECS attribute (further detailed in RFC 6381)
is a comma-separated list of codec identifiers that describe the media
contained in a Media Segment. This attribute is used in the EXT-X-STREAM-INF
tag to indicate what codecs (and profiles) are required to play back a stream.
#EXT-X-STREAM-INF:BANDWIDTH=65000,CODECS="mp4a.40.5"
audio-only.m3u8
There are two primary ways to use Codecs:
-
If the
codecsproperty is set, that pre-computed string is used directly. -
Otherwise, the
codecsstring is constructed by deriving the video and audio codec codes from the remaining properties:- For audio, the
audio_codecproperty is mapped as follows:
- For audio, the
"aac-lc" => "mp4a.40.2" (AAC low complexity)
"he-aac" => "mp4a.40.5" (HE-AAC)
"mp3" => "mp4a.40.34" (MPEG-1 Audio Layer 3)
- For video, both the
profileandlevelproperties must be provided. For example, for H.264 (AVC) video the following mappings are used:
Baseline Profile:
Level 3.0 => "avc1.66.30"
Level 3.1 => "avc1.42001f"
Main Profile:
Level 3.0 => "avc1.77.30"
Level 3.1 => "avc1.4d001f"
Level 4.0 => "avc1.4d0028"
Level 4.1 => "avc1.4d0029"
High Profile:
Level 3.1 => "avc1.64001f"
Level 4.0 => "avc1.640028"
Level 4.1 => "avc1.640029"
When constructing the codec string:
- If a video codec is expected (i.e. both
profileandlevelare provided) but the mapping is not recognized, no codec string is output. - Likewise, if an
audio_codecis specified but unrecognized, the codec string is empty.
For more details on these mappings and the overall semantics of HLS, refer to:
- RFC 8216 (HTTP Live Streaming) Section 4.3.4.2
- RFC 6381 (The 'Codecs' and 'Profiles' Parameters for "Bucket" Media Types)
- The HTTP Live Streaming FAQ (which discusses recommended values for stream playback)
Constructors
Creates a new Codecs instance from a NamedTuple.
Examples:
options = {audio_codec: "aac-lc"}
Codecs.new(options)
Codecs.new(audio_codec: "aac-lc")
Initializes a new Codecs instance.
Parameters with @ prefix are automatically assigned as instance variables.
Parameters:
codecs: A pre-computed codec string (if provided, it is used directly).audio_codec: The name of the audio codec (e.g.aac-lc,he-aac,mp3).level: The numeric level used to determine the video codec.profile: The video profile (e.g.baseline,main,high).
Example:
Codecs.new
Instance methods
Compares this Codecs instance with another Codecs instance.
Equality is determined by comparing their CODECS strings.
Example:
left = Codecs.new(audio_codec: "aac-lc")
right = Codecs.new(audio_codec: "aac-lc")
left == right # => true
Compares this Codecs instance with a String.
The instance is considered equal to the string if its CODECS string matches.
Example:
left = Codecs.new(audio_codec: "aac-lc")
right = "aac-lc"
left == right # => true
Returns true if the codecs string is empty.
Examples:
codecs = Codecs.new
codecs.empty? # => true
codecs.audio_codec = "aac-lc"
codecs.empty? # => false
Video level (e.g., 3.0, 3.1, etc.)
The video codecs string is determined from both the video profile and level.
-
baselineProfile:- Level
3.0=>avc1.66.30 - Level
3.1=>avc1.42001f
- Level
-
mainProfile:- Level
3.0=>avc1.77.30 - Level
3.1=>avc1.4d001f - Level
4.0=>avc1.4d0028 - Level
4.1=>avc1.4d0029
- Level
-
highProfile:- Level
3.1=>avc1.64001f - Level
4.0=>avc1.640028 - Level
4.1=>avc1.640029
- Level
Video level (e.g., 3.0, 3.1, etc.)
The video codecs string is determined from both the video profile and level.
-
baselineProfile:- Level
3.0=>avc1.66.30 - Level
3.1=>avc1.42001f
- Level
-
mainProfile:- Level
3.0=>avc1.77.30 - Level
3.1=>avc1.4d001f - Level
4.0=>avc1.4d0028 - Level
4.1=>avc1.4d0029
- Level
-
highProfile:- Level
3.1=>avc1.64001f - Level
4.0=>avc1.640028 - Level
4.1=>avc1.640029
- Level
Video profile (e.g., baseline, main, high)
The video codecs string is determined from both the video profile and level.
-
baselineProfile:- Level
3.0=>avc1.66.30 - Level
3.1=>avc1.42001f
- Level
-
mainProfile:- Level
3.0=>avc1.77.30 - Level
3.1=>avc1.4d001f - Level
4.0=>avc1.4d0028 - Level
4.1=>avc1.4d0029
- Level
-
highProfile:- Level
3.1=>avc1.64001f - Level
4.0=>avc1.640028 - Level
4.1=>avc1.640029
- Level
Video profile (e.g., baseline, main, high)
The video codecs string is determined from both the video profile and level.
-
baselineProfile:- Level
3.0=>avc1.66.30 - Level
3.1=>avc1.42001f
- Level
-
mainProfile:- Level
3.0=>avc1.77.30 - Level
3.1=>avc1.4d001f - Level
4.0=>avc1.4d0028 - Level
4.1=>avc1.4d0029
- Level
-
highProfile:- Level
3.1=>avc1.64001f - Level
4.0=>avc1.640028 - Level
4.1=>avc1.640029
- Level
Returns the CODECS string for this instance.
If a pre-computed codecs string is provided, it is returned directly.
Otherwise, the method constructs the codecs string by computing the video codec
(from profile and level) and the audio codec (from audio_codec).
Examples:
Codecs.new(codecs: "test").to_s # => "test"
Codecs.new(audio_codec: "aac-lc").to_s # => "mp4a.40.2"
Codecs.new(profile: "baseline", level: 3.0, audio_codec: "mp3").to_s # => "avc1.66.30,mp4a.40.34"