Skip to content

modified screenshotfolder.lua to save filename based on chapter name #18

@mrfragger

Description

@mrfragger

It had lots of trouble with utf-8 various characters and trimming since it's doing it based on bytes. Long story short just have to assume your chapters aren't too long and thus no trimming. In my case they aren't since audiobooks chapter names are based on filenames already. Thanks for the script zydezu. Mostly I will use s-s and turn off OSD if not wanted. Not sure why subs are way off when using just screenshot.

input.conf
s-s script-message screenshotfolder/screenshot window #! [Screenshots] > Screenshot Window with Subs aligned
s-v script-message screenshotfolder/screenshot video #! [Screenshots] > Screenshot without Subs
s-w script-message screenshotfolder/screenshot subs #! [Screenshots] > Screenshot with Subs misaligned

--[[ 
    screenshotfolder.lua by zydezu
    (https://github.com/zydezu/mpvconfig/blob/main/scripts/screenshotfolder.lua)

    Place screenshots into folders based on media title for each video/audio, along with timestamping them
    and each filename based on a chapter name.
]]

local options = {
    file_ext = "png",  -- File extension for screenshots jpg, png, avif
    include_YouTube_ID = true, -- Include YouTube video ID in the screenshot filename
    short_saved_message = false -- Show a short "Screenshot saved" message instead of the full path
}

(require "mp.options").read_options(options)

local title = "default"
local chaptername = ""

local function is_url(s)
    return string.match(s, "^[%w]+://[%w%.%-_]+%.[%a]+[-%w%.%-%_/?&=]*") ~= nil
end

local function safe_chaptername(chaptername)
    return chaptername:gsub('[\\/:*?"<>|]', '')
end

local function set_file_dir()
    mp.set_property("screenshot-directory", "~/datampv/screenshots/" .. title .. "/")
    mp.set_property("screenshot-format", options.file_ext)
end

local function set_screenshot_template()
    local current_time = os.date("%H-%M-%S")
    local safe_chapter = safe_chaptername(chaptername)
    
    if safe_chapter ~= "" then
        mp.set_property("screenshot-template", safe_chapter .. " (" .. current_time .. ")")
    else
        mp.set_property("screenshot-template", title .. " (" .. current_time .. ")")
    end
end

local function init()
    local filename = mp.get_property("filename/no-ext")
    local media = mp.get_property("media-title")
    local path = mp.get_property("path")

    if is_url(path) then
        local youtube_ID = ""
        local videoIDMatch = mp.get_property("filename"):match("[?&]v=([^&]+)")
        if options.include_YouTube_ID and videoIDMatch then
            youtube_ID = " [" .. videoIDMatch .. "]"
        end
        filename = string.gsub(media:sub(1, 100), "^%s*(.-)%s*$", "%1") .. youtube_ID
    end

    title = filename:gsub('[\\/:*?"<>|]', '')
    set_file_dir()
    set_screenshot_template()
end

local function screenshot_done(flag)
    if flag == "video" or flag == "window" then
        mp.commandv("screenshot", flag)
    elseif flag == "subs" then
        mp.commandv("screenshot") -- No flag needed for subs
    end
    
    if options.short_saved_message then
        mp.osd_message("Screenshot saved")
    else
        local screenshot_dir = mp.command_native({"expand-path", mp.get_property("screenshot-directory")})
        local current_time = os.date("%H-%M-%S")
        local filename = (chaptername ~= "" and safe_chaptername(chaptername) or title) .. " (" .. current_time .. ")." .. options.file_ext
        local filepath = screenshot_dir .. title .. "/" .. filename
        mp.osd_message("Screenshot saved: " .. filepath:gsub("\\", "/"))
    end
end

local function take_screenshot(flag)
    if title == "default" then
        init()
    end
    set_screenshot_template()
    screenshot_done(flag)
end

mp.register_script_message("screenshotfolder/screenshot", function(flag)
    take_screenshot(flag)
end)

mp.observe_property("chapter-metadata/title", "string", function(_, value)
    chaptername = value or ""
    set_screenshot_template()
end)

mp.register_event("file-loaded", init)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions