View Full Version : Making batch scripts for first pass crf encoding?
stpdrgstr
17th May 2009, 05:39
Hi, I have a question.
I've been wondering if there is a way to do this:
I do a crf 18 first pass, and I want to use the final bitrate of that first pass for the second one.
I know how to do that, but, I've been wondering if there's a way to automate it and make batchs, because is very annoying to do it manually, if I can automate it, I can go to sleep or go out and let the encoder do his thing.
These would be the parameters:
First Pass
x264.exe --pass 1 --crf 18 --stats ".stats" --ref 16 --mixed-refs --no-fast-pskip --bframes 5 --b-adapt 2 --b-pyramid --deblock 1:1 --subme 9 --trellis 1 --partitions all --8x8dct --me umh --merange 32 --threads auto --thread-input --progress --no-dct-decimate --no-psnr --no-ssim --output "C:\BLACK LAGOON\MainMovie\BLACK_LAGOON_VOL_01\avs\op.264" "C:\BLACK LAGOON\MainMovie\BLACK_LAGOON_VOL_01\avs\op.avs" [704x480] --fps 23.976
Second Pass
x264.exe --pass 2 --bitrate [here would go the final bitrate given by first pass] --stats ".stats" --ref 16 --mixed-refs --no-fast-pskip --bframes 5 --b-adapt 2 --b-pyramid --deblock 1:1 --subme 9 --trellis 1 --partitions all --8x8dct --me umh --merange 32 --threads auto --thread-input --progress --no-dct-decimate --no-psnr --no-ssim --output "C:\BLACK LAGOON\MainMovie\BLACK_LAGOON_VOL_01\avs\op.264" "C:\BLACK LAGOON\MainMovie\BLACK_LAGOON_VOL_01\avs\op.avs" [704x480] --fps 23.976
akupenguin
17th May 2009, 06:58
Sure it's possible, but why? If you're happy with CRF's choice of bitrate, then just encode CRF.
I do a crf 18 first pass, and I want to use the final bitrate of that first pass for the second one.
I know how to do that, but, I've been wondering if there's a way to automate it and make batchs, because is very annoying to do it manually, if I can automate it, I can go to sleep or go out and let the encoder do his thing.
Doing a second pass like this would only make sense if you were using VBV, but perhaps you need the extra heat from the CPU to keep you warm through the night. ;)
Parsing the first-pass bitrate by a Windows batch script was just discussed a few days ago: http://forum.doom9.org/showthread.php?p=1283998#post1283998
stpdrgstr
17th May 2009, 18:25
@akupenguin
Let's say just for kicks :p. Although, I've seen an improvement in high motion scenes, but maybe that's just me or something :(
Sure it doesn't improve anything at all?
@nm
I've already tried it, it didn't work. Maybe I'm doing something wrong. Should I just copy/paste it, and replace with my own options? Because last time I did, after the first pass, x264 just closes.
I've already tried it, it didn't work. Maybe I'm doing something wrong. Should I just copy/paste it, and replace with my own options?
Yes, make a batch file (encode.bat, for example), copy those lines there and replace the "[options]" part with your x264 options. Then run encode.bat
Did you also notice the following post that corrects the second line: http://forum.doom9.org/showthread.php?p=1284002#post1284002
Because last time I did, after the first pass, x264 just closes.
Yes, x264 is supposed to close after every pass, but the script continues to run the next one unless there is a problem in the code.
stpdrgstr
18th May 2009, 11:19
Oh, dumb me didn't look around enough, I'll check that out today, thanks nm.
buzzqw
18th May 2009, 11:27
Hi !
i have added in HDC (latest beta) this encoding mode ( first pass crf, second pass with the bitrate of first)
BHH
Selur
20th May 2009, 17:30
just for information: sx264 too supports 'constant rate factor 2pass encoding'
stpdrgstr
20th May 2009, 17:32
Thanks for the info guys, although I tend to use just the cli, I'm sure this will come handy for some people around here.
rack04
20th May 2009, 20:45
Here is my weak attempt. Set the program paths and input the raw AVC, VC-1, or MPEG Video stream.
@SET MediaInfo_PATH=C:\Program Files\MediaInfo_CLI\MediaInfo.exe
@SET eac3to_PATH=C:\Program Files\eac3to\eac3to.exe
@SET WORKING_DIRECTORY=C:\Personal\Videos
@SET DGAVCIndex_PATH=C:\Program Files\DGAVCIndex\DGAVCIndex.exe
@SET DGAVCDecode_PATH=C:\Program Files\DGAVCIndex\DGAVCDecode.dll
@SET DGIndex_PATH=C:\Program Files\DGIndex\DGIndex.exe
@SET DGDecode_PATH=C:\Program Files\DGIndex\DGDecode.dll
@SET x264_PATH=C:\Program Files\MeGUI\tools\x264\x264.exe
@Echo off
set /p INPUT_VIDEO="Full path to video input: "
goto general
:general
Echo General;%%FileName%%>"%WORKING_DIRECTORY%\temporary-generalfilename.txt"
"%MediaInfo_PATH%" --Inform=file://"%WORKING_DIRECTORY%\temporary-generalfilename.txt" "%INPUT_VIDEO%">"%WORKING_DIRECTORY%\temporary-analyzegeneralfilename.txt"
type "%WORKING_DIRECTORY%\temporary-analyzegeneralfilename.txt">"%WORKING_DIRECTORY%\temporary-analyzegeneralfilename.tmp"
set /p SOURCE_FILENAME=<"%WORKING_DIRECTORY%\temporary-analyzegeneralfilename.tmp"
goto video
:video
Echo Video;%%Format%%>"%WORKING_DIRECTORY%\temporary-videoformat.txt"
"%MediaInfo_PATH%" --Inform=file://"%WORKING_DIRECTORY%\temporary-videoformat.txt" "%INPUT_VIDEO%">"%WORKING_DIRECTORY%\temporary-analyzevideoformat.txt"
Echo Video;%%Width%%x%%Height%%>"%WORKING_DIRECTORY%\temporary-videoresolution.txt"
"%MediaInfo_PATH%" --Inform=file://"%WORKING_DIRECTORY%\temporary-videoresolution.txt" "%INPUT_VIDEO%">"%WORKING_DIRECTORY%\temporary-analyzevideoresolution.txt"
Echo Video;%%FrameRate%%>"%WORKING_DIRECTORY%\temporary-videoframerate.txt"
"%MediaInfo_PATH%" --Inform=file://"%WORKING_DIRECTORY%\temporary-videoframerate.txt" "%INPUT_VIDEO%">"%WORKING_DIRECTORY%\temporary-analyzevideoframerate.txt"
type "%WORKING_DIRECTORY%\temporary-analyzevideoformat.txt">"%WORKING_DIRECTORY%\temporary-analyzevideoformat.tmp"
type "%WORKING_DIRECTORY%\temporary-analyzevideoresolution.txt">"%WORKING_DIRECTORY%\temporary-analyzevideoresolution.tmp"
type "%WORKING_DIRECTORY%\temporary-analyzevideoframerate.txt">"%WORKING_DIRECTORY%\temporary-analyzevideoframerate.tmp"
set /p SOURCE_VIDFORMAT=<"%WORKING_DIRECTORY%\temporary-analyzevideoformat.tmp"
set /p SOURCE_VIDRES=<"%WORKING_DIRECTORY%\temporary-analyzevideoresolution.tmp"
set /p SOURCE_VIDFRAMERATE=<"%WORKING_DIRECTORY%\temporary-analyzevideoframerate.tmp"
Echo ************ Video ************>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-video.txt"
Echo Format = %SOURCE_VIDFORMAT%>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-video.txt"
Echo Resolution = %SOURCE_VIDRES%>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-video.txt"
Echo Framerate = %SOURCE_VIDFRAMERATE%>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-video.txt"
goto analyzeclean
:analyzeclean
DEL "%WORKING_DIRECTORY%\temporary-generalfilename.txt"
DEL "%WORKING_DIRECTORY%\temporary-analyzegeneralfilename.txt"
DEL "%WORKING_DIRECTORY%\temporary-analyzegeneralfilename.tmp"
DEL "%WORKING_DIRECTORY%\temporary-videoformat.txt"
DEL "%WORKING_DIRECTORY%\temporary-analyzevideoformat.txt"
DEL "%WORKING_DIRECTORY%\temporary-analyzevideoformat.tmp"
DEL "%WORKING_DIRECTORY%\temporary-videoresolution.txt"
DEL "%WORKING_DIRECTORY%\temporary-analyzevideoresolution.txt"
DEL "%WORKING_DIRECTORY%\temporary-analyzevideoresolution.tmp"
DEL "%WORKING_DIRECTORY%\temporary-videoframerate.txt"
DEL "%WORKING_DIRECTORY%\temporary-analyzevideoframerate.txt"
DEL "%WORKING_DIRECTORY%\temporary-analyzevideoframerate.tmp"
goto resizecheck
:resizecheck
if "%SOURCE_VIDRES%" EQU "1920x1080" goto resize
goto qpfilecheck
:resize
set /p RESIZE="Resize to 1280x720 (yes/no): "
goto qpfilecheck
:qpfilecheck
if "%SOURCE_VIDFORMAT%" EQU "VC-1" goto avisynth
goto qpfile
:qpfile
set /p QPFILE="Encode with I-frame chapters (yes/no): "
if "%QPFILE%" EQU "yes" goto qpfileyes
goto avisynth
:qpfileyes
set /p QPFILEGEN_FILE_PATH="Full path to x264 QPfile: "
goto avisynth
:avisynth
if "%SOURCE_VIDFORMAT%" EQU "AVC" goto h264
if "%SOURCE_VIDFORMAT%" EQU "VC-1" goto vc1
if "%SOURCE_VIDFORMAT%" EQU "MPEG VIDEO" goto m2v
:h264
Echo.
@Echo on
"%DGAVCIndex_PATH%" -i "%INPUT_VIDEO%" -o "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.dga" -e -h
@Echo off
if "%RESIZE%" EQU "yes" goto resize
goto noresize
:noresize
Echo LoadPlugin("%DGAVCDecode_PATH%")>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
Echo AVCSource("%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.dga")>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
goto avisynthclean
:resize
Echo LoadPlugin("%DGAVCDecode_PATH%")>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
Echo AVCSource("%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.dga")>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
Echo Spline64Resize(1280,720)>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
goto avisynthclean
:vc1
Echo.
@Echo on
"%eac3to_PATH%" "%INPUT_VIDEO%" "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.mkv"
@Echo off
if "%RESIZE%" EQU "yes" goto resize
goto noresize
:noresize
if "%SOURCE_VIDFRAMERATE%" EQU "23.976" goto 23
if "%SOURCE_VIDFRAMERATE%" EQU "24.000" goto 24
if "%SOURCE_VIDFRAMERATE%" EQU "25.000" goto 25
if "%SOURCE_VIDFRAMERATE%" EQU "29.970" goto 29
:23
Echo DirectShowSource("%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.mkv", audio=false)>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
Echo AssumeFPS(24000,1001)>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
goto avisynthclean
:24
Echo DirectShowSource("%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.mkv", audio=false)>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
Echo AssumeFPS(48,2)>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
goto avisynthclean
:25
Echo DirectShowSource("%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.mkv", audio=false)>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
Echo AssumeFPS(50,2)>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
goto avisynthclean
:29
Echo DirectShowSource("%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.mkv", audio=false)>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
Echo AssumeFPS(30000,1001)>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
goto avisynthclean
:resize
if "%SOURCE_VIDFRAMERATE%" EQU "23.976" goto 23
if "%SOURCE_VIDFRAMERATE%" EQU "24.000" goto 24
if "%SOURCE_VIDFRAMERATE%" EQU "25.000" goto 25
if "%SOURCE_VIDFRAMERATE%" EQU "29.970" goto 29
:23
Echo DirectShowSource("%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.mkv", audio=false)>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
Echo AssumeFPS(24000,1001)>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
Echo Spline64Resize(1280,720)>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
goto avisynthclean
:24
Echo DirectShowSource("%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.mkv", audio=false)>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
Echo AssumeFPS(48,2)>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
Echo Spline64Resize(1280,720)>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
goto avisynthclean
:25
Echo DirectShowSource("%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.mkv", audio=false)>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
Echo AssumeFPS(50,2)>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
Echo Spline64Resize(1280,720)>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
goto avisynthclean
:29
Echo DirectShowSource("%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.mkv", audio=false)>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
Echo AssumeFPS(30000,1001)>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
Echo Spline64Resize(1280,720)>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
goto avisynthclean
:m2v
Echo.
@Echo on
"%DGIndex_PATH%" -AIF=[%INPUT_VIDEO%] -OF=[%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output] -exit -hide -OM=0
@Echo off
if "%RESIZE%" EQU "yes" goto resize
goto noresize
:noresize
Echo LoadPlugin("%DGDecode_PATH%")>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
Echo MPEG2Source("%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.d2v")>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
goto avisynthclean
:resize
Echo LoadPlugin("%DGDecode_PATH%")>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
Echo MPEG2Source("%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.d2v")>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
Echo Spline64Resize(1280,720)>>"%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
goto avisynthclean
:avisynthclean
if "%SOURCE_VIDFORMAT%" EQU "VC-1" goto vc1clean
goto pass1
:vc1clean
DEL "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output - Log.txt"
goto pass1
:pass1
if "%QPFILE%" EQU "yes" goto iframechap
goto normal
:normal
Echo.
Echo.
Echo.[ %SOURCE_FILENAME% ] [ %TIME% ] Encoding pass 1 ...
Echo.
@Echo on
"%x264_PATH%" --pass 1 --crf 18 --stats "%WORKING_DIRECTORY%\%SOURCE_FILENAME%.stats" --level 4.1 --keyint 24 --min-keyint 1 --bframes 3 --b-adapt 2 --b-pyramid --weightb --direct auto --deblock -1:-1 --subme 2 --partitions none --qcomp 0.5 --me dia --threads auto --thread-input --sar 1:1 --aud --progress --no-psnr --no-ssim --output "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-crfoutput.h264" "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs" --mvrange 511 2> "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-1pass.txt"
@Echo off
goto obtainbitrate
:iframechap
Echo.
Echo.
Echo.[ %SOURCE_FILENAME% ] [ %TIME% ] Encoding pass 1 with I-frame chapters ...
Echo.
@Echo on
"%x264_PATH%" --pass 1 --crf 18 --stats "%WORKING_DIRECTORY%\%SOURCE_FILENAME%.stats" --level 4.1 --keyint 24 --min-keyint 1 --bframes 3 --b-adapt 2 --b-pyramid --weightb --direct auto --deblock -1:-1 --subme 2 --partitions none --qcomp 0.5 --me dia --threads auto --thread-input --sar 1:1 --aud --progress --no-psnr --no-ssim --output "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-crfoutput.h264" "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs" --mvrange 511 --qpfile "%QPFILEGEN_FILE_PATH%" 2> "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-1pass.txt"
@Echo off
goto obtainbitrate
:obtainbitrate
FOR /F "tokens=7 delims=. " %%A IN ('findstr encoded "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-1pass.txt"') DO SET CRF_VIDBITRATE=%%A
goto pass2
:pass2
if "%QPFILE%" EQU "yes" goto iframechap
goto normal
:normal
Echo.
Echo.
Echo.[ %SOURCE_FILENAME% ] [ %TIME% ] Encoding pass 2 ...
Echo.
@Echo on
"%x264_PATH%" --pass 2 --bitrate %CRF_VIDBITRATE% --stats "%WORKING_DIRECTORY%\%SOURCE_FILENAME%.stats" --level 4.1 --keyint 24 --min-keyint 1 --ref 3 --mixed-refs --bframes 3 --b-adapt 2 --b-pyramid --weightb --direct auto --deblock -1:-1 --subme 7 --trellis 2 --partitions all --8x8dct --vbv-bufsize 30000 --vbv-maxrate 24000 --qcomp 0.5 --me umh --threads auto --thread-input --sar 1:1 --aud --progress --no-psnr --no-ssim --output "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.h264" "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs" --mvrange 511 --nal-hrd 2> "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-2pass.txt"
@Echo off
goto cleanencode
:iframechap
Echo.
Echo.
Echo.[ %SOURCE_FILENAME% ] [ %TIME% ] Encoding pass 2 with I-frame chapters ...
Echo.
@Echo on
"%x264_PATH%" --pass 2 --bitrate %CRF_VIDBITRATE% --stats "%WORKING_DIRECTORY%\%SOURCE_FILENAME%.stats" --level 4.1 --keyint 24 --min-keyint 1 --ref 3 --mixed-refs --bframes 3 --b-adapt 2 --b-pyramid --weightb --direct auto --deblock -1:-1 --subme 7 --trellis 2 --partitions all --8x8dct --vbv-bufsize 30000 --vbv-maxrate 24000 --qcomp 0.5 --me umh --threads auto --thread-input --sar 1:1 --aud --progress --no-psnr --no-ssim --output "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.h264" "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs" --mvrange 511 --nal-hrd --qpfile "%QPFILEGEN_FILE_PATH%" 2> "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-2pass.txt"
@Echo off
goto cleanencode
:cleanencode
if "%SOURCE_VIDFORMAT%" EQU "AVC" goto h264
if "%SOURCE_VIDFORMAT%" EQU "VC-1" goto vc1
if "%SOURCE_VIDFORMAT%" EQU "MPEG VIDEO" goto m2v
:h264
DEL "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.dga"
goto cleanencodefinal
:vc1
DEL "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.mkv"
goto cleanencodefinal
:m2v
DEL "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.d2v"
goto cleanencodefinal
:cleanencodefinal
DEL "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-crfoutput.h264"
DEL "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-output.avs"
DEL "%WORKING_DIRECTORY%\%SOURCE_FILENAME%-1pass.txt"
DEL "%WORKING_DIRECTORY%\%SOURCE_FILENAME%.stats"
goto end
:end
Echo.
Echo.
Echo.
Echo.[ %OUTPUT_FILENAME% ] [ %TIME% ] Finished !
Echo.
Echo.
Echo.
Pause
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.