Reduce .mkv file size in a batch using Handbrake CLI and PowerShell

Problem:

I often need to reduce the size of multiple .mkv files and using the GUI is slow and cumbersome.

Solution:

Put all files in the F:\Convert directory and run the below script:

$videofiles = Get-ChildItem F:\Convert -filter *.mkv -recurse
 
$num = $videofiles | measure
$filecount = $num.count
 
$i = 0;ForEach ($file in $videofiles)
{
    $i++;$origfile = $file.DirectoryName + "\" + $file.BaseName + $file.Extension;$newfile = "D:\Temp" + \ + $file.BaseName + ".mkv";
      
    $progress = ($i / $filecount) * 100
    $progress = [Math]::Round($progress,2)
 
    Clear-HostWrite-Host -------------------------------------------------------------------------------
    Write-Host Handbrake Batch Encoding
    Write-Host "Processing - $origfile"Write-Host "File $i of $filecount - $progress%"Write-Host -------------------------------------------------------------------------------
     
    Start-Process "C:\ProgramData\chocolatey\lib\handbrake.portable\tools\HandBrakeCLI.exe" -ArgumentList "-i `"$origfile`" -t 1 -o `"$newfile`" -f mkv -O  --decomb -e x264 -q 22 --pfr -a 1 -E copy:ac3 -R Auto --x264-preset=fast  --x264-profile=main  --h264-level=`"4.0`"  --verbose=0" -Wait -NoNewWindow
}


 

 

The switches I used are below, see here for more information:

 -i, --input <string>    Set input file or device ("source")
   -t, --title <number>    Select a title to encode (0 to scan all titles
                           only, default: 1)
   -f, --format <string>   Select container format:
                               av_mp4
                               av_mkv
                               av_webm
                           default: auto-detected from destination file name)
   -O, --optimize          Optimize MP4 files for HTTP streaming (fast start,
                           s.s. rewrite file to place MOOV atom at beginning)
   -e, --encoder <string>  Select video encoder:
                               x264
                               x264_10bit
                               x265
                               x265_10bit
                               x265_12bit
                               mpeg4
                               mpeg2
                               VP8
                               VP9
                               theora
  -q, --quality <float>   Set video quality (e.g. 22.0)
   -a, --audio <string>    Select audio track(s), separated by commas
                           ("none" for no audio, "1,2,3" for multiple
                           tracks, default: first one).
                           Multiple output tracks can be used for one input.
   -E, --aencoder <string> Select audio encoder(s):
                               none
                               ca_aac
                               ca_haac
                               copy:aac
                               ac3
                               copy:ac3
                               eac3
                               copy:eac3
                               copy:truehd
                               copy:dts
                               copy:dtshd
                               mp3
                               copy:mp3
                               vorbis
                               flac16
                               flac24
                               copy:flac
                               opus
                               copy
                           "copy:<type>" will pass through the corresponding
                           audio track without modification, if pass through
                           is supported for the audio type.
                           Separate tracks by commas.
                           Defaults:
                               av_mp4   ca_aac
                               av_mkv   ca_aac
                               av_webm  vorbis
   -R, --arate             Set audio samplerate(s)
                           (8/11.025/12/16/22.05/24/32/44.1/48 kHz)
                           or "auto". Separate tracks by commas.

Leave a Reply

Your email address will not be published. Required fields are marked *