-- examples/playlist.luau: read an M3U, inspect an HLS master playlist, convert to PLS, XSPF (docs/guide/playlist.md). local F = require("@rosetta") local M3U = "#EXTM3U\n#PLAYLIST:Road Trip\n#EXTINF:215,Ada - First Song\nmusic/first.mp3\n" .. "#EXTINF:187,Bob - Second Song\nhttps://cdn.example/second.ogg\n#EXTINF:-1,Live\nhttp://radio.example/live\n" .. "C:\\Music\\local.flac\n" local pl = assert(F.playlist.load(M3U, { baseUrl = "https://example.com/lists/trip.m3u" })) print(string.format("%s (%s): %d entries", pl.title, pl.format, #pl.entries)) local total = 0 for i, e in pl.entries do print(string.format(" %d. %-32s %-18s %s", i, e.url, e.title or "", if e.playable then "ok" else "skip: " .. tostring(e.reason))) total += e.duration or 0 end assert(pl.entries[1].url == "https://example.com/lists/music/first.mp3") assert(not pl.entries[4].playable and pl.entries[4].reason == "local path") print(string.format("known length: %d s", total)) -- keep only what a player can fetch, then convert local keep = {} for _, e in pl.entries do if e.playable then table.insert(keep, e) end end pl.entries = keep local pls = assert(F.saveString(pl, { format = "pls" })) local xspf = assert(F.saveString(pl, { format = "xspf" })) assert(F.playlist.load(pls).entries[2].duration == 187) assert(F.playlist.load(xspf).entries[1].title == "Ada - First Song") print(string.format("pls %d bytes, xspf %d bytes", #pls, #xspf)) -- an HLS master playlist: pick the variant that fits a bandwidth local MASTER = "#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=800000,RESOLUTION=640x360\nlow.m3u8\n" .. "#EXT-X-STREAM-INF:BANDWIDTH=2400000,RESOLUTION=1280x720\nhigh.m3u8\n" local hls = assert(F.playlist.load(MASTER, { baseUrl = "https://cdn.example/show/master.m3u8" })) assert(hls.format == "hls" and hls.hls.master) local best = nil for _, v in hls.entries do if v.bandwidth <= 1500000 and (not best or v.bandwidth > best.bandwidth) then best = v end end print("variant for 1.5 Mbit/s: " .. best.attributes.RESOLUTION .. " " .. best.url) assert(best.url == "https://cdn.example/show/low.m3u8") print("playlist example: ok")