playlist formats: comments are specs only

This commit is contained in:
4DBug committed 2026-11-01 01:52:39 -05:00
1 parent 6f8456246f
commit 6c0c6daeb4
3 files changed
+34 -72

No files matched your search

+15 -31
View File
@@ -1,16 +1,7 @@
--!strict
-- playlist/m3u: M3U / M3U8 and HLS (RFC 8216) playlists. Absorbed from playlist-luau 3fc7fd3 (same author, no
-- third-party code). Decode: #EXTM3U / #EXTINF (durations, titles with attributes and commas), #PLAYLIST, plain
-- location lists, a BOM, CRLF / CR, Windows-1252 text; HLS master playlists (variants with BANDWIDTH / CODECS /
-- attributes, EXT-X-MEDIA renditions) and media playlists (segments, durations, byte ranges, target duration, media
-- sequence, end list, encryption, the segment format). Locations resolve against opts.baseUrl (RFC 3986,
-- codecs/text/Url); Windows / UNC paths stay as they are and are flagged unplayable, as are relative paths without a base
-- and non-HTTP schemes (mp3 HLS segments are playable; .ts / .aac / fMP4 are flagged). Encode: M3U8 (#EXTM3U,
-- #EXTINF, #PLAYLIST). Not supported: writing HLS tags.
--
-- local pl = M3u.decode(Bytes.reader(buf), { baseUrl = "https://example.com/music/list.m3u" })
-- for _, e in pl.entries do print(e.url, e.title, e.duration, e.playable) end -- pl.format: "m3u" | "hls"
-- local out = M3u.encode(pl, { title = "My Mix" })
-- playlist/m3u: M3U / M3U8 and HLS (RFC 8216) playlists, absorbed from playlist-luau 3fc7fd3 (same author). Supports:
-- #EXTM3U / #EXTINF / #PLAYLIST, plain lists, BOM, CRLF / CR, Windows-1252, HLS master and media playlists; locations
-- resolve per RFC 3986; mp3 HLS segments are playable, .ts / .aac / fMP4 are flagged. Not supported: writing HLS tags.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
@@ -34,13 +25,12 @@ local M3u = {
export type Entry = {
url: string,
title: string?,
duration: number?, -- seconds; nil when unknown (-1 in the file)
playable: boolean, -- an http(s) URL the player can fetch
reason: string?, -- why not playable: "local path", "relative path without a base URL", "scheme ftp", ...
-- HLS
bandwidth: number?, -- master: BANDWIDTH
codecs: string?, -- master: CODECS
attributes: { [string]: string }?, -- master: the whole EXT-X-STREAM-INF / EXT-X-MEDIA attribute list
duration: number?, -- seconds; -1 in the file = unknown
playable: boolean,
reason: string?,
bandwidth: number?, -- BANDWIDTH
codecs: string?, -- CODECS
attributes: { [string]: string }?, -- EXT-X-STREAM-INF / EXT-X-MEDIA attribute list
byteRange: { length: number, offset: number? }?, -- media: EXT-X-BYTERANGE
}
@@ -48,10 +38,10 @@ export type Hls = {
master: boolean,
targetDuration: number?,
mediaSequence: number?,
endList: boolean, -- media playlist: EXT-X-ENDLIST seen (false = live, the playlist grows)
endList: boolean, -- EXT-X-ENDLIST (false = live)
encrypted: boolean, -- EXT-X-KEY with a METHOD other than NONE
totalDuration: number?, -- media: sum of the segment durations
segmentFormat: string?, -- media: the segments' common extension ("mp3", "ts", "aac", ...) or "mixed"
totalDuration: number?,
segmentFormat: string?,
}
export type Result = {
@@ -60,13 +50,13 @@ export type Result = {
meta: { [string]: any },
entries: { Entry },
warnings: { string },
title: string?, -- #PLAYLIST: / PLS has none
title: string?, -- #PLAYLIST:
hls: Hls?,
}
export type DecodeOptions = {
baseUrl: string?, -- the playlist's own URL (or Windows path), for relative entries
maxBytes: number?, -- refuse bigger files (default 64 MB)
baseUrl: string?,
maxBytes: number?, -- default 64 MB
}
export type EncodeOptions = {
title: string?, -- #PLAYLIST: (default: the playlist's title)
@@ -75,7 +65,6 @@ export type EncodeOptions = {
-- ------------------------------------------------------------------------------------------------ constants
local UNSUPPORTED_SEGMENTS = { ts = true, aac = true, m4s = true, mp4 = true, m4a = true, fmp4 = true, cmfa = true }
-- The extension of a URL's path (lower case), or nil
local function extension(url: string): string?
local p = Url.split(url).path
local ext = string.match(p, "%.([%w]+)$")
@@ -301,7 +290,6 @@ local function parseHls(ls: { string }, base: string?, baseScheme: string?, warn
return res
end
-- decode(r, opts?) -> Types.Playlist | nil, Err. Bad lines become warnings (pl.warnings), never errors.
function M3u.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Playlist?, Err.Err?)
local n = r.limit - r.pos
if n > (opts and opts.maxBytes or 2 ^ 26) then
@@ -326,8 +314,6 @@ function M3u.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Playlist?, Er
end
-- ------------------------------------------------------------------------------------------------ encode
-- Writes entries as M3U8 (UTF-8, #EXTM3U, #EXTINF:<seconds>,<title>). Durations are rounded; unknown = -1.
-- opts.title adds #PLAYLIST:. Line breaks inside titles and URLs become spaces.
local function serialize(entries: { any }, opts: { title: string? }?): string
local out = { "#EXTM3U" }
local function clean(s: string): string
@@ -350,8 +336,6 @@ local function serialize(entries: { any }, opts: { title: string? }?): string
return table.concat(out, "\n") .. "\n"
end
-- encode(playlist, opts?) -> buffer | nil, Err: M3U8 text. playlist: a Types.Playlist (any format's) or a list of
-- entries ({ url, title?, duration? })
function M3u.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
if type(value) ~= "table" then
return nil, Err.new(Err.ARG, "encode: needs a Playlist or a list of entries")
+15 -24
View File
@@ -1,12 +1,7 @@
--!strict
-- playlist/pls: PLS playlists ([playlist], FileN / TitleN / LengthN, NumberOfEntries, Version). Absorbed from
-- playlist-luau 3fc7fd3 (same author, no third-party code; the parser), plus a new writer. Decode: keys in any case
-- and order, comments, a BOM, CRLF / CR, Windows-1252 text, gaps in the numbering (entries sort by number), a
-- NumberOfEntries mismatch as a warning; locations resolve against opts.baseUrl (RFC 3986, codecs/text/Url), local /
-- relative / non-HTTP locations are flagged unplayable. Encode: Version=2, Length -1 for unknown durations.
--
-- local pl = Pls.decode(Bytes.reader(buf), { baseUrl = "https://radio.example/list.pls" })
-- local out = Pls.encode(pl)
-- playlist/pls: PLS playlists ([playlist], FileN / TitleN / LengthN, NumberOfEntries, Version), absorbed from
-- playlist-luau 3fc7fd3 (same author) plus a new writer. Supports: keys in any case / order, comments, BOM, CRLF / CR,
-- Windows-1252, numbering gaps; locations resolve per RFC 3986. Encode: Version=2, Length -1 for unknown durations.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
@@ -30,13 +25,12 @@ local Pls = {
export type Entry = {
url: string,
title: string?,
duration: number?, -- seconds; nil when unknown (-1 in the file)
playable: boolean, -- an http(s) URL the player can fetch
reason: string?, -- why not playable: "local path", "relative path without a base URL", "scheme ftp", ...
-- HLS
bandwidth: number?, -- master: BANDWIDTH
codecs: string?, -- master: CODECS
attributes: { [string]: string }?, -- master: the whole EXT-X-STREAM-INF / EXT-X-MEDIA attribute list
duration: number?, -- seconds; -1 in the file = unknown
playable: boolean,
reason: string?,
bandwidth: number?, -- BANDWIDTH
codecs: string?, -- CODECS
attributes: { [string]: string }?, -- EXT-X-STREAM-INF / EXT-X-MEDIA attribute list
byteRange: { length: number, offset: number? }?, -- media: EXT-X-BYTERANGE
}
@@ -44,10 +38,10 @@ export type Hls = {
master: boolean,
targetDuration: number?,
mediaSequence: number?,
endList: boolean, -- media playlist: EXT-X-ENDLIST seen (false = live, the playlist grows)
endList: boolean, -- EXT-X-ENDLIST (false = live)
encrypted: boolean, -- EXT-X-KEY with a METHOD other than NONE
totalDuration: number?, -- media: sum of the segment durations
segmentFormat: string?, -- media: the segments' common extension ("mp3", "ts", "aac", ...) or "mixed"
totalDuration: number?,
segmentFormat: string?,
}
export type Result = {
@@ -56,13 +50,13 @@ export type Result = {
meta: { [string]: any },
entries: { Entry },
warnings: { string },
title: string?, -- #PLAYLIST: / PLS has none
title: string?, -- #PLAYLIST:
hls: Hls?,
}
export type DecodeOptions = {
baseUrl: string?, -- the playlist's own URL (or Windows path), for relative entries
maxBytes: number?, -- refuse bigger files (default 64 MB)
baseUrl: string?,
maxBytes: number?, -- default 64 MB
}
-- ------------------------------------------------------------------------------------------------ constants
@@ -129,7 +123,6 @@ local function parsePls(ls: { string }, base: string?, baseScheme: string?, warn
return res
end
-- decode(r, opts?) -> Types.Playlist | nil, Err. Bad lines become warnings (pl.warnings), never errors.
function Pls.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Playlist?, Err.Err?)
local n = r.limit - r.pos
if n > (opts and opts.maxBytes or 2 ^ 26) then
@@ -153,8 +146,6 @@ function Pls.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Playlist?, Er
end
-- ------------------------------------------------------------------------------------------------ encode
-- encode(playlist, opts?) -> buffer | nil, Err: PLS text (Version=2). playlist: a Types.Playlist (any format's) or a
-- list of entries ({ url, title?, duration? }). Line breaks inside titles and URLs become spaces.
function Pls.encode(value: any, _opts: any?): (buffer?, Err.Err?)
if type(value) ~= "table" then
return nil, Err.new(Err.ARG, "encode: needs a Playlist or a list of entries")
+4 -17
View File
@@ -1,15 +1,7 @@
--!strict
-- playlist/xspf: XSPF 1 ("spiff", https://xspf.org/spec) playlists on codecs/text/Xml. New in rosetta (playlist-luau
-- had no XSPF). Decode: the playlist's title / creator / annotation / info / location / identifier / image / date,
-- every track's location (the first one is the entry url, all of them in entry.locations), title, creator, album,
-- annotation, info, image, trackNum and duration (milliseconds -> seconds), xml:base on the playlist and on tracks;
-- locations resolve against xml:base, then opts.baseUrl (RFC 3986, codecs/text/Url), and are classified like the other
-- playlist formats (local / relative / non-HTTP locations are unplayable). Encode: XSPF version 1 with those fields.
-- playlist/xspf: XSPF 1 (https://xspf.org/spec) playlists on codecs/text/Xml. Supports: playlist and track metadata,
-- every track location (entry.locations), duration (ms), xml:base then opts.baseUrl (RFC 3986).
-- Not supported: extension / link / meta elements (skipped), XSPF version 0 differences.
--
-- local pl = Xspf.decode(Bytes.reader(buf), { baseUrl = "https://example.com/list.xspf" })
-- for _, e in pl.entries do print(e.url, e.title, e.creator, e.duration) end
-- local out = Xspf.encode(pl)
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
@@ -30,8 +22,8 @@ local Xspf = {
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = {
baseUrl: string?, -- the playlist's own URL (or Windows path), for relative locations
maxBytes: number?, -- refuse bigger files (default 64 MB)
baseUrl: string?,
maxBytes: number?, -- default 64 MB
}
-- ------------------------------------------------------------------------------------------------ constants
@@ -43,7 +35,6 @@ local function trim(s: string): string
return (string.match(s, "^%s*(.-)%s*$") :: string)
end
-- the text of the first child element with this local name, trimmed (nil when missing or empty)
local function field(node: any, name: string): string?
for _, c in node.children do
if type(c) == "table" and Xml.localName(c.name) == name then
@@ -75,8 +66,6 @@ function Xspf.sniff(r: Bytes.Reader): number
end
-- ------------------------------------------------------------------------------------------------ decode
-- decode(r, opts?) -> Types.Playlist | nil, Err (malformed XML or a root that is not <playlist> is an error; odd
-- tracks become warnings)
function Xspf.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Playlist?, Err.Err?)
local n = r.limit - r.pos
if n > (opts and opts.maxBytes or 2 ^ 26) then
@@ -161,8 +150,6 @@ function Xspf.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Playlist?, E
end
-- ------------------------------------------------------------------------------------------------ encode
-- encode(playlist, opts?) -> buffer | nil, Err: XSPF version 1. playlist: a Types.Playlist (any format's) or a list
-- of entries ({ url, title?, duration?, creator?, album?, ... }). opts.indent (default 2) pretty-prints.
function Xspf.encode(value: any, opts: { indent: number?, title: string? }?): (buffer?, Err.Err?)
if type(value) ~= "table" then
return nil, Err.new(Err.ARG, "encode: needs a Playlist or a list of entries")