-- USAGE: Set an A-B loop (default keybind is l) then press o to save
-- The file will be saved in your working directory or screenshot-directory if set
-- dump-cache doesn't seem to work for local files
local os = require("os")
local mp = require("mp")
local utils = require("mp.utils")
local webm_codecs_v = {"vp8", "vp9", "av1"}
local webm_codecs_a = {"opus", "vorbis"}
local mp4_codecs_v = {"h264", "h265", "av1"}
local mp4_codecs_a = {"opus", "aac", "flac", "mp3"}
local audio_only = {mp3 = ".mp3", flac = ".flac", aac = ".mp4"}
function contains(list, x)
for k, v in pairs(list) do
if v == x then return true end
end
return false
end
function get_format(vcodec, acodec)
if vcodec == nil then -- audio only
return audio_only[acodec] or ".ogg" -- vorbis, opus
elseif acodec == nil then -- video only
if contains(webm_codecs_v, vcodec) then return ".webm"
elseif contains(mp4_codecs_v, vcodec) then return ".mp4" end
elseif contains(webm_codecs_v, vcodec) and contains(webm_codecs_a, acodec) then
return ".webm"
elseif contains(mp4_codecs_v, vcodec) and contains(mp4_codecs_a, acodec) then
return ".mp4"
end
return ".mkv"
end
function handler()
if mp.get_property("ab-loop-a") == "no" or mp.get_property("ab-loop-b") == "no" then
mp.commandv("show-text", "Couldn't save clip: A-B loop not set")
else
local dir = mp.get_property("screenshot-directory", "")
local format = get_format(mp.get_property_native("video-format"), mp.get_property_native("audio-codec-name"))
local filename = mp.command_native({"expand-path", dir .. "clipsegment-" .. os.time() .. format})
if filename then
mp.commandv("show-text", "Saving to: " .. filename)
mp.commandv("print-text", "[clipsegment] Saving to: " .. filename)
if not mp.commandv("ab-loop-dump-cache", filename) then
mp.commandv("show-text", "Error while saving, check console")
end
end
end
end
mp.add_key_binding("o", "clip-segment", handler)