Advertisement
maujogador

Batch Sub Muxer

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