
rem ffmpeg.exe -i test.AVS -vframes 200 

REM set input=%1
REM set input_meta=%2
REM set videos=%3
 if "%mycrf%"=="3" (
 call encode_mp4s2hq.bat
 goto HQEND
 ) 
 set mypath=%MOVIE_FOLDER%
 
 
 SET FIXED_ENC_X=0
 SET FIXED_ENC_Y=0
 set vbvmax=80 
 set vbvbuf=400
 
 
 set ref_frame=2
 rem generate AVS file
 set current_path=%CD%

 echo LoadModule("avslib", "filters", "resize") > "en_%input%_crf_%mycrf%.avs"
 echo LoadPlugin("MKTD.dll") >> "en_%input%_crf_%mycrf%.avs"
 echo clip = import("%videos%") >> "en_%input%_crf_%mycrf%.avs"
 echo bc=BlankClip(1,width=320, height=240, fps=15, color=$FFFFFF) >> "en_%input%_crf_%mycrf%.avs"
 echo c1 = clip.ResizeToTarget(bc) >> "en_%input%_crf_%mycrf%.avs"
 echo c1 = MKTD(c1,MAP="%current_path%\mm%input%_%mycrf%.txt",MAX_MKTD_MB_NUM=120,TEMPLATE=%mytemplate%,FIXED_ENC_X=%FIXED_ENC_X%,FIXED_ENC_Y=%FIXED_ENC_Y%,ROI="%myRoi%") >> "en_%input%_crf_%mycrf%.avs"
 echo #clip = clip.ConvertToYUY2()  #use for player  >> "en_%input%_crf_%mycrf%.avs"
 echo return c1 >> "en_%input%_crf_%mycrf%.avs"
 rem end <generate AVS file> end

 if %STM%==1 goto vbv
 if %SHORT_TEST%==1 (x264.exe --profile baseline --frames 150 --level 3.1 --preset fast --fps 15 --tune %mytune% --crf %mycrf% --nr 100 --thread-input --deblock 0:0 --no-cabac --b-bias 0 --scenecut 40 --ref 2 --chroma-qp-offset 5 --rc-lookahead 30 --aq-mode 1 --aq-strength 1.5 --merange 40 --subme 10 --qpmin %MIN_Q% --qpmax %MAX_Q% --partitions p8x8,i4x4,p4x4 --no-8x8dct --trellis 0 --psy-rd 1.0:0 --psnr --ssim --output "%input%_crf_%mycrf%.264" "en_%input%_crf_%mycrf%.avs") else (
 x264.exe --profile baseline --level 3.1 --preset fast --fps 15 --tune %mytune% --crf %mycrf% --nr 100 --thread-input --deblock 0:0 --no-cabac --b-bias 0 --scenecut 40 --ref 2 --chroma-qp-offset 5 --rc-lookahead 30 --aq-mode 1 --aq-strength 1.5 --merange 40 --subme 10 --qpmin %MIN_Q% --qpmax %MAX_Q% --partitions p8x8,i4x4,p4x4 --no-8x8dct --trellis 0 --psy-rd 1.0:0 --psnr --ssim --output "%input%_crf_%mycrf%.264" "en_%input%_crf_%mycrf%.avs")
 goto nvbv

 :vbv
 x264.exe --profile baseline --level 3.1 --preset fast --fps 15 --tune %mytune% --crf %mycrf% --vbv-bufsize %vbvbuf% --vbv-maxrate %vbvmax% --nr 100 --thread-input --deblock 0:0 --no-cabac --b-bias 0 --ref 2 --chroma-qp-offset 5 --rc-lookahead 30 --aq-mode 1 --aq-strength 1.5 --merange 40 --subme 10 --qpmin %MIN_Q% --qpmax %MAX_Q% --partitions p8x8,i4x4,p4x4 --no-8x8dct --trellis 0 --psy-rd 1.0:0 --psnr --ssim --output "%input%_crf_%mycrf%.264" "en_%input%_crf_%mycrf%.avs"
 :nvbv 
 echo clip = import("%videos%") > concatenate.avs
 echo return clip >> concatenate.avs
 ren "%input%_crf_%mycrf%.264" "%input%_crf_%mycrf%.h264"
 if %SHORT_TEST%==1 (ffmpeg.exe -ss 00:00:00 -t 00:00:10 -i "concatenate.avs" -ar 8000 -ac 1 -ab 12.2k "%input%_15_85.amr") else (
 ffmpeg.exe -i "concatenate.avs" -ar 8000 -ac 1 -ab 12.2k "%input%_15_85.amr")
 
 MbMapCompress.exe -e "mm%input%_%mycrf%.txt" map.bin stco.bin stsz.bin 320 240 0 %nver% map2.sei
 copy /b stco.bin+stsz.bin+map.bin "%input%_%mycrf%.bin"
 del map.bin
 del stco.bin
 del stsz.bin 
 if not exist "%input%_15_85.amr" cancel
 ren "%input%_15_85.amr" tmp_in_amr.amr
 
 if not exist "%input%_crf_%mycrf%.h264" cancel
 ren "%input%_crf_%mycrf%.h264" tmp2_in_h264.h264

 if not "%PRV%"=="0" (
 Yamb\MP4Box.exe -add tmp2_in_h264.h264:fps=15 -add tmp_in_amr.amr -new 1min_out.mp4
 Yamb\MP4Box.exe -splitx 0:%PRV% 1min_out.mp4 -out 1min.mp4
 if exist 1min.mp4 (
 Yamb\MP4Box.exe -raw 1 1min.mp4
 Yamb\MP4Box.exe -raw 2 1min.mp4
 seicut.exe map2.sei %PRV% 15 1min.sei
 )
 )
 
 if %STM%==0 (
 EncDecrpt.exe -e tmp2_in_h264.h264 tmp_in_h264.h264
 goto NEXT
 )
 EncDecrpt.exe -e tmp2_in_h264.h264 tmp_in_h264.h264 map2.sei
 :NEXT
 call_mp4box.exe Yamb\MP4Box.exe tmp_in_h264.h264 tmp_in_amr.amr tmp_out.mp4 "%input_meta%"
 
 if not exist tmp_out.mp4 cancel
 if "%mycrf%"=="19" (
 AtomicParsley.exe tmp_out.mp4 --meta-uuid "kmhq" file "%input%_%mycrf%.bin"
 move /Y tmp_out-temp*.* "%input%_crf_%mycrf%_amr.mp4"
 del tmp_out.mp4
 copy /b "%input%_crf_%mycrf%_amr.mp4"+fake.fst "%input%_crf_%mycrf%_amr_e_kmv.kmv"
 goto out
 )
if %STM%==0 (
 ren tmp_out.mp4 "%input%_crf_%mycrf%_amr.mp4"
 copy /b "%input%_crf_%mycrf%_amr.mp4"+"%input%_%mycrf%.bin" "%input%_crf_%mycrf%_amr_e_kmv.kmv"
 goto out
)
 AtomicParsley.exe tmp_out.mp4 --meta-uuid "kmve" file Edge.key
 move /Y tmp_out-temp*.* "%input%_crf_%mycrf%_amr.mp4"
 del tmp_out.mp4
 copy /b "%input%_crf_%mycrf%_amr.mp4"+fake.fst "%input%_crf_%mycrf%_amr_e_kmv.kmv"
 
 if exist 1min_track1.h264 (
 if exist tmp.mp4 del tmp.mp4
 EncDecrpt.exe -e 1min_track1.h264 1min_tmp.h264 1min.sei
 call_mp4box.exe Yamb\MP4Box.exe 1min_tmp.h264 1min_track2.amr tmp_1min_out.mp4 "%input_meta%" 
 if exist tmp.mp4 del tmp.mp4
 )
 if exist tmp_1min_out.mp4 (
 AtomicParsley.exe tmp_1min_out.mp4 --meta-uuid "kmve" file Edge.key 
 move /Y tmp_1min_out-temp*.* "%input%_crf_%mycrf%_amr_1min.mp4" 
 copy /b "%input%_crf_%mycrf%_amr_1min.mp4"+fake.fst "%input%_crf_%mycrf%_1min.kmv"
 )
:out 
if not exist "%input%_crf_%mycrf%_amr_e_kmv.kmv" cancel
 ren "%input%_crf_%mycrf%_amr_e_kmv.kmv" "%outfnm%.kmv"

if exist "%input%_crf_%mycrf%_1min.kmv" ren "%input%_crf_%mycrf%_1min.kmv" "%outfnm%_preview.kmv"
 
if %DEBUG%==1 (
zip.exe "err_%outfnm%.zip" "en_%input%_crf_%mycrf%.avs" *.bin *.amr *.h264 "%input%_crf_%mycrf%_amr.mp4" tmp.mp4 "mm%input%_%mycrf%.txt" concatenate.avs "%videos%"
move /Y "err_%outfnm%.zip" _Log_
)
del "en_%input%_crf_%mycrf%.avs"
del *.bin 
del *.amr
del *.h264 
del "%input%_crf_%mycrf%_amr.mp4"
del tmp.mp4
del concatenate.avs


 if %DRM%==1 (
 call DRM_remux.bat "%outfnm%.kmv" out.kmv "%input_meta%"
 move /Y out.kmv "%outfnm%.kmv"
 )
 move /Y "%outfnm%.kmv" "%out_folder%"
 if exist "%outfnm%_preview.kmv" (
 move /Y "%outfnm%_preview.kmv" "%out_folder%"
 del 1min.sei
 del tmp_1min_out.mp4
 del 1min.mp4
 del "%input%_crf_%mycrf%_amr_1min.mp4"
 )
 if exist 1min_out.mp4 del 1min_out.mp4
del "mm%input%_%mycrf%.txt"
del map2.sei
:HQEND
