Welcome Visitor…

Perhaps you will be wondering how come this Chemical engineer got mixed up with computer stuff & is talking about religion & nationality, something totally unknown to this unknown guy!

Well read through my musings then & you will be given an opportunity to change your perspective perhaps for the good or perhaps for the worst.

So go on & take a peek...

Don't Show it again

Follow me on:

Wednesday, October 24, 2012

Automating, Sequencing & Logging: Beauty of x264CLI Video Encoding

x264 Video encoding is perhaps the best OpenSource compression utility without degrading video playback quality. The graphical interfaces (GUIs) available out there in my view seriously handicap of what x264 can really do. Of coarse one needs to learn a bit of encoding syntax but once learned you can encode all of your videos to ~half the size without losing your precious quality. One must however realise that this encoding is extremely time consuming & without necessary logging & automation x264 encoding may not prove trivial enough for a common user.

I have created a set of batch (.bat) files that run under Windows platform but can easily be ported to Linux based OS bash language or one can run them under Wine.

Functionality provided

My batch files will scan a given folder for the videos present there. Then filter them against a set of rules, then created necessary files. After an optional check you just have to run the main batch file created & all the encoding runs smoothly & sequentially. You can go & sleep because necessary logging is saved to a text file for you to revise afterwards.

Logging file created will only contain necessary information of the encode like duration & file names giving a clear picture which file demanded greatest time to encode.

How to

  • You need folder path containing all the video files to be converted
  • Folder path where the downloaded files are located
  • Files to download are a total of three namely x264_automator.bat, mtee.exe & x264.exe
  • A good renaming utility can suffix required files with a unique text string to let the automator identify in which profile filter it has to place this respective file e.g _profA or _profB

x264_automator as the name applies is the file that will create further files necessary for automatic sequential encoding. mtee.exe & x264.exe are 3rd party files needed by the x264_automator.bat

More about mtee

x264 build I use

Download my x264_automator (updated)

Code explanation

I willn’t explain the code to the fullest though questions regarding this are welcome via comments. I will explain only the working of the code.

  • It scans a given folder & store the names of particular file extensions in filename.txt
  • dir /B /O:N | findstr ".wmv$ .mpg$ .mpeg$ .avi$" >filename.txt
  • Depending upon names it decides which files are to be encoded with user set x264 profileA or profileB . Every profile corresponds to different for loop
  • It creates a separate .bat file(s) corresponding to every video file. This contains information that will be fed to x264.exe as encode options
  • Finally it construct x264_home.bat that combines all the batch files created in the previous step such that they be executed sequentially
  • Log of every encode is stored in the log.txt file which too is created automatically

Flexibility & Benefits

  • Following lines can be changed for every profile settings see the included x264_commands.txt
    set v=x264 --crf 23  --level 3.1 --tune film -o "d:\Trainers\toconvert\!out!" "d:\Trainers\toconvert\!in!"
  • Also in the above lines encoded files are suffixed with _out.mp4 but can be changed to _out.mkv
  • If you desire a detailed log file (frame per frame detail) change the following line
    set v=x264 --crf 23  --level 3.1 --tune film -o "d:\Trainers\toconvert\!out!" "d:\Trainers\toconvert\!in!"


    set “v=x264 --crf 23  --level 3.1 --tune film -o "d:\Trainers\toconvert\!out!" "d:\Trainers\toconvert\!in!" 2>detail_log.txt”

    Now comprehensive details will be placed in another detail_log.txt file without polluting the summarised log.txt

  • Before you execute x264_home.bat user can edit (by using a simple notepad) contents of each *.bat file created that holds information for encode e.g profileB_file1.bat contents are:

    x264 --crf 20  --level 4.0 --tune animation -o "d:\Trainers\toconvert\Heart _profB_out.mp4" "d:\Trainers\toconvert\Heart _profB.avi"

    One can edit it to add zones, scene cut (see x264 syntax) & what not this surely is very flexible!

  • Finally you can use the same batch file to encode via some different encoder. For instance you can use ffmpeg build or HandbrakeCLI you only have to place the specified build in the same folder as automator & edit required syntax for every profile loop. Change this

    set v=x264 --crf 23 --level 3.1 --tune film -o "d:\Trainers\toconvert\!out!" "d:\Trainers\toconvert\!in!"

    to appropriate HandbrakeCLI syntax, for instance

    set v=HandBrakeCLI -i "d:\Trainers\toconvert\!in!" -o "d:\Trainers\toconvert\!out!" -e x264 -q 20 -B 160

…if you encounter any error do leave me a comment

@echo off
:: user input required
:: folder path for videos to encode
cd /d "d:\Trainers\toconvert\"
setLocal EnableDelayedExpansion
:: file extensions to be included
dir /B /O:N | findstr ".wmv$ .mpg$ .mpeg$ .avi$" >filename.txt
echo. >log.txt
:: user input required
:: each loop correpsonds to unique profile
:: each loop must have separate variable names like out replaced by _out
:: loop parameter named %%a can be same
 for /f "tokens=* delims= " %%a in ('type filename.txt ^|findstr "profA"') do (
 set /a n+=1
 echo. >profA_file!n!.bat
 set in=%in%%%a
 :: user input required
 set out=!in:.wmv=_out.mp4!
 set out=!out:.mpg=_out.mp4!
 set out=!out:.mpeg=_out.mp4!
 set out=!out:.avi=_out.mp4!
 :: user input required
 :: separate folder can be chosen for output files
 :: read x264 syntax to set your encoding profile
 set v=x264 --crf 23  --level 3.1 --tune film -o "d:\Trainers\toconvert\!out!" "d:\Trainers\toconvert\!in!"
 echo. !v!>profA_file!n!.bat 
:: bat files are named such that they arrrange alphabetically
:: this thus sets the priority order for each profile
:: in our case files having profA will be encode prior to profB files 
  for /f "tokens=* delims= " %%a in ('type filename.txt ^|findstr "profB"') do (
  set /a _n+=1
  echo. >profB_file!_n!.bat
  set _in=%_in%%%a
  :: user input required
  set _out=!_in:.wmv=_out.mp4!
  set _out=!_out:.mpg=_out.mp4!
  set _out=!_out:.mpeg=_out.mp4!
  set _out=!_out:.avi=_out.mp4!
  :: user input required
  set _v=x264 --crf 20  --level 4.0 --tune animation -o "d:\Trainers\toconvert\!_out!" "d:\Trainers\toconvert\!_in!"
  echo. !_v!>profB_file!_n!.bat
dir /B /O:N | findstr ".bat$ " >x264_home.txt
for /f "tokens=* delims= " %%a in (x264_home.txt) do (
set /a x+=1
set "z=call %%a | mtee /d/c/t/+ log.txt"
set "d=echo. Operation!x! Complete... | mtee /d/c/t/+ log.txt"
echo. !z! >> x264_home.bat
echo. !d! >> x264_home.bat
echo. @echo off > newFile.bat
type x264_home.bat >> newFile.bat
type newFile.bat > x264_home.bat
del newFile.bat,x264_home.txt,filename.txt
:: user input required
:: folder path from video folder to automator folder
move "d:\Trainers\toconvert\*.bat" "d:\Program Files\x264_automator\"
move "d:\Trainers\toconvert\log.txt" "d:\Program Files\x264_automator\"

Gallery below demands high internet speed to function properly, if you encounter any error refresh your browser.

You can use following features (bottom left corner):

  • Toggle information about the respective image
  • View Gallery in Full Screen


  1. Skype - yourarnav I need to hire you for x264 handbrake automation, Hit me up!