1. @echo off
  2.  
  3. cd %~dp0
  4. if "%~f1" == "" goto fail
  5.  
  6. echo Preparing to recode "%~f1"...
  7. setlocal enabledelayedexpansion
  8. set tmpname=%Time%
  9. set tmpname=%tmpname::=%
  10. set tmpname=%tmpname:.=%
  11. set tmpname=%tmpname:,=%
  12. set tmpname=%tmpname: =%
  13. for /l %%a in (0,1,1) do set tmpname=!tmpname!!random!
  14. endlocal & set tmpfile=%tmpname%
  15.  
  16. if not exist tmp mkdir tmp
  17. del /q tmp\*.*
  18.  
  19. echo Building Avisynth script...
  20. echo LoadPlugin("%~dp0bin\plugins\ffms2.dll") > tmp\%tmpfile%.avs
  21. echo LoadPlugin("%~dp0bin\plugins\vsfilter.dll") >> tmp\%tmpfile%.avs
  22. echo LoadPlugin("%~dp0bin\plugins\LoadFont.dll") >> tmp\%tmpfile%.avs
  23. echo Import("%~dp0bin\plugins\ResizeKAR.avsi") >> tmp\%tmpfile%.avs
  24. echo FFIndex("%~f1",cachefile="%~dp0tmp\%tmpfile%.index") >> tmp\%tmpfile%.avs
  25. echo v=FFVideoSource("%~f1",cachefile="%~dp0tmp\%tmpfile%.index").ChangeFPS(23.976) >> tmp\%tmpfile%.avs
  26. echo a=FFAudioSource("%~f1",cachefile="%~dp0tmp\%tmpfile%.index") >> tmp\%tmpfile%.avs
  27. echo AudioDub(v,a) >> tmp\%tmpfile%.avs
  28. if not exist "%~dpn1.ass" goto srt
  29. echo Detected external ASS subtitles...
  30. echo TextSub("%~dpn1.ass") >> tmp\%tmpfile%.avs
  31. goto endsub
  32. :srt
  33. if not exist "%~dpn1.srt" goto internal
  34. echo Detected external SRT subtitles...
  35. echo TextSub("%~dpn1.srt") >> tmp\%tmpfile%.avs
  36. goto endsub
  37. :internal
  38. if /i "%~x1" NEQ ".mkv" goto endsub
  39. echo Detected MKV container. Trying to parse...
  40. set subnum == ""
  41. bin\mkvmerge -i "%~f1" >> tmp\%tmpfile%.info
  42. findstr subtitles tmp\%tmpfile%.info >> tmp\%tmpfile%.subsinfo
  43. for /f "tokens=3 delims=: " %%a in (tmp\%tmpfile%.subsinfo) do set subnum=%%a
  44. if %subnum% == "" goto endsub
  45. echo Detected internal subtitles. Extracting...
  46. findstr truetype tmp\%tmpfile%.info >> tmp\%tmpfile%.fontsinfo
  47. for /f "tokens=1,5 delims=:'" %%i in (tmp\%tmpfile%.fontsinfo) do for /f "tokens=3" %%k in ("%%i") do (
  48. bin\mkvextract attachments "%~f1" %%k:"tmp\%%j" > nul
  49. echo LoadFont^("%~dp0tmp\%%j"^) >> tmp\%tmpfile%.avs
  50. )
  51. bin\mkvextract tracks "%~f1" %subnum%:"tmp\%tmpfile%.ass" > nul
  52. echo TextSub("%~dp0tmp\%tmpfile%.ass") >> tmp\%tmpfile%.avs
  53. :endsub
  54. echo ResizeKAR(640,360) >> tmp\%tmpfile%.avs
  55. rem echo Trim(0,300) >> tmp\%tmpfile%.avs
  56. echo Avisynth script is ready.
  57.  
  58. echo Encoding video. Pass 1...
  59. bin\xvid_encraw -i tmp\%tmpfile%.avs -o nul -pass1 tmp\%tmpfile%.stats -w 640 -h 360 -framerate 23.976 -bitrate 550 -max_bframes 0 -progress
  60. echo Encoding video. Pass 2...
  61. bin\xvid_encraw -i tmp\%tmpfile%.avs -avi tmp\%tmpfile%.avi -pass2 tmp\%tmpfile%.stats -w 640 -h 360 -framerate 23.976 -bitrate 550 -max_bframes 0 -progress
  62. echo Encoding audio...
  63. bin\ffmpeg -i tmp\%tmpfile%.avs -vn -acodec pcm_s16le -f wav -y tmp\%tmpfile%.wav
  64. bin\neroaacenc -br 96000 -lc -2pass -if tmp\%tmpfile%.wav -of tmp\%tmpfile%.m4a
  65. echo Muxing...
  66. if exist "%~dpn1.phone.mp4" del "%~dpn1.phone.mp4"
  67. bin\mp4box -add tmp\%tmpfile%.avi -add tmp\%tmpfile%.m4a "%~dpn1.phone.mp4"
  68. echo Removing temporary files...
  69. del /q tmp\%tmpfile%.*
  70. del /q tmp\*.?tf
  71. echo Recode is completed.
  72. goto done
  73.  
  74. :fail
  75. echo FAIL
  76. :done
  77. pause