playlist formats: comments are specs only
This commit is contained in:
1 parent
6f8456246f
commit
6c0c6daeb4
3 files changed
+34
-72
No files matched your search
@@ -1,16 +1,7 @@
|
|||||||
--!strict
|
--!strict
|
||||||
-- playlist/m3u: M3U / M3U8 and HLS (RFC 8216) playlists. Absorbed from playlist-luau 3fc7fd3 (same author, no
|
-- playlist/m3u: M3U / M3U8 and HLS (RFC 8216) playlists, absorbed from playlist-luau 3fc7fd3 (same author). Supports:
|
||||||
-- third-party code). Decode: #EXTM3U / #EXTINF (durations, titles with attributes and commas), #PLAYLIST, plain
|
-- #EXTM3U / #EXTINF / #PLAYLIST, plain lists, BOM, CRLF / CR, Windows-1252, HLS master and media playlists; locations
|
||||||
-- location lists, a BOM, CRLF / CR, Windows-1252 text; HLS master playlists (variants with BANDWIDTH / CODECS /
|
-- resolve per RFC 3986; mp3 HLS segments are playable, .ts / .aac / fMP4 are flagged. Not supported: writing HLS tags.
|
||||||
-- 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" })
|
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ requires
|
-- ------------------------------------------------------------------------------------------------ requires
|
||||||
local Err = require("@rosetta/core/Err")
|
local Err = require("@rosetta/core/Err")
|
||||||
@@ -34,13 +25,12 @@ local M3u = {
|
|||||||
export type Entry = {
|
export type Entry = {
|
||||||
url: string,
|
url: string,
|
||||||
title: string?,
|
title: string?,
|
||||||
duration: number?, -- seconds; nil when unknown (-1 in the file)
|
duration: number?, -- seconds; -1 in the file = unknown
|
||||||
playable: boolean, -- an http(s) URL the player can fetch
|
playable: boolean,
|
||||||
reason: string?, -- why not playable: "local path", "relative path without a base URL", "scheme ftp", ...
|
reason: string?,
|
||||||
-- HLS
|
bandwidth: number?, -- BANDWIDTH
|
||||||
bandwidth: number?, -- master: BANDWIDTH
|
codecs: string?, -- CODECS
|
||||||
codecs: string?, -- master: CODECS
|
attributes: { [string]: string }?, -- EXT-X-STREAM-INF / EXT-X-MEDIA attribute list
|
||||||
attributes: { [string]: string }?, -- master: the whole EXT-X-STREAM-INF / EXT-X-MEDIA attribute list
|
|
||||||
byteRange: { length: number, offset: number? }?, -- media: EXT-X-BYTERANGE
|
byteRange: { length: number, offset: number? }?, -- media: EXT-X-BYTERANGE
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,10 +38,10 @@ export type Hls = {
|
|||||||
master: boolean,
|
master: boolean,
|
||||||
targetDuration: number?,
|
targetDuration: number?,
|
||||||
mediaSequence: 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
|
encrypted: boolean, -- EXT-X-KEY with a METHOD other than NONE
|
||||||
totalDuration: number?, -- media: sum of the segment durations
|
totalDuration: number?,
|
||||||
segmentFormat: string?, -- media: the segments' common extension ("mp3", "ts", "aac", ...) or "mixed"
|
segmentFormat: string?,
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Result = {
|
export type Result = {
|
||||||
@@ -60,13 +50,13 @@ export type Result = {
|
|||||||
meta: { [string]: any },
|
meta: { [string]: any },
|
||||||
entries: { Entry },
|
entries: { Entry },
|
||||||
warnings: { string },
|
warnings: { string },
|
||||||
title: string?, -- #PLAYLIST: / PLS has none
|
title: string?, -- #PLAYLIST:
|
||||||
hls: Hls?,
|
hls: Hls?,
|
||||||
}
|
}
|
||||||
|
|
||||||
export type DecodeOptions = {
|
export type DecodeOptions = {
|
||||||
baseUrl: string?, -- the playlist's own URL (or Windows path), for relative entries
|
baseUrl: string?,
|
||||||
maxBytes: number?, -- refuse bigger files (default 64 MB)
|
maxBytes: number?, -- default 64 MB
|
||||||
}
|
}
|
||||||
export type EncodeOptions = {
|
export type EncodeOptions = {
|
||||||
title: string?, -- #PLAYLIST: (default: the playlist's title)
|
title: string?, -- #PLAYLIST: (default: the playlist's title)
|
||||||
@@ -75,7 +65,6 @@ export type EncodeOptions = {
|
|||||||
-- ------------------------------------------------------------------------------------------------ constants
|
-- ------------------------------------------------------------------------------------------------ constants
|
||||||
local UNSUPPORTED_SEGMENTS = { ts = true, aac = true, m4s = true, mp4 = true, m4a = true, fmp4 = true, cmfa = true }
|
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 function extension(url: string): string?
|
||||||
local p = Url.split(url).path
|
local p = Url.split(url).path
|
||||||
local ext = string.match(p, "%.([%w]+)$")
|
local ext = string.match(p, "%.([%w]+)$")
|
||||||
@@ -301,7 +290,6 @@ local function parseHls(ls: { string }, base: string?, baseScheme: string?, warn
|
|||||||
return res
|
return res
|
||||||
end
|
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?)
|
function M3u.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Playlist?, Err.Err?)
|
||||||
local n = r.limit - r.pos
|
local n = r.limit - r.pos
|
||||||
if n > (opts and opts.maxBytes or 2 ^ 26) then
|
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
|
end
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ encode
|
-- ------------------------------------------------------------------------------------------------ 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 function serialize(entries: { any }, opts: { title: string? }?): string
|
||||||
local out = { "#EXTM3U" }
|
local out = { "#EXTM3U" }
|
||||||
local function clean(s: string): string
|
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"
|
return table.concat(out, "\n") .. "\n"
|
||||||
end
|
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?)
|
function M3u.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||||
if type(value) ~= "table" then
|
if type(value) ~= "table" then
|
||||||
return nil, Err.new(Err.ARG, "encode: needs a Playlist or a list of entries")
|
return nil, Err.new(Err.ARG, "encode: needs a Playlist or a list of entries")
|
||||||
|
|||||||
@@ -1,12 +1,7 @@
|
|||||||
--!strict
|
--!strict
|
||||||
-- playlist/pls: PLS playlists ([playlist], FileN / TitleN / LengthN, NumberOfEntries, Version). Absorbed from
|
-- 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
|
-- playlist-luau 3fc7fd3 (same author) plus a new writer. Supports: keys in any case / order, comments, BOM, CRLF / CR,
|
||||||
-- and order, comments, a BOM, CRLF / CR, Windows-1252 text, gaps in the numbering (entries sort by number), a
|
-- Windows-1252, numbering gaps; locations resolve per RFC 3986. Encode: Version=2, Length -1 for unknown durations.
|
||||||
-- 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)
|
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ requires
|
-- ------------------------------------------------------------------------------------------------ requires
|
||||||
local Err = require("@rosetta/core/Err")
|
local Err = require("@rosetta/core/Err")
|
||||||
@@ -30,13 +25,12 @@ local Pls = {
|
|||||||
export type Entry = {
|
export type Entry = {
|
||||||
url: string,
|
url: string,
|
||||||
title: string?,
|
title: string?,
|
||||||
duration: number?, -- seconds; nil when unknown (-1 in the file)
|
duration: number?, -- seconds; -1 in the file = unknown
|
||||||
playable: boolean, -- an http(s) URL the player can fetch
|
playable: boolean,
|
||||||
reason: string?, -- why not playable: "local path", "relative path without a base URL", "scheme ftp", ...
|
reason: string?,
|
||||||
-- HLS
|
bandwidth: number?, -- BANDWIDTH
|
||||||
bandwidth: number?, -- master: BANDWIDTH
|
codecs: string?, -- CODECS
|
||||||
codecs: string?, -- master: CODECS
|
attributes: { [string]: string }?, -- EXT-X-STREAM-INF / EXT-X-MEDIA attribute list
|
||||||
attributes: { [string]: string }?, -- master: the whole EXT-X-STREAM-INF / EXT-X-MEDIA attribute list
|
|
||||||
byteRange: { length: number, offset: number? }?, -- media: EXT-X-BYTERANGE
|
byteRange: { length: number, offset: number? }?, -- media: EXT-X-BYTERANGE
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,10 +38,10 @@ export type Hls = {
|
|||||||
master: boolean,
|
master: boolean,
|
||||||
targetDuration: number?,
|
targetDuration: number?,
|
||||||
mediaSequence: 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
|
encrypted: boolean, -- EXT-X-KEY with a METHOD other than NONE
|
||||||
totalDuration: number?, -- media: sum of the segment durations
|
totalDuration: number?,
|
||||||
segmentFormat: string?, -- media: the segments' common extension ("mp3", "ts", "aac", ...) or "mixed"
|
segmentFormat: string?,
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Result = {
|
export type Result = {
|
||||||
@@ -56,13 +50,13 @@ export type Result = {
|
|||||||
meta: { [string]: any },
|
meta: { [string]: any },
|
||||||
entries: { Entry },
|
entries: { Entry },
|
||||||
warnings: { string },
|
warnings: { string },
|
||||||
title: string?, -- #PLAYLIST: / PLS has none
|
title: string?, -- #PLAYLIST:
|
||||||
hls: Hls?,
|
hls: Hls?,
|
||||||
}
|
}
|
||||||
|
|
||||||
export type DecodeOptions = {
|
export type DecodeOptions = {
|
||||||
baseUrl: string?, -- the playlist's own URL (or Windows path), for relative entries
|
baseUrl: string?,
|
||||||
maxBytes: number?, -- refuse bigger files (default 64 MB)
|
maxBytes: number?, -- default 64 MB
|
||||||
}
|
}
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ constants
|
-- ------------------------------------------------------------------------------------------------ constants
|
||||||
@@ -129,7 +123,6 @@ local function parsePls(ls: { string }, base: string?, baseScheme: string?, warn
|
|||||||
return res
|
return res
|
||||||
end
|
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?)
|
function Pls.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Playlist?, Err.Err?)
|
||||||
local n = r.limit - r.pos
|
local n = r.limit - r.pos
|
||||||
if n > (opts and opts.maxBytes or 2 ^ 26) then
|
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
|
end
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ encode
|
-- ------------------------------------------------------------------------------------------------ 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?)
|
function Pls.encode(value: any, _opts: any?): (buffer?, Err.Err?)
|
||||||
if type(value) ~= "table" then
|
if type(value) ~= "table" then
|
||||||
return nil, Err.new(Err.ARG, "encode: needs a Playlist or a list of entries")
|
return nil, Err.new(Err.ARG, "encode: needs a Playlist or a list of entries")
|
||||||
|
|||||||
@@ -1,15 +1,7 @@
|
|||||||
--!strict
|
--!strict
|
||||||
-- playlist/xspf: XSPF 1 ("spiff", https://xspf.org/spec) playlists on codecs/text/Xml. New in rosetta (playlist-luau
|
-- playlist/xspf: XSPF 1 (https://xspf.org/spec) playlists on codecs/text/Xml. Supports: playlist and track metadata,
|
||||||
-- had no XSPF). Decode: the playlist's title / creator / annotation / info / location / identifier / image / date,
|
-- every track location (entry.locations), duration (ms), xml:base then opts.baseUrl (RFC 3986).
|
||||||
-- 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.
|
|
||||||
-- Not supported: extension / link / meta elements (skipped), XSPF version 0 differences.
|
-- 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
|
-- ------------------------------------------------------------------------------------------------ requires
|
||||||
local Err = require("@rosetta/core/Err")
|
local Err = require("@rosetta/core/Err")
|
||||||
@@ -30,8 +22,8 @@ local Xspf = {
|
|||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ types
|
-- ------------------------------------------------------------------------------------------------ types
|
||||||
export type DecodeOptions = {
|
export type DecodeOptions = {
|
||||||
baseUrl: string?, -- the playlist's own URL (or Windows path), for relative locations
|
baseUrl: string?,
|
||||||
maxBytes: number?, -- refuse bigger files (default 64 MB)
|
maxBytes: number?, -- default 64 MB
|
||||||
}
|
}
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ constants
|
-- ------------------------------------------------------------------------------------------------ constants
|
||||||
@@ -43,7 +35,6 @@ local function trim(s: string): string
|
|||||||
return (string.match(s, "^%s*(.-)%s*$") :: string)
|
return (string.match(s, "^%s*(.-)%s*$") :: string)
|
||||||
end
|
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?
|
local function field(node: any, name: string): string?
|
||||||
for _, c in node.children do
|
for _, c in node.children do
|
||||||
if type(c) == "table" and Xml.localName(c.name) == name then
|
if type(c) == "table" and Xml.localName(c.name) == name then
|
||||||
@@ -75,8 +66,6 @@ function Xspf.sniff(r: Bytes.Reader): number
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ decode
|
-- ------------------------------------------------------------------------------------------------ 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?)
|
function Xspf.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Playlist?, Err.Err?)
|
||||||
local n = r.limit - r.pos
|
local n = r.limit - r.pos
|
||||||
if n > (opts and opts.maxBytes or 2 ^ 26) then
|
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
|
end
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ encode
|
-- ------------------------------------------------------------------------------------------------ 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?)
|
function Xspf.encode(value: any, opts: { indent: number?, title: string? }?): (buffer?, Err.Err?)
|
||||||
if type(value) ~= "table" then
|
if type(value) ~= "table" then
|
||||||
return nil, Err.new(Err.ARG, "encode: needs a Playlist or a list of entries")
|
return nil, Err.new(Err.ARG, "encode: needs a Playlist or a list of entries")
|
||||||
|
|||||||
Reference in new issue
Block a user