-- Radio Chapters -- Creates chapters for streams from metadata titles. local options = { -- whether to show OSD messages on title changes show_osd = true, -- String format to show on OSD on title change osd_string = "%s", -- If the first chapter should start at 00:00 zero_first_chapter = true, } local last_title = nil local first_metadata_received = false function create_chapter(title, time) if not title then return end -- Create chapter local chapter_title = string.gsub(title, '[\n\r]', ' ') local chapter = { title = chapter_title, time = time } local chapter_list = mp.get_property_native("chapter-list") or {} table.insert(chapter_list, chapter) mp.set_property_native("chapter-list", chapter_list) if options.show_osd then mp.osd_message(string.format(options.osd_string, chapter_title)) end end function handle_metadata(metadata) local current_title = metadata["icy-title"] or metadata["title"] if not current_title then return end local current_time = mp.get_property_number("time-pos") or 0 -- First metadata recieved, create chapter. if not first_metadata_received then -- Set chapter at time 0 or current time based on option local initial_time = options.zero_first_chapter and 0 or current_time create_chapter(current_title, initial_time) first_metadata_received = true last_title = current_title return end -- Create new chapter if title changed if current_title ~= last_title then create_chapter(current_title, current_time) last_title = current_title end end function on_metadata_change(name, metadata) if name ~= "metadata" then return end if not metadata then return end handle_metadata(metadata) end -- Reset state on new file function on_file_loaded() last_title = nil first_metadata_received = false mp.set_property_native("chapter-list", {}) end mp.observe_property("metadata", "native", on_metadata_change) mp.register_event("file-loaded", on_file_loaded)