Tool

Batch Folder Encode Script Builder

You may have shrunk one file with HandBrake or ffmpeg, but when dozens are piled up in a folder, you can’t repeat the same settings file by file. Choose the input and output folders and a codec, and the tool generates a script that converts every video in the folder in turn and skips files that were already converted the next time it runs. Using the encode times and bitrates we measured, it also shows the total time and expected size.

1. Tool and shell

2. Folders and target files

3. Encoding settings

Target quality

HandBrake's RF is known to be passed straight through to the encoder's CRF for x264 and x265. For SVT-AV1, check the value in the activity log.

Output container

Audio

With “copy”, a file fails if its audio codec can't go into the selected container. Audio was not measured and is not included in the size estimate below.

Use my HandBrake preset JSON (optional)

If you fill in both, the script loads the preset with --preset-import-file and -Z instead of the codec, quality, container and audio settings. The preset decides the container, so match the extension above to the preset.

4. Total footage in the folder (for the time estimate)

The sum of the durations of all videos in the folder. If files differ in resolution or fps, pick the most common one. The commands that sum the durations are in the notes below.

Loading the measured data…

6. Script

Set a RF value (enter it yourself if the target quality is outside the measured range).

All calculation and script generation happens only inside this browser, and no folder names or files are sent anywhere. This tool only generates the script and does not run it. The measurements used 4 threads per job at IDLE priority, so treat the speeds as a rough guide.

Why a script?

HandBrake’s graphical interface is known to have no watch-folder feature that monitors a folder and converts new files automatically, and when you drag a folder in, files inside its subfolders are reportedly sometimes not added to the queue. The command-line tools HandBrakeCLI and ffmpeg can handle this kind of repetition with a single loop. This tool builds that loop as one script: find the files, skip what is already done, convert them one at a time, and tally the results.

How to use it

  1. Choose the tool (HandBrakeCLI or ffmpeg) and the shell. Use the PowerShell version on Windows and the bash version on macOS or Linux.
  2. Enter the input folder, the output folder and the target extensions. The output folder must differ from the input folder. If you turn on subfolders, the same folder structure is created in the output folder.
  3. Choose the codec, the preset and the target quality. Once you pick a target quality, the CRF (RF in HandBrake) is set automatically from the measured data. If you already use a particular value, enter it yourself.
  4. Enter the total length of the videos in the folder, and the total time and size appear. You can get the total with the commands below.
  5. Save the script and try it first on a folder with just two or three files. Check the results before you use it on the whole folder.

What the script does

  • Skipping files that are already converted: if the output folder has a file with the same relative path and the same name (only the extension changes to the output container) and its size is not 0, the file is skipped. So if the run stopped halfway, or new files were added afterward, running it again converts only what is left.
  • Keeping an interrupted file from being mistaken for a finished one: while a file is being converted, it is written under a temporary name such as name.part.mp4, and it is renamed to the final name only when the encoder succeeds (exit code 0). If you stop the script midway, only the temporary file is left, and the next run converts that file again from the start.
  • One failed file does not stop the run. Failed files are collected into a list and written to failed.txt in the output folder. At the end, the script prints the number of files converted, skipped and failed, and the elapsed time, on one line.
  • Even if the output folder is inside the input folder, files in the output folder are excluded from the targets. This prevents already converted files from being converted again.
  • If you turn on low priority, the PowerShell version sets this window to ‘Below Normal’ priority so the encoder inherits the same priority, and the bash version runs the encoder with nice -n 10. Keep it on if you need to use the PC for other things while it encodes.

HandBrakeCLI or ffmpeg?

  • HandBrakeCLI: a good fit when you want to carry on the settings you already use in the HandBrake window. If you enter the path of the file you downloaded from the HandBrake Preset JSON Builder and the preset name in the fields above, the script loads that preset with --preset-import-file and -Z. If you don’t use a preset, this tool passes the codec, preset, RF and audio as arguments itself.
  • ffmpeg: lighter if you only need a simple conversion without extras such as subtitles or chapters. This script includes only the first video stream and all audio streams, and does not include subtitles. It specifies -pix_fmt yuv420p to match the 8-bit measurements, so a 10-bit source is converted to 8-bit. For a command to use on a single file, the FFmpeg Command Builder is simpler.
  • HandBrake’s RF is known to be passed straight through to the encoder’s CRF for x264 and x265. To translate numbers between the two tools, see the HandBrake RF ↔ FFmpeg CRF conversion chart.

How are the total time and size calculated?

  • Data: the raw data of our measurement of encode time for an hour of 1080p video. We encoded four 10-second 1080p25 clips at three CRF values for each codec and preset, and measured the time taken, the bitrate and the VMAF. The measurements were made with FFmpeg 9.0.2 (gyan.dev full build) on a single AMD Ryzen 5 PRO 4650G (6 cores, 12 threads).
  • Choosing the CRF: for each clip, we connect the measured points with straight lines to read the CRF that corresponds to the target VMAF, take the lowest of the four values (based on the most demanding clip), and round it down to an integer. That is why easier footage ends up with a higher VMAF than the target. It is the same rule as the preset JSON tool. However, the data for this tool has only three CRF points per codec and preset, so the readings between points are coarser.
  • Time: at that CRF, we read the ‘time per frame’ for each clip and multiply it by the total number of frames (length × fps). Based on the fastest clip it is ‘easy footage’, the mean of the four is ‘average’, and based on the slowest clip it is ‘demanding footage’. Simple footage such as scenery and complex footage such as waves or leaves differ a lot, so if you don’t know which your footage is closer to, look at all three numbers.
  • Size: the per-clip bitrate read at the same CRF multiplied by the length, shown as a range from the smallest clip to the largest. Audio and container overhead are not included, and it is shown only for 1080p. The same CRF gives very different bitrates depending on the content, so a wide range is normal.
  • Calibrating to your PC: encode a 1080p video briefly with the same codec and preset, enter the fps= value from the progress line, and all times are scaled by the ratio to the measured average. It is an approximation that corrects everything with a single value.
  • When you run it again: files already converted are skipped, so for the second run, enter only the length of the remaining videos when you calculate the estimated time.

Summing the lengths of the videos in a folder

If ffprobe (distributed with ffmpeg) is on your PATH, you can use the commands below to see the total length in hours. Change the folder name to your own. If the output folder is inside the input folder, the lengths of converted files are added in too, so it is best to run this before the first run.

PowerShell

$s = 0
Get-ChildItem -LiteralPath 'D:\videos' -File -Recurse | Where-Object { '.mp4','.mkv','.mov' -contains $_.Extension.ToLower() } | ForEach-Object {
  $d = ffprobe -v error -show_entries format=duration -of csv=p=0 $_.FullName
  if ($d -match '^[0-9.]+$') { $s += [double]$d }
}
'{0:N1} hours' -f ($s / 3600)

bash

find /data/videos -type f \( -iname '*.mp4' -o -iname '*.mkv' -o -iname '*.mov' \) -print0 \
  | xargs -0 -I{} ffprobe -v error -show_entries format=duration -of csv=p=0 {} \
  | awk '{s+=$1} END {printf "%.1f hours\n", s/3600}'

Limitations to keep in mind

  • This tool only generates the script and does not run it, and we have not been able to test the generated script in every environment. Options can differ by HandBrakeCLI and ffmpeg version, so check with a few files first. If the executable can’t be found or you get an option error, the FFmpeg Error Log Explainer and the FFmpeg Build Capability Checker can help.
  • The measurements cover four 10-second clips, 8-bit, on one PC. The times were measured with 4 threads per job at IDLE priority, so treat them as a rough guide; they can differ a lot depending on your PC’s core count and HandBrake’s own thread settings. Hardware encoders (NVENC, QSV, AMF) were not measured. For resolutions other than 1080p, the time is scaled assuming it is proportional to pixel count.
  • The time and size of files made by HandBrake can differ from this estimate. The measurements were made with FFmpeg’s encoders and do not reflect HandBrake’s internal filters (deinterlacing, denoising and so on) or preset settings. Check the activity log to see whether SVT-AV1’s RF is on the same scale as CRF.
  • It is not a quality guarantee. The target VMAF is a reference read from four clips, so it does not mean your footage will get the same score. For important videos, convert one file first and check it with the Video A/B Quality Compare Slider or the VMAF batch measurement script.
  • Name collisions: if a.mkv and a.mp4 are in the same folder, both would be written to a.mp4, so only the one processed first is converted and the other is skipped as ‘already exists’. Check beforehand whether any files share a name with a different extension.
  • It does not touch the originals, but it does not delete them either. Clean up yourself after you have checked the results. Extras such as subtitles and chapters are not included in the ffmpeg version, and we did not check whether metadata and HDR information are preserved. For HDR video, look at the HDR10 Metadata-Preserving Encode Helper first.
  • The bash version assumes a typical macOS or Linux environment that has find and nice. It is written to read file names that contain line breaks, but we have not tested such names.