SHARE
TWEET

encode.lua

a guest May 25th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local start_timestamp = nil
  2. local utils = require "mp.utils"
  3. local options = require "mp.options"
  4. local msg = require "mp.msg"
  5.  
  6. local o = {
  7.     -- if true, the ffmpeg process will be detached and we won't know if it
  8.     -- succeeded or not and we can stop mpv at any time
  9.     -- if false, we know the result of calling ffmpeg, but we can only encode
  10.     -- one extract at a time and mpv will block on exit
  11.     detached = false,
  12.     -- if true, the current working directory of mpv is used for the output
  13.     -- if false, the directory of the input is used
  14.     use_current_working_dir = true
  15. }
  16. options.read_options(o)
  17.  
  18. function get_title()
  19.     local title = mp.get_property("title")
  20.     mp.msg.info(title)
  21.     mp.osd_message(title)
  22. end
  23.  
  24. function file_exists(name)
  25.     local f = io.open(name, "r")
  26.     if f ~= nil then
  27.         io.close(f)
  28.         return true
  29.     else
  30.         return false
  31.     end
  32. end
  33.  
  34. function get_unused_filename(dir)
  35.     local res = utils.readdir(dir)
  36.     local files = {}
  37.     for _, f in ipairs(res) do
  38.         files[f] = true
  39.     end
  40.     local i = 1
  41.     local suffix = ".webm"
  42.     while true do
  43.         local potential_name = string.format("%d%s", i, suffix)
  44.         if not files[potential_name] then
  45.             return i
  46.         end
  47.         i = i + 1
  48.     end
  49. end
  50.  
  51. function start_encoding(path, from, to, cam)
  52.     local filename = mp.get_property("filename/no-ext") or "encode"
  53.  
  54.     local args = {
  55.         "webm-lua", path, from, to
  56.     }
  57.  
  58.     -- path of the output
  59.     local directory = "."
  60.     local desktop = "/Users/[YOURUSERNAMEHERE]/Desktop/"
  61.     if not o.use_current_working_dir then
  62.         directory, _ = utils.split_path(path)
  63.     end
  64.     local output = get_unused_filename(desktop)
  65.     args[#args + 1] = output
  66.     args[#args + 1] = cam
  67.    
  68.     print(table.concat(args, " "))
  69.     if o.detached then
  70.         utils.subprocess_detached({ args = args })
  71.     else
  72.         local res = utils.subprocess({ args = args, max_size = 0, cancellable = false })
  73.         if res.status == 0 then
  74.             mp.osd_message("Finished encoding succesfully")
  75.             mp.msg.info("Finished encoding succesfully")
  76.         else
  77.             mp.osd_message("Failed to encode, check the log")
  78.             mp.msg.info("Failed to encode, check the log")
  79.         end
  80.     end
  81. end
  82.  
  83. function set_timestamp()
  84.     local path = mp.get_property("path")
  85.     local cam
  86.     if not path then
  87.         mp.osd_message("No file currently playing")
  88.         return
  89.     end
  90.     if not file_exists(path) then
  91.          local path = mp.get_property("path")
  92.        mp.msg.info(path)
  93.     end
  94.     if start_timestamp == nil then
  95.         mp.osd_message("Start timestamp set")
  96.         start_timestamp = mp.get_property_number("time-pos")
  97.             mp.msg.info(start_timestamp)
  98.     else
  99.         local current_timestamp = mp.get_property_number("time-pos")
  100.         if current_timestamp <= start_timestamp then
  101.             mp.osd_message("Second timestamp cannot be before the first")
  102.             return
  103.         end
  104.         mp.osd_message("End timestamp set, encoding...")
  105.  
  106.         start_encoding(path, start_timestamp, current_timestamp, cam)
  107.         start_timestamp = nil
  108.     end
  109. end
  110.  
  111. function clear_timestamp()
  112.     start_timestamp = nil
  113.     mp.osd_message("Timestamp cleared")
  114.     mp.msg.info("Timestamp cleared")
  115. end
  116.  
  117. mp.add_key_binding(z, "get_title", get_title)
  118. mp.add_key_binding(nil, "set_timestamp", set_timestamp)
  119. mp.add_key_binding(nil, "clear_timestamp", clear_timestamp)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top