• Batching video transcodes with Handbrake (Windows)

    by  • February 22, 2010 • Handbrake, transcode, windows • 9 Comments

    I keep all my media in a central location on a network store but I want to be able to play it on my iphone when I’m away without network connectivity. Most of the content is avi/divx/xvid which the iphone does not play natively.

    Handbrake is a really slick transcoder which converts a wide number of video formats to those compatible with mobile devices. It offers a number of built-in profiles for many mobile devices, including the iphone, and comes as UI or command line interface.

    Here’s a quick and dirty batch script for Windows which processes a given folder (this assumes you installed Handbrake into the default directory):

    1. FOR /F "tokens=*" %%G IN (DIR /B /S *.avi’) DO "C:\Program Files\Handbrake\HandBrakeCLI" -i "%%G" -o "%%G".mp4 —-preset="iPhone & iPod Touch"

    Copy this into a .bat file in the root of the media folder you wish to transcode the media from. This script will iterate through all the .avi files in the folder, calling Handbrake, and creating an .mp4, suitable for playback on the iphone/ipod touch.

    You can customise the batcher to suit, replacing *.avi in the DIR part of the batch file (which provides the input to drive Handbrake) with the type of media file you want to deal with, or replacing the --preset argument with any of those mentioned in the Handbrake wiki (there are profiles for most common devices, including Xbox 360, PSP, PS3, AppleTV etc). Great thing is, I can run the script and leave the PC whirring away overnight

    About

    .NET developer at thetrainline.com, previously web developer at MRM Meteorite. Awarded a PhD in misbehaviour detection in wireless ad-hoc networks.A keen C# ASP.net developer bridging the gap with APIs and JavaScript frameworks, one web app at a time.

    http://www.paulkiddie.com

    9 Responses to Batching video transcodes with Handbrake (Windows)

    1. Mat Landers
      October 3, 2011 at 3:44 am

      Mr. Kiddie,

      Thank you SO MUCH for this batch script! I did have to make a few minor changes to it in order to get it to work. I wanted to let you and your audience know so they can easily use this if they have a similar setup.

      On the DIR part, make sure to replace the single quotation with the single quotation next to your enter key on the keyboard. For some reason it is not the same character and will not be interpreted correctly. The same thing is with the –preset. The first dash seems to be a little longer.

      For 64-bit systems (I am running Windows 7 Ultimate 64-bit) it is important to remember that the default installation is in the Program Files (x86) folder, not simply Program Files.

      Excellent script! I am using it right now.

      Thanks a bunch!

      Mat

    2. Corrected
      November 16, 2011 at 11:37 am

      FOR /F “tokens=*” %%G IN (‘DIR /B /S *.avi’) DO “C:\Program Files\Handbrake\HandBrakeCLI” -i “%%G” -o “%%G”.mp4 –preset=”iPad”

    3. dave
      March 14, 2012 at 6:32 am

      This is exactly what I am looking for. The only problem is that I’m a computer dud so don’t know how to execute a batch script………

      Any chance you are able to provide some more detail?

    4. nonob
      June 30, 2012 at 2:49 pm

      Hi,
      I want to use a different folder than source for encoded files, so i modified code like that:
      FOR /F “tokens=*” %%G IN (‘DIR /B /S *.mov’) DO “C:\Program Files\Handbrake\HandBrakeCLI” -i “%%G” -o

      “C:\Documents and Settings\Nonob\Mes documents\Videos encod‚es\”.mkv -f mkv -w 720 -l 400 -e x264 -q 20

      –vfr -a 1 -E faac -B 64 -6 mono -R Auto -D 0 –gain=0 –audio-copy-mask none –audio-fallback ffac3 -x

      ref=1:weightp=1:subq=2:rc-lookahead=10:trellis=0:8x8dct=0 –verbose=1
      pause

      But haw can i keep source file name for encoded files, without old extension ?

      Thanks

    5. John
      July 10, 2012 at 4:19 am

      Could someone help me with how to iterate through subfolders with this script?

    6. skanga
      September 30, 2012 at 7:58 am

      Try something like this to exclude the old extension from the file name

      FOR /F “tokens=*” %%G IN (‘DIR /B /S *.mov’) DO “C:\Program Files\Handbrake\HandBrakeCLI” -i “%%~nG.mov” -o “%%~nG.mkv” —-preset=”High Profile”

    7. schmuckle
      March 9, 2013 at 2:04 pm

      Maybe the syntax changed for Windows 8, but this is what I used to dump the file extension:

      FOR /F “tokens=*” %%G IN (‘DIR /B /S *.wmv’) DO “C:\Program Files\Handbrake\HandBrakeCLI” -i “%%G” -o “%%~dG%%~pG%%~nG.mp4″ –preset=”High Profile”

      Think of %%G as the variable with the file names in it. Therefore %%~dG becomes just the drive letter and %%~pG is just the path and %%~nG is just the file name without the extension. For example

      %%G = D:\Videos\Movie File.wmv
      %%~dG = D:
      %%~pG=\Videos\
      %%~nG=Movie File

      HTH :)

    8. July 15, 2013 at 12:37 pm

      Hi

      I must be really thick, i am running windows homeserver and cannot get the script to run

      FOR /F “tokens=*” %%G IN (‘DIR /B /S *.avi’) DO “C:\Program Files\Handbrake\HandBrakeCLI” -i “%%G” -o “%%G”.mp4 —-preset=”iPad”

      I keep getting

      %%G was unexpected at this time.

    9. Mateo
      September 2, 2013 at 8:21 pm

      How you can do if you transcode mkv to m4v but you want only enlighs DTS track and spanish AC3 by that order?

    Leave a Reply

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