Guest User

chapter_frame_only

a guest
Apr 13th, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.85 KB | Source Code | 0 0
  1. # DOES NOT APPEND CHAPTERS CORRECTLY even if you delete chapter file after loading and resave
  2. --[[
  3.   * chapter_make_read.lua v.2022-06-24
  4.   *
  5.   * AUTHORS: dyphire
  6.   * License: MIT
  7.   * link: https://github.com/dyphire/mpv-scripts
  8. --]]
  9.  
  10. -- This script also supports marks and creates external chapter files, usage:
  11. -- Note: It can also be used to export the existing chapter information of the playing file.
  12. -- add bindings to input.conf:
  13. -- key script-message-to chapter_frame_only create_chapter
  14. -- key script-message-to chapter_frame_only write_chapter
  15.  
  16. local msg = require 'mp.msg'
  17. local utils = require 'mp.utils'
  18. local options = require "mp.options"
  19.  
  20. local o = {
  21.     -- Specifies the extension of the external chapter file.
  22.     chapter_flie_ext = "_chapter.keyframe",
  23. }
  24.  
  25. (require 'mp.options').read_options(o)
  26.  
  27.  
  28. local function create_chapter()
  29.     local time_pos = mp.get_property_number("time-pos")
  30.     local frame_pos = mp.get_property_number("estimated-frame-number")
  31.     local frame_pos_osd = mp.get_property_osd("estimated-frame-number")
  32.     local curr_chapter = mp.get_property_number("chapter")
  33.     local chapter_count = mp.get_property_number("chapter-list/count")
  34.     local all_chapters = mp.get_property_native("chapter-list")
  35.     mp.osd_message(frame_pos_osd, 1)
  36.  
  37.     if chapter_count == 0 then
  38.         all_chapters[1] = {
  39.             title = frame_pos .. "",
  40.             time = time_pos
  41.         }
  42.         -- We just set it to zero here so when we add 1 later it ends up as 1
  43.         -- otherwise it's probably "nil"
  44.         curr_chapter = 0
  45.         -- note that mpv will treat the beginning of the file as all_chapters[0] when using pageup/pagedown
  46.         -- so we don't actually have to worry if the file doesn't start with a chapter
  47.     else
  48.         -- to insert a chapter we have to increase the index on all subsequent chapters
  49.         -- otherwise we'll end up with duplicate chapter IDs which will confuse mpv
  50.         -- +2 looks weird, but remember mpv indexes at 0 and lua indexes at 1
  51.         -- adding two will turn "current chapter" from mpv notation into "next chapter" from lua's notation
  52.         -- count down because these areas of memory overlap
  53.         for i = chapter_count, curr_chapter + 2, -1 do
  54.             all_chapters[i + 1] = all_chapters[i]
  55.         end
  56.         all_chapters[curr_chapter+2] = {
  57.             title = frame_pos .. "",
  58.             time = time_pos
  59.         }
  60.     end
  61.     mp.set_property_native("chapter-list", all_chapters)
  62.     mp.set_property_number("chapter", curr_chapter+1)
  63. end
  64.  
  65.  
  66. local function write_chapter()
  67.     local euid = mp.get_property_number("estimated-frame-count")
  68.     local chapter_count = mp.get_property_number("chapter-list/count")
  69.     local all_chapters = mp.get_property_native("chapter-list")
  70.     local insert_chapters = ""
  71.     local curr = nil
  72.  
  73.     for i = 1, chapter_count, 1 do
  74.         curr = all_chapters[i]
  75.         local next_chapter = curr.title .. "\n"
  76.         insert_chapters = insert_chapters .. next_chapter
  77.     end
  78.  
  79.     local chapters = insert_chapters
  80.  
  81.     local path = mp.get_property("path")
  82.     dir, name_ext = utils.split_path(path)
  83.     local name = string.sub(name_ext, 1, (string.len(name_ext)-4))
  84.     local out_path = utils.join_path(dir, name .. o.chapter_flie_ext)
  85.     local file = io.open(out_path, "w")
  86.     if file == nil then
  87.         dir = utils.getcwd()
  88.         out_path = utils.join_path(dir, "create" .. o.chapter_flie_ext)
  89.         file = io.open(out_path, "w")
  90.     end
  91.     if file == nil then
  92.         mp.error("Could not open chapter file for writing.")
  93.         return
  94.     end
  95.     file:write(chapters)
  96.     file:close()
  97.     mp.osd_message("Export file to: " .. out_path, 3)
  98. end
  99.  
  100.  
  101. mp.register_script_message("create_chapter", create_chapter, {repeatable=true})
  102. mp.register_script_message("write_chapter", write_chapter, {repeatable=false})
Advertisement
Add Comment
Please, Sign In to add comment