Problem resizing only larger than 720p in script/

Discussion of the HandBrake command line interface (CLI)
Forum rules
An Activity Log is required for support requests. Please read How-to get an activity log? for details on how and why this should be provided.
Post Reply
Raptordin
Posts: 13
Joined: Sun Mar 19, 2023 10:51 pm

Problem resizing only larger than 720p in script/

Post by Raptordin »

I have made a script that helps me to convert all the MKV files in folder and subfolders to MP3 with my specific settings but I want it to resize files larger than 720p to 720p while retaining the size of files with resolutions less than 720p.

Code: Select all

@echo off
setlocal

REM Set the directory containing your MKV video files and HandBrakeCLI.exe
set "VideoDir=%~dp0"

REM Set the directory where you want to save the compressed videos
set "OutputDir=%~dp0"

REM Set the path to your HandBrakeCLI.exe
set "HandBrakeCLI=%~dp0\HandBrakeCLI.exe"

REM Set the path to your ffprobe.exe
set "ffprobe=%~dp0\ffprobe.exe"

REM Set the path to your log file
set "LogFile=%~dp0\log.txt"

for /R "%VideoDir%" %%F in (*.mkv) do (
    for /f "tokens=2 delims=x" %%a in ('"%ffprobe%" -v error -select_streams v:0 -show_entries stream=height -of default=noprint_wrappers=1:nokey=1 "%%F"') do (
        set "height=%%a"
    )
    if !height! geq 720 (
        "%HandBrakeCLI%" --input "%%F" --output "%%~dpnF.mp4" --all-audio --all-subtitles --mixdown 5point1 -R 44.1 -e nvenc_h265 -q 27 -x cabac=1:ref=5:analyse=0x133:me=umh:subme=9:chroma-me=1:deadzone-inter=21:deadzone-intra=11:b-adapt=2:rc-lookahead=60:vbv-maxrate=10000:vbv-bufsize=10000:qpmax=69:bframes=5:b-adapt=2:direct=auto:crf-max=51:weightp=2:merange=24:chroma-qp-offset=-1:sync-lookahead=2:psy-rd=1.00,0.15:trellis=2:min-keyint=23:partitions=all --maxWidth 1280 --maxHeight 720 --keep-display-aspect --modulus 2
    ) else (
        "%HandBrakeCLI%" --input "%%F" --output "%%~dpnF.mp4" --all-audio --all-subtitles --mixdown 5point1 -R 44.1 -e nvenc_h265 -q 27 -x cabac=1:ref=5:analyse=0x133:me=umh:subme=9:chroma-me=1:deadzone-inter=21:deadzone-intra=11:b-adapt=2:rc-lookahead=60:vbv-maxrate=10000:vbv-bufsize=10000:qpmax=69:bframes=5:b-adapt=2:direct=auto:crf-max=51:weightp=2:merange=24:chroma-qp-offset=-1:sync-lookahead=2:psy-rd=1.00,0.15:trellis=2:min-keyint=23:partitions=all
    )
    if errorlevel 1 (
        echo %%F >> "%LogFile%"
        echo ERROR >> "%LogFile%"
    ) else (
        echo %%F >> "%LogFile%"
        echo SUCCESS >> "%LogFile%"
REM		Unremark the following line to delete Source files		
REM		del "%%F"
    )
)

endlocal



Windows 11, AMD Ryzen 7 3700X with 32 GBs Ram




HandBrake Activity Log ***required*** (see How-to get an activity log)

Code: Select all


Encoding: task 1 of 1, 0.00 %
Encoding: task 1 of 1, 0.00 %
Encoding: task 1 of 1, 0.00 %
Encoding: task 1 of 1, 1.99 %
Encoding: task 1 of 1, 5.45 %
Encoding: task 1 of 1, 9.82 %
Encoding: task 1 of 1, 13.68 %
Encoding: task 1 of 1, 17.85 %
Encoding: task 1 of 1, 22.07 %
Encoding: task 1 of 1, 25.96 %
Encoding: task 1 of 1, 30.10 %
Encoding: task 1 of 1, 34.23 %
Encoding: task 1 of 1, 38.49 %
Encoding: task 1 of 1, 42.58 %
Encoding: task 1 of 1, 46.88 %
Encoding: task 1 of 1, 51.01 %
Encoding: task 1 of 1, 54.91 %
Encoding: task 1 of 1, 58.69 %
Encoding: task 1 of 1, 63.10 %
Encoding: task 1 of 1, 67.12 %
Encoding: task 1 of 1, 71.13 %
Encoding: task 1 of 1, 75.19 %
Encoding: task 1 of 1, 79.24 %
Encoding: task 1 of 1, 83.06 % (505.47 fps, avg 499.88 fps, ETA 00h00m01s)
Encoding: task 1 of 1, 87.36 % (505.47 fps, avg 499.88 fps, ETA 00h00m01s)
Encoding: task 1 of 1, 91.33 % (505.47 fps, avg 499.88 fps, ETA 00h00m01s)
Encoding: task 1 of 1, 95.71 % (505.47 fps, avg 499.88 fps, ETA 00h00m01s)
Encoding: task 1 of 1, 99.60 % (505.47 fps, avg 499.88 fps, ETA 00h00m01s)
Encoding: task 1 of 1, 99.60 % (505.47 fps, avg 499.88 fps, ETA 00h00m01s)
Encoding: task 1 of 1, 99.60 % (505.47 fps, avg 499.88 fps, ETA 00h00m01s)

Encoding: task 1 of 1, 0.00 %
Encoding: task 1 of 1, 0.00 %
Encoding: task 1 of 1, 0.00 %
Encoding: task 1 of 1, 0.78 %
Encoding: task 1 of 1, 4.24 %
Encoding: task 1 of 1, 7.21 %
Encoding: task 1 of 1, 10.45 %
Encoding: task 1 of 1, 13.59 %
Encoding: task 1 of 1, 16.83 %
Encoding: task 1 of 1, 19.80 %
Encoding: task 1 of 1, 23.18 %
Encoding: task 1 of 1, 26.55 %
Encoding: task 1 of 1, 29.61 %
Encoding: task 1 of 1, 33.07 %
Encoding: task 1 of 1, 36.18 %
Encoding: task 1 of 1, 39.64 %
Encoding: task 1 of 1, 42.93 %
Encoding: task 1 of 1, 46.08 %
Encoding: task 1 of 1, 49.27 %
Encoding: task 1 of 1, 52.78 %
Encoding: task 1 of 1, 56.30 %
Encoding: task 1 of 1, 59.22 %
Encoding: task 1 of 1, 62.41 %
Encoding: task 1 of 1, 65.92 % (345.79 fps, avg 345.42 fps, ETA 00h00m02s)
Encoding: task 1 of 1, 68.98 % (345.79 fps, avg 345.42 fps, ETA 00h00m02s)
Encoding: task 1 of 1, 72.13 % (345.79 fps, avg 345.42 fps, ETA 00h00m02s)
Encoding: task 1 of 1, 75.41 % (345.79 fps, avg 345.42 fps, ETA 00h00m02s)
Encoding: task 1 of 1, 78.88 % (345.79 fps, avg 345.42 fps, ETA 00h00m02s)
Encoding: task 1 of 1, 81.93 % (353.21 fps, avg 348.37 fps, ETA 00h00m01s)
Encoding: task 1 of 1, 84.85 % (353.21 fps, avg 348.37 fps, ETA 00h00m01s)
Encoding: task 1 of 1, 88.46 % (353.21 fps, avg 348.37 fps, ETA 00h00m01s)
Encoding: task 1 of 1, 91.83 % (353.21 fps, avg 348.37 fps, ETA 00h00m01s)
Encoding: task 1 of 1, 95.12 % (353.21 fps, avg 348.37 fps, ETA 00h00m01s)
Encoding: task 1 of 1, 99.54 % (348.26 fps, avg 346.43 fps, ETA 00h00m00s)
Encoding: task 1 of 1, 99.54 % (348.26 fps, avg 346.43 fps, ETA 00h00m00s)
Encoding: task 1 of 1, 99.54 % (348.26 fps, avg 346.43 fps, ETA 00h00m00s)
Encoding: task 1 of 1, 99.54 % (348.26 fps, avg 346.43 fps, ETA 00h00m00s)

Raptordin
Posts: 13
Joined: Sun Mar 19, 2023 10:51 pm

Re: Problem resizing only larger than 720p in script/

Post by Raptordin »

OK I made several changes to the script and as per every information I saw online it should do the Job but still it does not want to resize the output video to 720p.

Here is the script in its current state.

Code: Select all

@echo off
setlocal enabledelayedexpansion

REM Set the directory containing your MKV video files and HandBrakeCLI.exe
set "VideoDir=%~dp0"

REM Set the directory where you want to save the compressed videos
set "OutputDir=%~dp0"

REM Set the path to your HandBrakeCLI.exe
set "HandBrakeCLI=%~dp0\HandBrakeCLI.exe"

REM Set the path to your ffprobe.exe
set "ffprobe=%~dp0\ffprobe.exe"

REM Set the path to your log file
set "LogFile=%~dp0\log.txt"

for /R "%VideoDir%" %%F in (*.mkv) do (
    for /f "tokens=2 delims=x" %%a in ('%ffprobe% -v error -select_streams v:0 -show_entries stream^=height -of default^=noprint_wrappers^=1:nokey^=1 "%%F"') do set "height=%%a"
    if !height! GTR 720 (
        set "scale=720"
    ) else (
        set "scale=-1"
    )
    "%HandBrakeCLI%" --input "%%F" --output "%%~dpnF.mp4" --all-audio --all-subtitles --mixdown 5point1 -R 44.1 -e nvenc_h265 -q 27 -x cabac=1:ref=5:analyse=0x133:me=umh:subme=9:chroma-me=1:deadzone-inter=21:deadzone-intra=11:b-adapt=2:rc-lookahead=60:vbv-maxrate=10000:vbv-bufsize=10000:qpmax=69:bframes=5:b-adapt=2:direct=auto:crf-max=51:weightp=2:merange=24:chroma-qp-offset=-1:sync-lookahead=2:psy-rd=1.00,0.15:trellis=2:min-keyint=23:partitions=all --height !scale! --auto-anamorphic
    if errorlevel 1 (
        echo %%F >> "%LogFile%"
        echo ERROR >> "%LogFile%"
    ) else (
        echo %%F >> "%LogFile%"
        echo SUCCESS >> "%LogFile%"
        for %%I in ("%%F") do set "inputSize=%%~zI"
        for %%J in ("%%~dpnF.mp4") do set "outputSize=%%~zJ"
        if !outputSize! GTR !inputSize! (
            del "%%~dpnF.mp4"
            echo %%F >> "%LogFile%"
            echo OUTPUT LARGER THAN INPUT >> "%LogFile%"
        ) else (
            del "%%F"
        )
    )
)

Also added that if the output is larger than the source then will delete the output and maintain the source as is.
Raptordin
Posts: 13
Joined: Sun Mar 19, 2023 10:51 pm

Re: Problem resizing only larger than 720p in script/

Post by Raptordin »

Throughout my testing the following does work but when I try to integrate it (Again) in my script it fails and does not resize.

Code: Select all

.\HandBrakeCLI.exe -i input.mkv -o output.mkv -e nvenc_h265 -q 20 -B 160 -r 30 --width 1280 --height 720 --crop 0:0:0:0
Raptordin
Posts: 13
Joined: Sun Mar 19, 2023 10:51 pm

Re: Problem resizing only larger than 720p in script/

Post by Raptordin »

OK now I am 100% sure it is not a HandbrakeCLO problem it is a scripting problem.

I echoed and checked the output sample

Code: Select all

HandBrakeCLI.exe --input "D:\Temp\00\Convert\Test\input.mkv" --output "D:\Temp\00\Convert\Test\input.mp4" --all-audio --all-subtitles --mixdown 5point1 -R 44.1 -e nvenc_h265 -q 27 --keep-display-aspect  --audio 1 --aencoder mp3 --ab 96 -x cabac=1:ref=5:analyse=0x133:me=umh:subme=9:chroma-me=1:deadzone-inter=21:deadzone-intra=11:b-adapt=2:rc-lookahead=60:vbv-maxrate=10000:vbv-bufsize=10000:qpmax=69:bframes=5:b-adapt=2:direct=auto:crf-max=51:weightp=2:merange=24:chroma-qp-offset=-1:sync-lookahead=2:psy-rd=1.00,0.15:trellis=2:min-keyint=23:partitions=all
and as shown the information about resize is unavailable and thus not passed to the command.

Will keep working on it and hope to solve it.,
Raptordin
Posts: 13
Joined: Sun Mar 19, 2023 10:51 pm

Re: Problem resizing only larger than 720p in script/

Post by Raptordin »

If anyone is interested I compromised by removing choice options as obviously that is what was confusing it.

Code: Select all

@echo off
REM This script converts and encodes all MKV files in the current folder and subfolders to MP4 files using HandBrakeCLI.exe
REM It also checks the file sizes and deletes the source file if it is larger than the encoded file
REM It resizes all videos to 720p
REM It uses nvenc_h265 for encoding and keeps the video quality at 27
REM It keeps all subtitles and all audio tracks and encodes audio in mp3 with 96 bitrate
REM It also uses the specified settings for encoding
REM It creates a log file in the startup folder called log.txt that records success or failure status and the input and output file sizes

setlocal enabledelayedexpansion
REM Set the path to HandBrakeCLI.exe
set HANDBRAKE=HandBrakeCLI.exe
REM Create the log file if it does not exist
if not exist log.txt echo Log file for HandBrakeCLI.exe batch script > log.txt
REM Loop through all MKV files in the current folder and subfolders
for /r %%f in (*.mkv) do (
    REM Get the file name without extension
    set FILENAME=%%~nf
    REM Get the file size in bytes
    set INPUTSIZE=%%~zf
    REM Set the resize filter to 720p
    set RESIZE=--width 1280 --height 720
    REM Set the audio encoder to mp3 with 96 bitrate
    set AUDIO=--aencoder mp3 --ab 96
    REM Set the output file name with MP4 extension
    set OUTPUTFILE=%%~dpnf.mp4
    REM Encode the file using HandBrakeCLI.exe with the specified settings
    %HANDBRAKE% -i "%%f" -o "!OUTPUTFILE!" --encoder nvenc_h265 --quality 27 --all-subtitles --all-audio !RESIZE! !AUDIO! -x cabac=1:ref=5:analyse=0x133:me=umh:subme=9:chroma-me=1:deadzone-inter=21:deadzone-intra=11:b-adapt=2:rc-lookahead=60:vbv-maxrate=10000:vbv-bufsize=10000:qpmax=69:bframes=5:b-adapt=2:direct=auto:crf-max=51:weightp=2:merange=24:chroma-qp-offset=-1:sync-lookahead=2:psy-rd=1.00,0.15:trellis=2:min-keyint=23:partitions=all
    REM Check the exit code of HandBrakeCLI.exe
    if !ERRORLEVEL! EQU 0 (
        REM Get the output file size in bytes
        for %%g in ("!OUTPUTFILE!") do set OUTPUTSIZE=%%~zg
        REM Check if the output file is equal to or smaller than the input file
        if !OUTPUTSIZE! EQU !INPUTSIZE! (
            REM Delete the source file
            del "%%f"
            REM Write the success status and the file sizes to the log file
            echo Successfully encoded "%%f" to "!OUTPUTFILE!" and deleted the source file. Input file size: !INPUTSIZE! bytes. Output file size: !OUTPUTSIZE! bytes. >> log.txt
        ) else if !OUTPUTSIZE! GTR !INPUTSIZE! (
            REM Delete the output file
            del "!OUTPUTFILE!"
            REM Write the failure status and the file sizes to the log file
            echo Failed to delete the output file "!OUTPUTFILE!" after encoding from "%%f" because the output file is larger than the input file. Input file size: !INPUTSIZE! bytes. Output file size: !OUTPUTSIZE! bytes. >> log.txt
        ) else (
            REM Delete the input file
            del "%%f"
            REM Write the success status and the file sizes to the log file
            echo Successfully encoded "%%f" to "!OUTPUTFILE!" and deleted the input file because the input file is larger than the output file. Input file size: !INPUTSIZE! bytes. Output file size: !OUTPUTSIZE! bytes. >> log.txt
        )
    ) else (
        REM Write the error code and the file names to the log file
        echo Error !ERRORLEVEL! occurred while encoding "%%f" to "!OUTPUTFILE!". >> log.txt
    )
)
REM End the script
endlocal
ccccc
Posts: 14
Joined: Sat Feb 22, 2014 8:58 am

Re: Problem resizing only larger than 720p in script/

Post by ccccc »

Much about the script confuses me, and I'm not sure that you meant to say that you're converting MKV to MP3? I'm also confused why you'd be resampling the audio to 44.1 unless you're making CDs (that's just screwing with the audio quality for zero advantage).

Either way, a batch file that will convert MKV's to MP4's (what I presume you meant to say) recursively in all the directories, with MP3 audio (if you were going for that), and make sure nothing is higher than 720p (though I'd use "--maxWidth 1280" instead to account for aspect ratios):
@ECHO OFF
setlocal enabledelayedexpansion
FOR /R %%A IN (*.mkv) DO (
HandBrakeCLI.exe -f mp4 -i "%%A" -o "%%~dpnA.mp4" -m -e nvenc_h265 --encoder-preset slow --loose-anamorphic --crop 0:0:0:0 -q 27 --maxHeight 720 -E mp3 -Q 7 --all-subtitles
ECHO ======================= )
endlocal
ECHO.
ECHO =======================
ECHO =======================
ECHO.
ECHO Conversion completed.
ECHO.
ECHO =======================
ECHO =======================
PAUSE
The detailed encoding parameters are unnecessary. The MP3 encoder should use the quality preset, not bitrate. The slow encoder preset is the best size/quality setting for nvenc in my experience.
Raptordin
Posts: 13
Joined: Sun Mar 19, 2023 10:51 pm

Re: Problem resizing only larger than 720p in script/

Post by Raptordin »

Thanks for your reply and sorry for the late reply.

Well, My Script basically encodes MKV files to MP4 with quality 27 and MP3 96 kbs for audio.
I would like to make it so that if the Audio is more than 2 channels it will leave the track as is and just copy it, and if the video resolution is more than 720 resize it to 720, so as you see my problem is with the scripting itself not the HandbrakeCLI application.
My Code is below for your viewing.

Code: Select all

@echo off
REM This script converts and encodes all MKV files in the current folder and subfolders to MP4 files using HandBrakeCLI.exe
REM It also checks the file sizes and deletes the source file if it is larger than the encoded file
REM It leave video resolution as is without changingit
REM It uses nvenc_h265 for encoding and keeps the video quality at 27
REM It keeps all subtitles and all audio tracks and encodes audio in mp3 with 96 bitrate
REM It also uses the specified settings for encoding
REM It creates a log file in the startup folder called log.txt that records success or failure status and the input and output file sizes

setlocal enabledelayedexpansion
REM Set the path to HandBrakeCLI.exe
set HANDBRAKE=HandBrakeCLI.exe
REM Create the log file if it does not exist
if not exist log.txt echo Log file for HandBrakeCLI.exe batch script > log.txt
REM Loop through all MKV files in the current folder and subfolders
for /r %%f in (*.mkv) do (
    REM Get the file name without extension
    set FILENAME=%%~nf
    REM Get the file size in bytes
    set INPUTSIZE=%%~zf
    REM Set the audio encoder to mp3 with 96 bitrate
    set AUDIO=--aencoder mp3 --ab 96
    REM Set the output file name with MP4 extension
    set OUTPUTFILE=%%~dpnf.mp4
    REM Encode the file using HandBrakeCLI.exe with the specified settings
    %HANDBRAKE% -i "%%f" -o "!OUTPUTFILE!" --encoder nvenc_h265 --quality 29 --all-subtitles --all-audio !AUDIO! -x cabac=1:ref=5:analyse=0x133:me=umh:subme=9:chroma-me=1:deadzone-inter=21:deadzone-intra=11:b-adapt=2:rc-lookahead=60:vbv-maxrate=10000:vbv-bufsize=10000:qpmax=69:bframes=5:b-adapt=2:direct=auto:c
    REM Check the exit code of HandBrakeCLI.exe
    if !ERRORLEVEL! EQU 0 (
        REM Get the output file size in bytes
        for %%g in ("!OUTPUTFILE!") do set OUTPUTSIZE=%%~zg
        REM Check if the output file is equal to or smaller than the input file
        if !OUTPUTSIZE! EQU !INPUTSIZE! (
            REM Delete the source file
            del "%%f"
            REM Write the success status and the file sizes to the log file
            echo Successfully encoded "%%f" to "!OUTPUTFILE!" and deleted the source file. Input file size: !INPUTSIZE! bytes. Output file size: !OUTPUTSIZE! bytes. >> log.txt
        ) else if !OUTPUTSIZE! GTR !INPUTSIZE! (
            REM Delete the output file
            del "!OUTPUTFILE!"
            REM Write the failure status and the file sizes to the log file
            echo Failed to delete the output file "!OUTPUTFILE!" after encoding from "%%f" because the output file is larger than the input file. Input file size: !INPUTSIZE! bytes. Output file size: !OUTPUTSIZE! bytes. >> log.txt
        ) else (
            REM Delete the input file
            del "%%f"
            REM Write the success status and the file sizes to the log file
            echo Successfully encoded "%%f" to "!OUTPUTFILE!" and deleted the input file because the input file is larger than the output file. Input file size: !INPUTSIZE! bytes. Output file size: !OUTPUTSIZE! bytes. >> log.txt
        )
    ) else (
        REM Write the error status to the log file
        echo Error encoding "%%f" to "!OUTPUTFILE!". Error code: !ERRORLEVEL! >> log.txt
    )
)

Post Reply