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))
Which version of Handbrake are you using? I assume from the paths above you are using the 32bit version? I’ve tried using the script you posted above on a 64bit Windows 7 system and despite the -NoNewWindow switch, I ALWAYS get a CMD window that opens with no data in it. (It does appear that the encoding is going on in the background because a file is generated). Any tips on how to get output like you described? Were there environment configurations that you set previously to customize your workspace? Part of what I really liked about your script was that it seemed to show status as it progressed.
Thanks!
Dear Andy,
The script of wayne zimmeman is amazing. The only thing that you have to modify is to remove this line : –angle 1 ( because it is for DVD or Blu-ray only) -E ac3 and .m4v for the extension and the script works fine for me.
regards
Thanks Wayne this is great.
I have a 500 .avi files to convert and this is just what I needed.
One thing though – when I first tried it I could not get it work at all – the handbrake process terminated as soon as it launched. I traced the problem down to an issue with the argument list in your post. There are a couple of extra white spaces in the script – here for example
`”$newfile`” -f mp4 -O –decomb
double spaces after mp4 and -O
Which for some reason appears as ” mp4┬á -O┬á ” when run at the command prompt. Delete them and replace with spaces and it works just fine. I’ve no idea why it’s doing that, but thought it might help others who have similar issues.
After mp4
[…] This site has a very good example. However, I used the GUI to build up a profile and then export that to a file. The only thing I had to add was setting the first subtitle stream to be enabled by default. (I don’t know why that setting isn’t preserved in the presets.) […]