local mark_in = nil

local function fmt_hms(t)
    local h = math.floor(t/3600)
    local m = math.floor((t%3600)/60)
    local s = t - h*3600 - m*60
    return string.format("%02d:%02d:%06.3f", h, m, s)
end

local function mark_start()
    mark_in = mp.get_property_number("time-pos")
    mp.osd_message("IN: " .. fmt_hms(mark_in))
end

local function mark_end()
    local path = mp.get_property("path")
    local filename = path:match("([^/\\]+)$") or path
    local time_out = mp.get_property_number("time-pos")
    local dir = mp.get_property("working-directory") or "."
    local f = io.open(dir .. "/clips.txt", "a")
    if mark_in then
        f:write(string.format("file '%s' %s-%s\n", filename, fmt_hms(mark_in), fmt_hms(time_out)))
        mp.osd_message("Clip: " .. fmt_hms(mark_in) .. " - " .. fmt_hms(time_out))
        mark_in = nil
    else
        f:write(string.format("file '%s'\n", filename))
        mp.osd_message("Ganzer Clip -> clips.txt")
    end
    f:close()
end

mp.add_key_binding("i", "mark_in", mark_start)
mp.add_key_binding("o", "mark_out", mark_end)
