Use Powershell to Batch Convert Videos using Handbrake

I recently downloaded a large amount of training video from the good old interweb, but they were all in the awful WMV format. After playing around with Handbrake a bit I knew it was the tool for the job, however it will not recursively add folders to its encoding queue and with several hundred videos nested a few layers deep I wasn’t about to add all of them by hand.

So opened up notepad and got to work writing some code to query the file system for all the WMV in a specific folder and any sub folders and then queue them up to pass into HandbrakeCLI. It also does a nice clean header and tracks progress so you can estimate how long it might have to go. It will just place the new converted file in the same folder as the current one. You will want to adjust the HandBrakeCLI arguments to encode your video with the quality and settings you desire, these are pretty aggressive and mostly to save space while re-encoding video created with screen capture software.

Code

$filelist = Get-ChildItem D:\Video\ -filter *.wmv -recurse

$num = $filelist | measure
$filecount = $num.count

$i = 0;
ForEach ($file in $filelist)
{
    $i++;
    $oldfile = $file.DirectoryName + "\" + $file.BaseName + $file.Extension;
    $newfile = $file.DirectoryName + "\" + $file.BaseName + ".mp4";
     
    $progress = ($i / $filecount) * 100
    $progress = [Math]::Round($progress,2)

    Clear-Host
    Write-Host -------------------------------------------------------------------------------
    Write-Host Handbrake Batch Encoding 
    Write-Host "Processing - $oldfile"
    Write-Host "File $i of $filecount - $progress%"
    Write-Host -------------------------------------------------------------------------------
    
    Start-Process "C:\Program Files\HandBrake\HandBrakeCLI.exe" -ArgumentList "-i `"$oldfile`" -t 1 --angle 1 -c 1 -o `"$newfile`" -f mp4  -O  --decomb --modulus 16 -e x264 -q 32 --vfr -a 1 -E lame -6 dpl2 -R Auto -B 48 -D 0 --gain 0 --audio-fallback ffac3 --x264-preset=veryslow  --x264-profile=high  --x264-tune=`"animation`"  --h264-level=`"4.1`"  --verbose=0" -Wait -NoNewWindow
}

Console Output

-------------------------------------------------------------------------------
Handbrake Batch Encoding
Processing - D:\Video\subfolder\Installing IIS on Windows Server Core.wmv
File 10 of 100 - 10.00%
-------------------------------------------------------------------------------
HandBrake 0.9.9 (2013052900) - MinGW x86_64 - http://handbrake.fr
8 CPUs detected
Opening D:\Video\subfolder\Installing IIS on Windows Server Core.wmv...
libbluray/bdnav/index_parse.c:162: indx_parse(): error opening D:\Video\subfolde
r\Installing IIS on Windows Server Core.wmv/BDMV/index.bdmv
libbluray/bdnav/index_parse.c:162: indx_parse(): error opening D:\Video\subfolde
r\Installing IIS on Windows Server Core.wmv/BDMV/BACKUP/index.bdmv
libbluray/bluray.c:1725: nav_get_title_list(D:\Video\subfolder\Installing IIS on
 Windows Server Core.wmv) failed (000000000035D900)
libdvdnav: Using dvdnav version 4.1.3
libdvdread: Encrypted DVD support unavailable.
libdvdnav:DVDOpenFileUDF:UDFFindFile /VIDEO_TS/VIDEO_TS.IFO failed
libdvdnav:DVDOpenFileUDF:UDFFindFile /VIDEO_TS/VIDEO_TS.BUP failed
libdvdread: Can't open file VIDEO_TS.IFO.
libdvdnav: vm: failed to read VIDEO_TS.IFO
Input #0, asf, from 'D:\Video\subfolder\Installing IIS on Windows Server Core.wmv':
  Metadata:
    title           : Video1
    artist          : 
    WMFSDKVersion   : 11.0.5721.5251
    WMFSDKNeeded    : 0.0.0.0000
    IsVBR           : 1
    VBR Peak        : 1437
    Buffer Average  : 1077
  Duration: 00:57:33.22, start: 0.000000, bitrate: 135 kb/s
    Stream #0.0(eng): Audio: wmav2, 44100 Hz, 1 channels, fltp, 48 kb/s
    Stream #0.1(eng): Video: wmv3 (Main), yuv420p, 800x600, 74 kb/s, 15 fps, 15
tbr, 1k tbn
Scanning title 1 of 1, preview 10, 100.00 %+ title 1:
  + stream: D:\Video\subfolder\Installing IIS on Windows Server Core.wmv
  + duration: 00:57:33
  + size: 800x600, pixel aspect: 1/1, display aspect: 1.33, 15.000 fps
  + autocrop: 0/0/0/0
  + chapters:
    + 1: cells 0->0, 0 blocks, duration 00:57:33
  + audio tracks:
    + 1, English (wmav2) (1.0 ch) (iso639-2: eng)
  + subtitle tracks:
x264 [warning]: --psnr used with psy on: results will be invalid!
x264 [warning]: --tune psnr should be used if attempting to benchmark psnr!
x264 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.1 Cache64
x264 [info]: profile High, level 4.1
Encoding: task 1 of 1, 52.54 % (80.50 fps, avg 83.86 fps, ETA 00h04m53s))

Read More