Guest User

Untitled

a guest
Oct 4th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.30 KB | None | 0 0
  1. local msg = require "mp.msg"
  2. local options = require "mp.options"
  3.  
  4. local cut_pos = nil
  5. local copy_audio = false
  6. local o = {
  7.     target_dir = "~/video",
  8.     vcodec = "rawvideo",
  9.     acodec = "pcm_s16le",
  10.     prevf = "",
  11.     vf = "scale=in_color_matrix=$matrix,format=bgr24",
  12.     postvf = "",
  13.     opts = "",
  14.     ext = "avi",
  15.     command_template = [[
  16.         ffmpeg -loglevel warning
  17.         -ss $shift -i '$in' -t $duration
  18.         -c:v $vcodec -c:a $acodec $audio
  19.         -vf $prevf$vf$postvf $opts -y '$out.$ext'
  20.     ]],
  21. }
  22. options.read_options(o)
  23.  
  24. function timestamp(duration)
  25.     local hours = duration / 3600
  26.     local minutes = duration % 3600 / 60
  27.     local seconds = duration % 60
  28.     return string.format("%02d:%02d:%02.03f", hours, minutes, seconds)
  29. end
  30.  
  31. function log(str)
  32.     return mp.osd_message(str)
  33. end
  34.  
  35. function escape(str)
  36.     return str:gsub("'", "'\\''")
  37. end
  38.  
  39. function get_csp()
  40.     local csp = mp.get_property("colormatrix")
  41.     if csp == "bt.601" then return "bt601"
  42.         elseif csp == "bt.709" then return "bt709"
  43.         elseif csp == "smpte-240m" then return "smpte240m"
  44.         else error("unknown colorspace " .. csp)
  45.     end
  46. end
  47.  
  48. function get_outname(shift, endpos)
  49.     local name = mp.get_property("filename")
  50.     local dotidx = name:reverse():find(".", 1, true)
  51.     if dotidx then name = name:sub(1, -dotidx-1) end
  52.     name = name:gsub(" ", "_")
  53.     name = name .. string.format(".%s-%s", timestamp(shift), timestamp(endpos))
  54.     return name
  55. end
  56.  
  57. function cut(shift, endpos)
  58.     local cmd = o.command_template:gsub("%s+", " ")
  59.     local inpath = escape(mp.get_property("path"))
  60.     -- TODO: Windows?
  61.     local outpath = escape(string.format(
  62.         "%s/%s",
  63.         o.target_dir:gsub("~", os.getenv("HOME")),
  64.         get_outname(shift, endpos)))
  65.  
  66.     cmd = cmd:gsub("$shift", shift)
  67.     cmd = cmd:gsub("$duration", endpos - shift)
  68.     cmd = cmd:gsub("$vcodec", o.vcodec)
  69.     cmd = cmd:gsub("$acodec", o.acodec)
  70.     cmd = cmd:gsub("$audio", copy_audio and "" or "-an")
  71.     cmd = cmd:gsub("$prevf", o.prevf)
  72.     cmd = cmd:gsub("$vf", o.vf)
  73.     cmd = cmd:gsub("$postvf", o.postvf)
  74.     cmd = cmd:gsub("$matrix", get_csp())
  75.     cmd = cmd:gsub("$opts", o.opts)
  76.     -- Beware that input/out filename may contain replacing patterns.
  77.     cmd = cmd:gsub("$ext", o.ext)
  78.     cmd = cmd:gsub("$out", outpath)
  79.     cmd = cmd:gsub("$in", inpath, 1)
  80.  
  81.     msg.info(cmd)
  82.     os.execute(cmd)
  83. end
  84.  
  85. function toggle_mark()
  86.     local pos = mp.get_property_number("time-pos")
  87.     if cut_pos then
  88.         local shift, endpos = cut_pos, pos
  89.         if shift > endpos then
  90.             shift, endpos = endpos, shift
  91.         end
  92.         if shift == endpos then
  93.             log("Cut fragment is empty")
  94.         else
  95.             cut_pos = nil
  96.             log(string.format(
  97.                 "Cut fragment: %s - %s",
  98.                 timestamp(shift), timestamp(endpos)))
  99.             cut(shift, endpos)
  100.         end
  101.     else
  102.         cut_pos = pos
  103.         log(string.format("Marked %s as start position", timestamp(pos)))
  104.     end
  105. end
  106.  
  107. function toggle_audio()
  108.     copy_audio = not copy_audio
  109.     log("Audio slicing is " .. (copy_audio and "enabled" or "disabled"))
  110. end
  111.  
  112. mp.add_key_binding("c", "slicing_mark", toggle_mark)
  113. mp.add_key_binding("a", "slicing_audio", toggle_audio)
Advertisement
Add Comment
Please, Sign In to add comment