Advertisement
Guest User

encode.lua

a guest
May 25th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.77 KB | None | 0 0
  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.  
  95.     --if string.match(path, "5.webm") then
  96.     if string.match(path, "http://nlds.*.cdnak.neulion.com/.*") then
  97.         local title = mp.get_property("title")
  98.         path, cam = string.match(title, "(%S*)%s?(%d?)")
  99.         mp.msg.info("Path = ", path)
  100.         mp.msg.info("CamID = ", cam)
  101.     end
  102.     --]]
  103.  
  104.     if start_timestamp == nil then
  105.         mp.osd_message("Start timestamp set")
  106.         start_timestamp = mp.get_property_number("time-pos")
  107.             mp.msg.info(start_timestamp)
  108.     else
  109.         local current_timestamp = mp.get_property_number("time-pos")
  110.         if current_timestamp <= start_timestamp then
  111.             mp.osd_message("Second timestamp cannot be before the first")
  112.             return
  113.         end
  114.         mp.osd_message("End timestamp set, encoding...")
  115.  
  116.         start_encoding(path, start_timestamp, current_timestamp, cam)
  117.         start_timestamp = nil
  118.     end
  119. end
  120.  
  121. function clear_timestamp()
  122.     start_timestamp = nil
  123.     mp.osd_message("Timestamp cleared")
  124.     mp.msg.info("Timestamp cleared")
  125. end
  126.  
  127. mp.add_key_binding(z, "get_title", get_title)
  128. mp.add_key_binding(nil, "set_timestamp", set_timestamp)
  129. mp.add_key_binding(nil, "clear_timestamp", clear_timestamp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement