Advertisement
Guest User

MKVmerge Subtitle Muxing

a guest
May 10th, 2015
2,083
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 3.49 KB | None | 0 0
  1. @ECHO OFF
  2. REM // =======================
  3. REM || Batch Subtitle Muxing
  4. REM ||       for MKVmerge
  5. REM || v0.4-20150511 by drudge
  6. REM \\ =======================
  7. REM @@ This batch script will scan the provided directory (or current folder)
  8. REM @@ for video files, attempt to match them with subtitle files,
  9. REM @@ and mux them using the MKVmerge command line.
  10. REM // ---- ---- ---- ----
  11. REM || USER CONFIGURATION
  12. REM // full path to mkvmerge.exe
  13.     SET "muxpath=C:\Program Files\MKVToolNix\mkvmerge.exe"
  14. REM \\ -- -- -- -- --
  15. REM // full path to unrar program, make sure to include switches
  16.     SET "rarpath=C:\Program Files\7-Zip\7z.exe"
  17.     SET "rarcmd=e"
  18. REM \\ -- -- -- -- --
  19. REM // output path (with trailing slash)
  20. REM ++ leave blank for working directory
  21.     SET "outputdir="
  22. REM @@ default: SET "outputdir="
  23. REM \\ -- -- -- -- --
  24. REM // prepend text to output filename
  25. REM ++ REQUIRED if outputdir is left blank
  26.     SET "fileprefix=subtitled-"
  27. REM @@ blank: SET "fileprefix="
  28. REM \\ -- -- -- -- --
  29. REM || END USER CONFIG
  30. REM \\ ---- ---- ---- ----
  31. REM -- editing below this line should be done precisely. (here thar be dragons)
  32. REM ===========================================================================
  33.  
  34. REM @@ simple counters
  35. SET /A "mc=0"
  36. SET /A "me=0"
  37.  
  38. REM @@ default working path
  39. SET "wp=."
  40.  
  41. REM @@ optional working path via argument
  42. IF EXIST "%~1" SET "wp=%~1"
  43.  
  44. REM @@ ready steady go!
  45. CLS
  46. ECHO ===========================================================================
  47.  
  48. REM @@ attempt to use custom setting for output path
  49. IF EXIST "%outputdir%" (
  50.     REM @@ user has provided output path
  51.     ECHO == User Setting -- Output to: [%outputdir%]
  52. ) ELSE (
  53.     REM @@ no custom setting, check for prefix
  54.     IF [%fileprefix%]==[] (
  55.         ECHO @@ ERROR: empty [fileprefix] setting requires [outputdir] to be set
  56.         SET /A me+=1
  57.         GOTO:done
  58.     )
  59.     REM @@ use working path for output
  60.     SET "outputdir=%wp%\"
  61. )
  62. FOR %%H IN ("%wp%") DO (
  63.     REM @@ because "." doesn't tell us where we are
  64.     ECHO == Scanning [%%~dpfH\] for video files...
  65. )
  66.  
  67. :getfiles
  68. FOR %%I IN ("%wp%\*.avi",
  69. REM         "%wp%\*.customVideoExtension",
  70.             "%wp%\*.mkv",
  71.             "%wp%\*.mp4") DO (
  72.     REM @@ found a video file, now check for subtitles
  73.     CALL:getsubs "%%~I"
  74. )
  75. GOTO:done
  76.  
  77. :getsubs
  78. FOR %%J IN ("%wp%\%~n1.idx",
  79. REM         "%wp%\%~n1.customSubtitleExtension",
  80.             "%wp%\%~n1.srt") DO (
  81. REM @@ check for paired subtitle files. USF/XML may require this check as well
  82.     IF %%~xJ==.idx IF EXIST "%wp%\%%~nJ.idx" IF NOT EXIST "%wp%\%%~nJ.sub" (
  83.         IF EXIST "%wp%\%%~nJ.rar" (
  84.             ECHO -- [%%~nJ.sub] -- Found potential .rar
  85.             ECHO | SET /p extdone=">> "
  86.             "%rarpath%" "%rarcmd%" "%%~dpJ%%~nJ.rar" | FIND "Extracting"
  87.         ) ELSE (
  88.             ECHO @@ ERROR: [%%~nJ.idx] -- Missing .sub file
  89.             SET /A me+=1
  90.             GOTO:eof
  91.         )
  92.     )
  93. REM @@ subtitle found, time to put it all together
  94.     IF EXIST "%wp%\%%~nJ%%~xJ" CALL:muxit "%%~f1" "%%~xJ"
  95. )
  96. GOTO:eof
  97.  
  98. :muxit
  99. REM @@ make sure the destination file doesn't exist first
  100. IF EXIST "%outputdir%%fileprefix%%~n1%~x1" (
  101.     ECHO @@ ERROR: [%~n1%~x1] -- Existing output file
  102.     SET /A me+=1
  103.     GOTO:eof
  104. )
  105.  
  106. REM @@ we've made it!
  107. SET /A mc+=1
  108. REM @@ now we let mkvmerge work its magic
  109. ECHO | SET /p muxdone="++ Muxing: (%mc%) [%~n1%~x1]"
  110. "%muxpath%" -q -o "%outputdir%%fileprefix%%~n1%~x1" "%~1" "%wp%\%~n1%~2"
  111. ECHO  ..complete
  112. REM @@ success!
  113. GOTO:eof
  114.  
  115. :done
  116. ECHO == Finished Processing: %mc% completed / %me% errors
  117. ECHO ===========================================================================
  118. REM @@ game over, man
  119. pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement