Skip to content

Commit

Permalink
add mkvpropedit
Browse files Browse the repository at this point in the history
fixes #12
  • Loading branch information
mar04 committed Nov 11, 2024
1 parent d6e9613 commit 449aa78
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This version of the script requires mpv 0.38 or newer.
* option to automatically save/load chapter files
* option to use ffmpeg to put the chapters into the media file container as a
metadata, so that other media players, like vlc, can make use of them
* embed chapters in place in mkv container using mkvpropedit
* should work on Unix and Windows (tested on Archlinux and Windows 10)

## Installation
Expand All @@ -37,6 +38,7 @@ This version of the script requires mpv 0.38 or newer.
ctrl+b script-binding chapters/write_xml
B script-binding chapters/write_txt
ctrl+, script-binding chapters/bake_chapters
K script-binding chapters/mkvpropedit
```

## Thanks
Expand Down
53 changes: 53 additions & 0 deletions chapters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,58 @@ local function bake_chapters()
end


-- edit mkv file in place
local function mkvpropedit()
local ext
local filename = mp.get_property("filename")
local file_path = mp.get_property("path")

if mp.get_property_number("chapter-list/count") == 0 then
msg.verbose("no chapters present")
return
end

-- check the extension
local reverse_dot_index = filename:reverse():find(".", 1, true)
if reverse_dot_index == nil then
msg.warning("file has no extension, are you sure it's mkv?")
else
local dot_index = #filename + 1 - reverse_dot_index
ext = filename:sub(dot_index + 1)
if ext ~= "mkv" then
msg.warning("file does not have mkv extension, are you sure it's mkv?")
end
end
local chapters_file_path = write("ffmetadata.tmp")
if not chapters_file_path then
msg.error("no chapters file")
return
end

local args = {"mkvpropedit", file_path, "--chapters", chapters_file_path}

msg.debug("args:", utils.to_string(args))

local process = mp.command_native({
name = 'subprocess',
playback_only = false,
capture_stdout = true,
capture_stderr = true,
args = args
})

if process.status == 0 then
mp.osd_message("chapters embedded", 3)
else
msg.error("failed to embed chapters", process.stderr)
end

-- remove temporary chapters file
rm(chapters_file_path)
end



-- HOOKS -----------------------------------------------------------------------


Expand All @@ -707,3 +759,4 @@ mp.add_key_binding(nil, "write_txt", function () write("txt", true) end)
mp.add_key_binding(nil, "write_list", function () write("list.txt", true) end)
mp.add_key_binding(nil, "write_ffmetadata", function () write("ffmetadata", true) end)
mp.add_key_binding(nil, "bake_chapters", bake_chapters)
mp.add_key_binding(nil, "mkvpropedit", mkvpropedit)

0 comments on commit 449aa78

Please sign in to comment.