@IF EXIST "%~dp0Output\%~n1.mp4" GOTO exist
@IF EXIST "%~dpn1.mkv" GOTO matroska
@IF EXIST "%~dpn1.avi" GOTO avi
GOTO other

:matroska
echo LoadPlugin("%~dp0Tools\ffms2.dll") > "%~dp0Temp\%~n1_Avisynth.avs"
echo Import("%~dp0Tools\FFMS2.avsi") >> "%~dp0Temp\%~n1_Avisynth.avs"
echo FFVideoSource("%~f1") >> "%~dp0Temp\%~n1_Avisynth.avs"
START /LOW /B /WAIT "" "%~dp0Tools\avs4x26x.exe" --x264-binary "%~dp0Tools\x264_64.exe" --preset slower --psy-rd 0.7:0.0 --bframes 8 --crf 18 --audiofile "%~f1" --acodec copy --output "%~dp0Output\%~n1.mp4" "%~dp0Temp\%~n1_Avisynth.avs"
del "%~dp0Temp\%~n1_Avisynth.avs"
del "%~f1.ffindex"
goto end

:other
echo LoadPlugin("%~dp0Tools\ffms2.dll") > "%~dp0Temp\%~n1_Avisynth.avs"
echo Import("%~dp0Tools\FFMS2.avsi") >> "%~dp0Temp\%~n1_Avisynth.avs"
echo FFVideoSource("%~f1") >> "%~dp0Temp\%~n1_Avisynth.avs"
echo Spline36Resize(960,544) >> "%~dp0Temp\%~n1_Avisynth.avs"
START /LOW /B /WAIT "" "%~dp0Tools\x264.exe" --preset slow --psy-rd 0.7:0.0 --crf 19 --audiofile "%~f1" --acodec copy --output "%~dp0Output\%~n1.mp4" "%~dp0Temp\%~n1_Avisynth.avs"
del "%~dp0Temp\%~n1_Avisynth.avs"
del "%~f1.ffindex"
goto end

:avi
echo AVISource("%~f1") > "%~dp0Temp\%~n1_Avisynth.avs"
echo Spline36Resize(960,544) >> "%~dp0Temp\%~n1_Avisynth.avs"
START /LOW /B /WAIT "" "%~dp0Tools\x264.exe" --preset slow --psy-rd 0.7:0.0 --crf 19 --audiofile "%~f1" --acodec copy --output "%~dp0Output\%~n1.mp4" "%~dp0Temp\%~n1_Avisynth.avs"
del "%~dp0Temp\%~n1_Avisynth.avs"
goto end

:exist
@echo off
cls
color 0c
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo             File already encoded - See %~dp0Output
echo.
pause > NUL
:end