mpv-chapter-title-duration/chapter-title-duration.lua

67 lines
2.1 KiB
Lua

local chapter = { number = 0, title = "", length = 0, available = false }
local function seconds_to_minutes(total_seconds)
local minutes = math.floor(total_seconds / 60)
local seconds = total_seconds % 60
return minutes, seconds
end
local function showChapterTitle()
local message = ""
if (chapter.available == true) then
local minutes, seconds = seconds_to_minutes(chapter.length)
local formatted_time = string.format("%02d:%02d", minutes, seconds)
message = string.format("Chapter (%s): %s (%s)", chapter.number, chapter.title, formatted_time)
else
message = "No chapters available"
end
mp.osd_message(message)
end
local function chapter_info_update()
local chapterList = mp.get_property_native("chapter-list")
if not chapterList or #chapterList == 0 then
chapter.available = false
return
end
chapter.available = true
local chapterCount = (mp.get_property_number("chapter-list/count") or 0)
local currentChapterIndex = (mp.get_property_number("chapter") or 0) + 1
chapter.number = currentChapterIndex
local currentChapter = chapterList[currentChapterIndex]
chapter.title = currentChapter.title or ""
local nextChapterIndex = currentChapterIndex + 1
if (nextChapterIndex > chapterCount) then
local durationFull = mp.get_property_number("duration/full") or 0
if durationFull ~= 0 then
chapter.length = durationFull - currentChapter.time
end
else
local nextChapter = chapterList[nextChapterIndex]
chapter.length = nextChapter.time - currentChapter.time
end
end
local function onChapterChange()
chapter_info_update()
showChapterTitle()
end
mp.register_event("file-loaded", chapter_info_update)
mp.observe_property("chapter", nil, function()
onChapterChange()
end)
mp.add_key_binding(nil, "show-chapter-title", function()
showChapterTitle()
end)
-- don't display the native OSD
-- when changing chapters as it overlaps
-- with ours but only sometimes
-- so it creates this janky feeling
mp.add_key_binding("PGUP", "next-chapter", function()
mp.command("no-osd add chapter 1")
end)
mp.add_key_binding("PGDWN", "prev-chapter", function()
mp.command("no-osd add chapter -1")
end)