First push

This commit is contained in:
2024-12-31 12:23:51 -06:00
commit 9b5d2863f7
3 changed files with 85 additions and 0 deletions

9
README.md Normal file
View File

@@ -0,0 +1,9 @@
# Radio Chapters MPV Plugin
This plugin automatically creates chapters for a stream if it has a metadata title.
## Options
| Option | Description | Type | Default |
| ------------- | ------------- |
| `show_osd` | Shows the chapter title on OSD when it changes | boolean | `true` |
| `osd_string` | Format to show chapter title on OSD | string | `"%s"` |
| `zero_first_chapter` | Set first chapter to the very beginning of the seekbar | boolean | `true` |

View File

@@ -0,0 +1,3 @@
show_osd=true
osd_string=%s
zero_first_chapter=true

View File

@@ -0,0 +1,73 @@
-- 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)