Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.48 KB | None | 0 0
  1. -- SplitWithMkvMerge.lua -- VLC extension --
  2. --[[
  3. INSTALLATION:
  4. Put the file in the VLC subdir /lua/extensions, by default:
  5. * Windows (all users): %ProgramFiles%\VideoLAN\VLC\lua\extensions\
  6. * Windows (current user): %APPDATA%\VLC\lua\extensions\
  7. (create directories if they don't exist)
  8. Restart the VLC.
  9. Then you simply use the extension by going to the "View" menu and selecting it.
  10. USAGE:
  11. 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
  12. 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
  13. 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!)
  14. If you got all needed time ranges simply click on the "> Auto split" button to send them to mkvMerge.
  15. 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)
  16. 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
  17. (output->splitting/by parts based on timestamps) if you want more options (remove subtitles, audio tracks etc.).
  18. Note:
  19.     This extension has been tested for VLC 3.0.4 on Win7 x64 sp1.
  20.     It needs mkvMerge.exe which is part of the mkvToolnix set. Download: https://mkvtoolnix.download/downloads.html#windows
  21.     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,
  22.     start VLC and the extension to paste the path into the text field of the first run routine of this extension.
  23.  
  24.     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
  25.     window to check whether letter "a" has been put in the system clipboard). If it doesn't exist, you can download it from here:
  26.     http://www.labnol.org/software/tutorials/copy-dos-command-line-output-clipboard-clip-exe/2506/
  27.  
  28.  
  29. --]]
  30.  
  31.  
  32. function descriptor()
  33.     return {
  34.         title = "Split video with mkvToolnix";
  35.         version = "1.0";
  36.         author = "elisacol";
  37.         url = '';
  38.         shortdesc = "Split video with mkvToolnix";
  39.         description = "<div style=\"background-color:lightgreen;\"><b>Send timestamps to mkvToolnix for cutting/splitting videos.</b></div>";
  40. }
  41. end
  42.  
  43. function activate()
  44. -- (for testing) mkvMerge_path = "c:/program files/mkvtoolnix/mkvmerge.exe"
  45.  
  46.     item = vlc.input.item()
  47.     uri = item:uri()
  48.     uri = string.gsub(uri, '^file:///', '')
  49.     uri = string.gsub(uri, '/', '\\')
  50.     uri = string.gsub(uri, '%%20', '\032')
  51.     extfile = io.open(vlc.config.datadir().."\\lua\\extensions\\mkvMergePath.txt", "r")
  52.     if extfile == nil
  53.         then
  54.             extfile = io.output (vlc.config.datadir().."\\lua\\extensions\\mkvMergePath.txt")
  55.             extfile = io.open(vlc.config.datadir().."\\lua\\extensions\\mkvMergePath.txt", "r")
  56.     end
  57.     if extfile
  58.         then
  59.             mkvMerge_path = extfile:read("*all")
  60.             extfile:close()
  61.             if mkvMerge_path == ""
  62.                 then
  63.  
  64.                     first_dialog()
  65.  
  66.             else
  67.  
  68.                     create_dialog()
  69.             end
  70.     end
  71. end
  72.  
  73. function deactivate()
  74. end
  75. function meta_changed()
  76. end
  77. function close()
  78.     vlc.deactivate()
  79. end
  80.  
  81.  
  82.  
  83. function first_dialog()
  84.     w = vlc.dialog("First run routine")
  85.     w1 = w:add_label("<b>Insert path to mkvMerge:</b>",1,1,5,1)
  86.     w2 = w:add_label("Example: c:/program files/mkvtoolnix/mkvmerge.exe - (shift + right click mkvMerge.exe -> Copy path)",1,2,5,1)
  87.     uinput = w:add_text_input("",1,3,19,1)
  88.     w3 = w:add_button("Save", save_path ,15,4,5,1)
  89. end
  90.  
  91. function save_path()
  92.     mpath = uinput:get_text()
  93.     input = vlc.object.input()
  94.     if input
  95.         then
  96.             mpath = string.gsub(mpath, '\"', '')
  97.             mpath = string.gsub(mpath, '\\', '/')
  98.             extfile = io.output(vlc.config.datadir().."\\lua\\extensions\\mkvMergePath.txt", "w")
  99.             io.write(mpath)
  100.             extfile:close()
  101.             activate()
  102.     end
  103. end
  104. i = 1
  105. a = 1
  106. b = 0
  107.  
  108. function create_dialog()
  109.     if w
  110.         then
  111.             w:delete()
  112.     end
  113.  
  114.  
  115.  
  116.     w = vlc.dialog("Split video with mkvToolnix")
  117.     w1 = w:add_label("<b>Timestamps\nStart/End:</b>",1,1,1,1)
  118.     w2 = w:add_text_input("0",2,1,1,1)
  119.     w3 = w:add_button("> Clipboard", send_Clip,3,1,1,1)
  120.  
  121.     if a % 2 ~= 0
  122.         then
  123.             w4 = w:add_button("Create start point (".. a ..")", click_SAVE,2,2,1,1)
  124.             w5 = w:add_button("> Auto Split ", send_Split,3,2,1,1)
  125.  
  126.             a = a + 1
  127.     else
  128.             w4 = w:add_button("Create end point (".. a - 1 ..")", click_SAVE,2,2,1,1)
  129.             a = a + 1
  130.     end
  131.     if i % 2 ~= 0
  132.         then
  133.            w6 = w:add_button ("Show Help", show_hide,1,3,3,1)
  134.     end
  135. end
  136. number = 0
  137. tpool={}
  138.  
  139.  
  140. function show_hide()
  141.     local input = vlc.object.input()
  142.     if input then
  143.         if i % 2 ~= 0
  144.             then
  145.                 w6 = w:add_button ("Hide Help", show_hide,1,3,3,1)
  146.                 w7 = w:add_html("<ul><li>While watching or pausing the video click once on <b>Create start point</b> 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.<br></li><li>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!<br></li><li>Click on <b>> Auto Clip</b> 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.<br></li><li>If you want more options - like removing audio tracks or subtitles - use <b>> Clipboard</b> 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)<br></li><li>Have fun! :)</li></ul>",1,5,5,1)
  147.                 i = i + 1
  148.                 w:update()
  149.         elseif (i % 2 == 0) then
  150.                 w6 = w:add_button ("Show Help", show_hide,1,3,3,1)
  151.                 w:del_widget( w7 )
  152.                 i = i + 1
  153.                 w:update()
  154.         end
  155.     end
  156. end
  157. clips={}
  158. function click_SAVE()
  159.     local input = vlc.object.input()
  160.     if input
  161.         then
  162.             local curtime=vlc.var.get(input, "time")
  163.             local curtime=math.floor(curtime/1000)
  164.             local curtime=curtime/1000
  165.             local hours = math.floor(curtime/3600)
  166.             local minutes = math.floor((curtime%3600)/60)
  167.             local seconds = math.floor(curtime%60)
  168.  
  169.         if number % 2 == 0
  170.             then
  171.                 timeString = string.format("%02d:%02d:%02d-",hours,minutes,seconds)
  172.         else
  173.                 timeString = string.format("%02d:%02d:%02d,",hours,minutes,seconds)
  174.         end
  175.  
  176.         table.insert(tpool, timeString)
  177.         finalString = table.concat( tpool )
  178.         finalString = finalString:sub(1, -2)
  179.         w2:set_text(finalString)
  180.  
  181.         if a % 2 ~= 0
  182.             then
  183.                 w4 = w:add_button("Create start point (".. (a + 1)/2 ..")", click_SAVE,2,2,1,1)
  184.                 w5 = w:add_button("> Auto Split ", send_Split,3,2,1,1)
  185.                 b = b + 1
  186.                 a = a + 1
  187.                 clipString = string.sub(finalString, -17)
  188.                 clipString = string.gsub(clipString, '-', ' - ')
  189.                 a_clip = string.sub(clipString,1 , 8)
  190.                 b_clip = string.sub(clipString,12, 19)
  191.                 ax = string.sub(a_clip,1,2)
  192.                 ay = string.sub(a_clip,4,5)
  193.                 az = string.sub(a_clip,-2)
  194.                 bx = string.sub(b_clip,1,2)
  195.                 by = string.sub(b_clip,4,5)
  196.                 bz = string.sub(b_clip,-2)
  197.                 ax = tonumber(ax)
  198.                 ay = tonumber(ay)
  199.                 az = tonumber(az)
  200.                 bx = tonumber(bx)
  201.                 by = tonumber(by)
  202.                 bz = tonumber(bz)
  203.                 a_time = (ax * 3600) + (ay * 60) + az
  204.                 b_time = (bx * 3600) + (by * 60) + bz
  205.                 diff = b_time - a_time
  206.                 diff_form_h = math.floor (diff / 3600)
  207.                 diff_form_m = math.floor ((diff - (diff_form_h * 3600))/60)
  208.                 diff_form_s = diff-(diff_form_h*3600)-(diff_form_m*60)
  209.  
  210.                 diff_time = string.format("%2dh %2dm %2ds",diff_form_h, diff_form_m, diff_form_s)
  211.  
  212.                 clip_line = "<li>Clip: "..clipString.." -> "..diff_time.."</li>"
  213.                 table.insert(clips, clip_line)
  214.                 clip_list = table.concat( clips )
  215.  
  216.                 w6 = w:add_html("<div>Clips to get split:</div><ol>"..clip_list.."</ol>",4,1,1,4)
  217.  
  218.                 w:update()
  219.         else
  220.                 w4 = w:add_button("Create end point (".. a/2 ..")", click_SAVE,2,2,1,1)
  221.                 w:del_widget( w5 )
  222.                 b = b + 1
  223.                 a = a + 1
  224.                 w:update()
  225.         end
  226.  
  227.         number = number + 1
  228.     end
  229. end
  230.  
  231. function send_Clip()
  232.     timeStamps = w2:get_text()
  233.     input = vlc.object.input()
  234.     if input
  235.         then
  236.             strCmd = 'echo | set /p dummyName='..timeStamps..'|clip'
  237.             os.execute(strCmd)
  238.     end
  239. end
  240.  
  241. function send_Split()
  242.     timeStamps = w2:get_text()
  243.     input = vlc.object.input()
  244.  
  245.  
  246.     if input
  247.         then
  248.             strCmd = mkvMerge_path..' --output "'..uri..'.mkv" "'..uri..'" --split parts:'..timeStamps
  249.             os.execute(strCmd)
  250.     end
  251. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement