7.9 KiB
Playlists
M3U / M3U8 (including HLS), PLS and XSPF load as one Playlist: a list of entries with a location, an optional
title and duration, and whether a player can fetch it. Relative locations resolve against the playlist's own URL
when you pass it, and every entry is classified as playable (an http(s) URL) or not, with the reason. All three
formats are writable, so converting between them is a load and a save.
local pl = assert(F.playlist.load(m3uText, { baseUrl = "https://example.com/lists/mix.m3u" }))
print(pl.format, pl.title, #pl.entries) -- m3u Docs Mix 3
for _, e in pl.entries do
print(e.url, e.title, e.duration, e.playable)
end
-- https://example.com/music/first.mp3 Ada - First Song 215 true
-- http://radio.example/stream Live Radio nil true
-- https://example.com/lists/songs/local.flac nil nil true
local pls = assert(F.saveString(pl, { format = "pls" }))
The type
Playlist:
| Field | Type | Meaning |
|---|---|---|
kind |
"playlist" |
|
format |
string | "m3u", "hls" (an M3U8 with HLS tags), "pls" or "xspf" |
entries |
{ PlaylistEntry } |
in playlist order |
warnings |
{ string } |
what was odd but readable (an unresolvable location, a NumberOfEntries mismatch, ...), with line numbers |
title |
string? | M3U #PLAYLIST:, XSPF <title> (PLS has none) |
hls |
table? | HLS playlists only (below) |
meta |
table | source.format; XSPF: creator, annotation, info, location, identifier, image, date |
PlaylistEntry:
| Field | Type | Meaning |
|---|---|---|
url |
string | the location, resolved against opts.baseUrl (or XSPF xml:base) when relative |
title |
string? | #EXTINF title, PLS TitleN, XSPF <title> |
duration |
number? | seconds; nil when unknown (-1 in M3U / PLS) |
playable |
boolean | an http / https URL a player can fetch |
reason |
string? | why not playable: "local path", "relative path without a base URL", "scheme ftp", ... |
bandwidth, codecs, attributes |
HLS master playlists: BANDWIDTH, CODECS and the whole attribute list (RESOLUTION, ...) |
|
byteRange |
{ length, offset? }? |
HLS media playlists: EXT-X-BYTERANGE |
creator, album, annotation, info, image, trackNum, locations |
XSPF tracks (locations: every <location>; url is the first) |
Load
| Option | Meaning |
|---|---|
baseUrl |
the playlist's own URL (or a Windows path): relative entries resolve against it (RFC 3986) |
maxBytes |
refuse bigger files (default 64 MB) |
Text decoding is forgiving: a UTF-8 BOM, CRLF or CR line ends, and Windows-1252 text (old M3U files) all work.
local pl = assert(F.playlist.load(m3uText)) -- no base URL: the relative entry is not playable
local last = pl.entries[3]
assert(last.url == "songs/local.flac" and not last.playable)
assert(last.reason == "relative path without a base URL")
assert(#pl.warnings == 1)
print(pl.warnings[1]) -- line 7: relative path without a base URL: songs/local.flac
local radio = assert(F.playlist.load(plsText))
assert(radio.format == "pls" and radio.entries[1].title == "Radio One" and radio.entries[1].duration == nil)
assert(radio.entries[2].duration == 187)
local spiff = assert(F.playlist.load(xspfText))
local track = spiff.entries[1]
assert(spiff.format == "xspf" and track.creator == "Ada" and track.duration == 215) -- XSPF stores milliseconds
HLS
An M3U8 with HLS tags loads with format = "hls" and an hls table. A master playlist lists variant streams; a
media playlist lists segments.
hls field |
Meaning |
|---|---|
master |
true for a master playlist (variants), false for a media playlist (segments) |
targetDuration, mediaSequence |
media: EXT-X-TARGETDURATION, EXT-X-MEDIA-SEQUENCE |
endList |
media: EXT-X-ENDLIST seen (false = live, the playlist grows) |
encrypted |
an EXT-X-KEY with a method other than NONE |
totalDuration |
media: the sum of the segment durations, seconds |
segmentFormat |
media: the segments' common extension ("mp3", "ts", "aac", ...) or "mixed" |
Segments that are MPEG-TS, AAC or fragmented MP4 are flagged unplayable (reason says so): the classification is
about what a simple player can stream as a file, not what the library can decode.
local master = assert(F.playlist.load(hlsText, { baseUrl = "https://cdn.example/v/master.m3u8" }))
assert(master.format == "hls" and master.hls.master)
for _, v in master.entries do
print(v.bandwidth, v.attributes.RESOLUTION, v.url)
end
-- 800000 640x360 https://cdn.example/v/low/index.m3u8
-- 2400000 1280x720 https://cdn.example/v/high/index.m3u8
assert(master.entries[1].codecs == "avc1.4d401e,mp4a.40.2")
local media = assert(F.playlist.load(
"#EXTM3U\n#EXT-X-TARGETDURATION:4\n#EXTINF:4,\nseg1.mp3\n#EXTINF:3.5,\nseg2.mp3\n#EXT-X-ENDLIST\n",
{ format = "m3u8", baseUrl = "https://cdn.example/audio/list.m3u8" }))
assert(not media.hls.master and media.hls.endList and media.hls.segmentFormat == "mp3")
assert(media.hls.totalDuration == 7.5 and media.entries[2].url == "https://cdn.example/audio/seg2.mp3")
Edit, save and convert
Entries are plain tables: filter, reorder or add them, then save. The format comes from opts.format, the
destination's extension, or the format the playlist was loaded from (as everywhere, see the guide).
local pl = assert(F.playlist.load(m3uText, { baseUrl = "https://example.com/lists/mix.m3u" }))
local keep = {}
for _, e in pl.entries do
if e.duration then -- drop the live stream (unknown length)
table.insert(keep, e)
end
end
table.insert(keep, { url = "https://example.com/music/bonus.ogg", title = "Bonus", duration = 61, playable = true })
pl.entries = keep
local m3u = assert(F.saveString(pl)) -- M3U8 text, the source format
assert(string.find(m3u, "#EXTINF:61,Bonus\nhttps://example.com/music/bonus.ogg", 1, true))
local pls = assert(F.saveString(pl, { format = "pls" }))
assert(string.find(pls, "NumberOfEntries=2", 1, true))
local xspf = assert(F.saveString(pl, { format = "xspf" }))
assert(F.playlist.load(xspf).entries[2].duration == 61)
A playlist built from scratch needs kind = "playlist", entries and a format to save as:
local pl = { kind = "playlist", format = "m3u", title = "New", entries = {
{ url = "https://example.com/a.mp3", title = "A", duration = 120, playable = true },
}, warnings = {}, meta = {} }
local text = assert(F.saveString(pl, { format = "m3u" }))
assert(string.sub(text, 1, 7) == "#EXTM3U")
| Writer | Writes |
|---|---|
| m3u | #EXTM3U, #PLAYLIST: (opts.title overrides), #EXTINF:<seconds>,<title>, the locations |
| pls | [playlist], FileN / TitleN / LengthN (-1 for unknown), NumberOfEntries, Version=2 |
| xspf | XSPF version 1: title, track location / title / creator / album / annotation / info / image / trackNum / duration |
Formats
| Format | Extensions | Read | Write |
|---|---|---|---|
| m3u | .m3u .m3u8 |
yes, including HLS master and media playlists | yes (plain M3U8; HLS tags are not written) |
| pls | .pls |
yes | yes |
| xspf | .xspf |
yes | yes |
See FORMATS.md.
Gotchas
- Pass
baseUrlfor playlists fetched from the web: otherwise relative entries keep their text and are not playable. - HLS round trips lose the HLS tags. Saving an HLS playlist writes a plain M3U8 (locations,
#EXTINF); variant attributes, keys and byte ranges are not written. - M3U durations are written as whole seconds (
#EXTINF:4,for 3.5 s). F.detectsaysm3ufor HLS text;pl.formattells you whether HLS tags were found.- Local paths (
C:\Music\a.mp3,/home/me/a.mp3,file:URLs) are kept as written and flagged unplayable; the library never reads the files a playlist points to.