Skip to content

Instantly share code, notes, and snippets.

@Moodkiller
Forked from The0x539/0x.EncodeClip.lua
Last active May 5, 2020 21:17
Show Gist options
  • Save Moodkiller/7947619c2e42904cf17618045c956fe8 to your computer and use it in GitHub Desktop.
Save Moodkiller/7947619c2e42904cf17618045c956fe8 to your computer and use it in GitHub Desktop.
-- Copyright (c) 2020, The0x539 <[email protected]>
--
-- Permission to use, copy, modify, and distribute this software for any
-- purpose with or without fee is hereby granted, provided that the above
-- copyright notice and this permission notice appear in all copies.
--
-- THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
local tr = aegisub.gettext
script_name = tr'Make clip'
script_description = tr'Encode a hardsubbed clip encompassing the current selection'
script_author = 'The0x539, mod by MK'
script_version = '0.1.1'
function make_clip(subs, sel, _)
local t1, t2 = math.huge, 0
for _, i in ipairs(sel) do
t1 = math.min(t1, subs[i].start_time)
t2 = math.max(t2, subs[i].end_time)
end
local t1, t2 = t1/1000, t2/1000
local mpv = "mpv"
local props = aegisub.project_properties()
local vid = props.video_file
local video = aegisub.decode_path("?video").."\\" .. aegisub.project_properties().video_file:gsub("^.*\\","")
local subs = aegisub.decode_path("?script\\") .. aegisub.file_name()
--local out = vid:gsub('.m[kp][v4]$', '') .. ('_%.3f-%.3f'):format(t1, t2) .. '.mp4'
local out = aegisub.decode_path("?video").."\\" .. aegisub.file_name():sub(1, -5) .. ('_%.3f-%.3f'):format(t1, t2) .. '.mp4'
--local out = video .. ('_%.3f-%.3f'):format(t1, t2) .. '.mp4'
local cmd = table.concat({
quo(mpv),
'--sub-font-provider=auto',
'--start=%.3f',
'--end=%.3f',
quo(video),
'--sub-file='..quo(subs),
'--vf=format=yuv420p',
'--o='..quo(out),
'\n@pause'
}, ' '):format(t1, t2, vid, subs, out)
aegisub.log(cmd)
--local tmp = aegisub.decode_path('?video\\tmp.bat')
local tmp = aegisub.decode_path('?video') .. "\\" .. aegisub.file_name():sub(1, -5) .. ('_%.3f-%.3f'):format(t1, t2) .. '.bat'
local f = io.open(tmp, 'w')
f:write(cmd)
f:close()
local p = io.popen(tmp)
local output = p:read('*a')
aegisub.log(output)
p:close()
os.execute('del ' .. tmp)
end
-- This little function takes a string and wraps it in quotation marks.
function quo(x) x="\""..x.."\"" return x end
aegisub.register_macro(script_name, script_description, make_clip)
@Moodkiller
Copy link
Author

Change log: 0.1.1 (I'm probably doing this wrong...)

  • Added variable to define mpv path / mpv.exe
  • Added variable to get currently loaded video path+file name instead of using subfolder path (i.e attempt to avoid long path names that get concatenated)
  • Changed output path to currently loaded video path
  • Updated variables in "cmd" section
  • Added @Pause to .bat script
  • Changed filename generated for each .bat to represent the video and subfile being used (i.e can be re-run as the .bats) will not be overwritten.
  • Added quotes function to automatically insert quotes around what ever.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment