Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local start_timestamp = nil
- local utils = require "mp.utils"
- local options = require "mp.options"
- local msg = require "mp.msg"
- local o = {
- -- if true, the ffmpeg process will be detached and we won't know if it
- -- succeeded or not and we can stop mpv at any time
- -- if false, we know the result of calling ffmpeg, but we can only encode
- -- one extract at a time and mpv will block on exit
- detached = false,
- -- if true, the current working directory of mpv is used for the output
- -- if false, the directory of the input is used
- use_current_working_dir = true
- }
- options.read_options(o)
- function get_title()
- local title = mp.get_property("title")
- mp.msg.info(title)
- mp.osd_message(title)
- end
- function file_exists(name)
- local f = io.open(name, "r")
- if f ~= nil then
- io.close(f)
- return true
- else
- return false
- end
- end
- function get_unused_filename(dir)
- local res = utils.readdir(dir)
- local files = {}
- for _, f in ipairs(res) do
- files[f] = true
- end
- local i = 1
- local suffix = ".webm"
- while true do
- local potential_name = string.format("%d%s", i, suffix)
- if not files[potential_name] then
- return i
- end
- i = i + 1
- end
- end
- function start_encoding(path, from, to, cam)
- local filename = mp.get_property("filename/no-ext") or "encode"
- local args = {
- "webm-lua", path, from, to
- }
- -- path of the output
- local directory = "."
- local desktop = "/Users/[YOURUSERNAMEHERE]/Desktop/"
- if not o.use_current_working_dir then
- directory, _ = utils.split_path(path)
- end
- local output = get_unused_filename(desktop)
- args[#args + 1] = output
- args[#args + 1] = cam
- print(table.concat(args, " "))
- if o.detached then
- utils.subprocess_detached({ args = args })
- else
- local res = utils.subprocess({ args = args, max_size = 0, cancellable = false })
- if res.status == 0 then
- mp.osd_message("Finished encoding succesfully")
- mp.msg.info("Finished encoding succesfully")
- else
- mp.osd_message("Failed to encode, check the log")
- mp.msg.info("Failed to encode, check the log")
- end
- end
- end
- function set_timestamp()
- local path = mp.get_property("path")
- local cam
- if not path then
- mp.osd_message("No file currently playing")
- return
- end
- if not file_exists(path) then
- local path = mp.get_property("path")
- mp.msg.info(path)
- end
- --if string.match(path, "5.webm") then
- if string.match(path, "http://nlds.*.cdnak.neulion.com/.*") then
- local title = mp.get_property("title")
- path, cam = string.match(title, "(%S*)%s?(%d?)")
- mp.msg.info("Path = ", path)
- mp.msg.info("CamID = ", cam)
- end
- --]]
- if start_timestamp == nil then
- mp.osd_message("Start timestamp set")
- start_timestamp = mp.get_property_number("time-pos")
- mp.msg.info(start_timestamp)
- else
- local current_timestamp = mp.get_property_number("time-pos")
- if current_timestamp <= start_timestamp then
- mp.osd_message("Second timestamp cannot be before the first")
- return
- end
- mp.osd_message("End timestamp set, encoding...")
- start_encoding(path, start_timestamp, current_timestamp, cam)
- start_timestamp = nil
- end
- end
- function clear_timestamp()
- start_timestamp = nil
- mp.osd_message("Timestamp cleared")
- mp.msg.info("Timestamp cleared")
- end
- mp.add_key_binding(z, "get_title", get_title)
- mp.add_key_binding(nil, "set_timestamp", set_timestamp)
- mp.add_key_binding(nil, "clear_timestamp", clear_timestamp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement