Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # DOES NOT APPEND CHAPTERS CORRECTLY even if you delete chapter file after loading and resave
- --[[
- * chapter_make_read.lua v.2022-06-24
- *
- * AUTHORS: dyphire
- * License: MIT
- * link: https://github.com/dyphire/mpv-scripts
- --]]
- -- This script also supports marks and creates external chapter files, usage:
- -- Note: It can also be used to export the existing chapter information of the playing file.
- -- add bindings to input.conf:
- -- key script-message-to chapter_frame_only create_chapter
- -- key script-message-to chapter_frame_only write_chapter
- local msg = require 'mp.msg'
- local utils = require 'mp.utils'
- local options = require "mp.options"
- local o = {
- -- Specifies the extension of the external chapter file.
- chapter_flie_ext = "_chapter.keyframe",
- }
- (require 'mp.options').read_options(o)
- local function create_chapter()
- local time_pos = mp.get_property_number("time-pos")
- local frame_pos = mp.get_property_number("estimated-frame-number")
- local frame_pos_osd = mp.get_property_osd("estimated-frame-number")
- local curr_chapter = mp.get_property_number("chapter")
- local chapter_count = mp.get_property_number("chapter-list/count")
- local all_chapters = mp.get_property_native("chapter-list")
- mp.osd_message(frame_pos_osd, 1)
- if chapter_count == 0 then
- all_chapters[1] = {
- title = frame_pos .. "",
- time = time_pos
- }
- -- We just set it to zero here so when we add 1 later it ends up as 1
- -- otherwise it's probably "nil"
- curr_chapter = 0
- -- note that mpv will treat the beginning of the file as all_chapters[0] when using pageup/pagedown
- -- so we don't actually have to worry if the file doesn't start with a chapter
- else
- -- to insert a chapter we have to increase the index on all subsequent chapters
- -- otherwise we'll end up with duplicate chapter IDs which will confuse mpv
- -- +2 looks weird, but remember mpv indexes at 0 and lua indexes at 1
- -- adding two will turn "current chapter" from mpv notation into "next chapter" from lua's notation
- -- count down because these areas of memory overlap
- for i = chapter_count, curr_chapter + 2, -1 do
- all_chapters[i + 1] = all_chapters[i]
- end
- all_chapters[curr_chapter+2] = {
- title = frame_pos .. "",
- time = time_pos
- }
- end
- mp.set_property_native("chapter-list", all_chapters)
- mp.set_property_number("chapter", curr_chapter+1)
- end
- local function write_chapter()
- local euid = mp.get_property_number("estimated-frame-count")
- local chapter_count = mp.get_property_number("chapter-list/count")
- local all_chapters = mp.get_property_native("chapter-list")
- local insert_chapters = ""
- local curr = nil
- for i = 1, chapter_count, 1 do
- curr = all_chapters[i]
- local next_chapter = curr.title .. "\n"
- insert_chapters = insert_chapters .. next_chapter
- end
- local chapters = insert_chapters
- local path = mp.get_property("path")
- dir, name_ext = utils.split_path(path)
- local name = string.sub(name_ext, 1, (string.len(name_ext)-4))
- local out_path = utils.join_path(dir, name .. o.chapter_flie_ext)
- local file = io.open(out_path, "w")
- if file == nil then
- dir = utils.getcwd()
- out_path = utils.join_path(dir, "create" .. o.chapter_flie_ext)
- file = io.open(out_path, "w")
- end
- if file == nil then
- mp.error("Could not open chapter file for writing.")
- return
- end
- file:write(chapters)
- file:close()
- mp.osd_message("Export file to: " .. out_path, 3)
- end
- mp.register_script_message("create_chapter", create_chapter, {repeatable=true})
- mp.register_script_message("write_chapter", write_chapter, {repeatable=false})
Advertisement
Add Comment
Please, Sign In to add comment