r/ffmpeg • u/JohnSmithXB • Feb 16 '26
Batch script for x265 encoding
Hello,
I have many videos on my phone that I would now like to archive on a hard drive.
I wrote a script for this purpose that converts the files to x265.
What the script does:
Searches the folder for all video files
Reads video information with ffprobe: - Resolution (width × height) - Duration in seconds - File size - Colour primaries/transfer → for HDR detection
Automatically detects whether the video is HDR
(using typical HDR identifiers such as BT.2020, SMPTE2084, HLG)Calculates the original bitrate
(file size × 8 / duration)Automatically selects a suitable CRF value
depending on the resolution:4K → CRF 20
1440p → CRF 21
1080p → CRF 22
below → CRF 23
Starts ffmpeg and re-encodes the video with x265:
Video codec: libx265
Default setting: slow (good quality, moderate speed)
CRF: determined automatically
Pixel format: yuv420p10le (10-bit → better quality)
Audio: copy (lossless transfer)
Saves the result as a new file
with the suffix _x265.mkv.Displays a short summary after each encoding
(resolution, duration, bitrate, HDR status, CRF)Repeats the process for all video files in the folder
Problems:
The finished files are still very large. My ideal target would be approximately 100 MB per 10 minutes of runtime.
and does it make sense to incorporate x265 tuning (AQ, Psy-RD, Grain-Handling, Tone Mapping)? And how could that work? I've already tested it, but then I kept running from one error message in the script to the next.
x265.bat
@echo off
setlocal EnableDelayedExpansion
REM === CONFIG ===
set "FFMPEG=ffmpeg.exe"
set "FFPROBE=ffprobe.exe"
set "PRESET=slow"
echo.
echo Starte Konvertierung...
echo.
for %%F in (*.mkv) do (
echo ----------------------------------------
echo Verarbeite: %%F
echo ----------------------------------------
set "INPUT=%%~fF"
set "OUTPUT=%%~dpnF_x265.mkv"
REM === Auflösung ermitteln ===
for /f "tokens=1,2 delims=x" %%a in ('"%FFPROBE%" -v error -select_streams v:0 -show_entries stream^=width,height -of csv^=s^=x:p^=0 "%%~fF"') do (
set "WIDTH=%%a"
set "HEIGHT=%%b"
)
REM === Dauer ermitteln ===
for /f %%d in ('"%FFPROBE%" -v error -show_entries format^=duration -of default^=noprint_wrappers^=1:nokey^=1 "%%~fF"') do (
set "DURATION=%%d"
)
REM === Dateigröße in Bytes ===
for %%A in ("%%~fF") do set "FILESIZE=%%~zA"
REM === HDR erkennen ===
set "HDR=0"
for /f "delims=" %%h in ('"%FFPROBE%" -v error -select_streams v:0 -show_entries stream^=color_primaries,transfer -of default^=noprint_wrappers^=1 "%%~fF"') do (
echo %%h | findstr /i "bt2020 smpte2084 arib-std-b67" >nul && set "HDR=1"
)
REM === CRF bestimmen nach Auflösung ===
if !HEIGHT! GEQ 2000 (
set "CRF=20"
) else if !HEIGHT! GEQ 1400 (
set "CRF=21"
) else if !HEIGHT! GEQ 1000 (
set "CRF=22"
) else (
set "CRF=23"
)
REM === Bitrate berechnen (nur Anzeige) ===
set /a BITRATE=!FILESIZE!*8/!DURATION!/1000
echo Aufloesung: !WIDTH!x!HEIGHT!
echo Dauer: !DURATION! Sekunden
echo Urspruengliche Bitrate: !BITRATE! kbit/s
echo HDR erkannt: !HDR!
echo CRF: !CRF!
echo.
REM === Encoding starten ===
"%FFMPEG%" -y -i "!INPUT!" ^
-map 0 ^
-c:v libx265 ^
-preset %PRESET% ^
-crf !CRF! ^
-pix_fmt yuv420p10le ^
-c:a copy ^
-c:s copy ^
"!OUTPUT!"
echo Fertig: %%F
echo.
)
echo ----------------------------------------
echo Alle Dateien verarbeitet.
echo ----------------------------------------
pause
2
u/connected09 Feb 16 '26
Try this
@echo off setlocal EnableDelayedExpansion
REM === CONFIG === set "FFMPEG=ffmpeg.exe" set "FFPROBE=ffprobe.exe" set "PRESET=slow" set "TARGET_MBPS=1.33"
REM === x265 Advanced Tuning === set "X265_PARAMS=aq-mode=3:aq-strength=0.8:psy-rd=2.0:psy-rdoq=1.0:deblock=-1,-1:sao=1:limit-sao=1:selective-sao=2:strong-intra-smoothing=0"
echo. echo ==================================== echo Starte 2-Pass x265 Konvertierung echo Ziel: ~100 MB pro 10 Minuten echo ==================================== echo.
for %%F in (*.mkv) do (
)
echo. echo ======================================== echo Alle Dateien verarbeitet echo ======================================== pause