-- SplitWithMkvMerge.lua -- VLC extension --
--[[
INSTALLATION:
Put the file in the VLC subdir /lua/extensions, by default:
* Windows (all users): %ProgramFiles%\VideoLAN\VLC\lua\extensions\
* Windows (current user): %APPDATA%\VLC\lua\extensions\
(create directories if they don't exist)
Restart the VLC.
Then you simply use the extension by going to the "View" menu and selecting it.
USAGE:
Clicking the "Create timestamps" button while playing a movie allows you to write a "start timestamp" at the present played time, clicking a second time
adds an "end timestamp". This two timestamps mark the time range and start time of your split. You can add more of such time ranges but you
have to make sure that the last stamp is an "end timestamp" and that no time ranges are overlapping. (mkvMerge only allows them in sequence!)
If you got all needed time ranges simply click on the "> Auto split" button to send them to mkvMerge.
If everything is set up correctly it should create the video clips automatically in the same directory as the source video file. (Adding suffixes 001,002)
You also can use the "> Clipboard" button to only copy the timestamps into the clipboard. This will allow you to insert them manually into mkvToolnix gui
(output->splitting/by parts based on timestamps) if you want more options (remove subtitles, audio tracks etc.).
Note:
This extension has been tested for VLC 3.0.4 on Win7 x64 sp1.
It needs mkvMerge.exe which is part of the mkvToolnix set. Download: https://mkvtoolnix.download/downloads.html#windows
Once installed, go to the mkvToolnix directory and shift+right click on mkvMerge.exe. "Copy as path" appears in the context menu. Copy the path,
start VLC and the extension to paste the path into the text field of the first run routine of this extension.
Because the function save_to_clipboard needs clip.exe, you've to make sure your windows system has it (just type "echo a |clip" in the cmd prompt
window to check whether letter "a" has been put in the system clipboard). If it doesn't exist, you can download it from here:
http://www.labnol.org/software/tutorials/copy-dos-command-line-output-clipboard-clip-exe/2506/
--]]
function descriptor()
return {
title = "Split video with mkvToolnix";
version = "1.01";
author = "elisacol";
url = '';
shortdesc = "Split video with mkvToolnix";
description = "
Send timestamps to mkvToolnix for cutting/splitting videos.
";
}
end
function activate()
-- (for testing) mkvMerge_path = "c:/program files/mkvtoolnix/mkvmerge.exe"
item = vlc.input.item()
uri = item:uri()
uri = string.gsub(uri, '^file:///', '')
uri = string.gsub(uri, '/', '\\')
uri = string.gsub(uri, '%%20', '\032')
extfile = io.open(vlc.config.datadir().."\\lua\\extensions\\mkvMergePath.txt", "r")
if extfile == nil
then
extfile = io.output (vlc.config.datadir().."\\lua\\extensions\\mkvMergePath.txt")
extfile = io.open(vlc.config.datadir().."\\lua\\extensions\\mkvMergePath.txt", "r")
end
if extfile
then
mkvMerge_path = extfile:read("*all")
extfile:close()
if mkvMerge_path == ""
then
first_dialog()
else
create_dialog()
end
end
end
function deactivate()
end
function meta_changed()
end
function close()
vlc.deactivate()
end
function first_dialog()
w = vlc.dialog("First run routine")
w1 = w:add_label("Insert path to mkvMerge:",1,1,5,1)
w2 = w:add_label("Example: c:/program files/mkvtoolnix/mkvmerge.exe - (shift + right click mkvMerge.exe -> Copy path)",1,2,5,1)
uinput = w:add_text_input("",1,3,19,1)
w3 = w:add_button("Save", save_path ,15,4,5,1)
end
function save_path()
mpath = uinput:get_text()
input = vlc.object.input()
if input
then
mpath = string.gsub(mpath, '\"', '')
mpath = string.gsub(mpath, '\\', '/')
extfile = io.output(vlc.config.datadir().."\\lua\\extensions\\mkvMergePath.txt", "w")
io.write(mpath)
extfile:close()
activate()
end
end
i = 1
a = 1
b = 0
function create_dialog()
if w
then
w:delete()
end
w = vlc.dialog("Split video with mkvToolnix")
w1 = w:add_label("Timestamps\nStart/End:",1,1,1,1)
w2 = w:add_text_input("0",2,1,1,1)
w3 = w:add_button("> Clipboard", send_Clip,3,1,1,1)
w0 = w:add_button("Delete all", delete_all,1,2,1,1)
if a % 2 ~= 0
then
w4 = w:add_button("Create start point (".. a ..")", click_SAVE,2,2,1,1)
w5 = w:add_button("> Auto Split ", send_Split,3,2,1,1)
a = a + 1
else
w4 = w:add_button("Create end point (".. a - 1 ..")", click_SAVE,2,2,1,1)
a = a + 1
end
if i % 2 ~= 0
then
w6 = w:add_button ("Show Help", show_hide,1,3,3,1)
end
end
number = 0
tpool={}
function delete_all()
local input = vlc.object.input()
if input then
timestamps = 0
number = 0
i = 1
a = 1
b = 0
tpool={}
clips={}
clip_line = 0
clipString = 0
diff_time = 0
a_clip = 0
b_clip = 0
activate()
end
end
function show_hide()
local input = vlc.object.input()
if input then
if i % 2 ~= 0
then
w6 = w:add_button ("Hide Help", show_hide,1,3,3,1)
w7 = w:add_html("- While watching or pausing the video click once on Create start point to create a start timestamp. Click a second time to create an end stamp. The created time range will be used to cut out your clip.
- You can click as often as you want to mark time ranges but you have to make sure that no time ranges overlap. mkvMerge only accepts time ranges in a chronological sequence!
- Click on > Auto Clip to send the clip list to mkvMerge. It will instantely begin to split the clips out of your video outputting them into the video source directory.
- If you want more options - like removing audio tracks or subtitles - use > Clipboard to copy the timestamps to the clipboard. They are correctly formatted and are ready to get pasted in mkvToolnix gui's output dialog. (splitting/by parts based on timestamps)
- Have fun! :)
",1,5,5,1)
i = i + 1
w:update()
elseif (i % 2 == 0) then
w6 = w:add_button ("Show Help", show_hide,1,3,3,1)
w:del_widget( w7 )
i = i + 1
w:update()
end
end
end
clips={}
function click_SAVE()
local input = vlc.object.input()
if input
then
local curtime=vlc.var.get(input, "time")
local curtime=math.floor(curtime/1000)
local curtime=curtime/1000
local hours = math.floor(curtime/3600)
local minutes = math.floor((curtime%3600)/60)
local seconds = math.floor(curtime%60)
if number % 2 == 0
then
timeString = string.format("%02d:%02d:%02d-",hours,minutes,seconds)
else
timeString = string.format("%02d:%02d:%02d,",hours,minutes,seconds)
end
table.insert(tpool, timeString)
finalString = table.concat( tpool )
finalString = finalString:sub(1, -2)
w2:set_text(finalString)
if a % 2 ~= 0
then
w4 = w:add_button("Create start point (".. (a + 1)/2 ..")", click_SAVE,2,2,1,1)
w5 = w:add_button("> Auto Split ", send_Split,3,2,1,1)
b = b + 1
a = a + 1
clipString = string.sub(finalString, -17)
clipString = string.gsub(clipString, '-', ' - ')
a_clip = string.sub(clipString,1 , 8)
b_clip = string.sub(clipString,12, 19)
ax = string.sub(a_clip,1,2)
ay = string.sub(a_clip,4,5)
az = string.sub(a_clip,-2)
bx = string.sub(b_clip,1,2)
by = string.sub(b_clip,4,5)
bz = string.sub(b_clip,-2)
ax = tonumber(ax)
ay = tonumber(ay)
az = tonumber(az)
bx = tonumber(bx)
by = tonumber(by)
bz = tonumber(bz)
a_time = (ax * 3600) + (ay * 60) + az
b_time = (bx * 3600) + (by * 60) + bz
diff = b_time - a_time
diff_form_h = math.floor (diff / 3600)
diff_form_m = math.floor ((diff - (diff_form_h * 3600))/60)
diff_form_s = diff-(diff_form_h*3600)-(diff_form_m*60)
diff_time = string.format("%2dh %2dm %2ds",diff_form_h, diff_form_m, diff_form_s)
clip_line = "Clip: "..clipString.." -> "..diff_time..""
table.insert(clips, clip_line)
clip_list = table.concat( clips )
w6 = w:add_html("Clips to get split:
"..clip_list.."
",4,1,1,4)
w:update()
else
w4 = w:add_button("Create end point (".. a/2 ..")", click_SAVE,2,2,1,1)
w:del_widget( w5 )
b = b + 1
a = a + 1
w:update()
end
number = number + 1
end
end
function send_Clip()
timeStamps = w2:get_text()
input = vlc.object.input()
if input
then
strCmd = 'echo | set /p dummyName='..timeStamps..'|clip'
os.execute(strCmd)
end
end
function send_Split()
timeStamps = w2:get_text()
input = vlc.object.input()
if input
then
-- strCmd = [[("D:/PortablePrograms/mkv toolnix\mkvmerge.exe" --output "C:\Users\Leonidas\Desktop\Neuer Ordner\life - ball et uii idhsj (1).mkv" "(" "C:\Users\Leonidas\Desktop\Neuer Ordner\life - ball et uii idhsj.mkv" ")" --split parts:00:01:22-00:02:22,00:02:23-00:02:40)]]
strCmd = [[("]]..mkvMerge_path..[[" --output "]]..uri..[[.mkv" "(" "]]..uri..[[" ")" --split parts:]]..timeStamps..[[)]]
-- strCmd = mkvMerge_path..' --output \"'..uri..'.mkv\" \"'..uri..'\" --split parts:'..timeStamps
os.execute(strCmd)
end
end