Powershell script, executing commands to lots of files

Following powershell script goes recursively the folder ”J:\pictures” and converts all avi file to mp4 files. It uses ffmpeg (remember to download newest binary)

I couldn’t get invoke-expression to work (it gave some error about ”-” operator), so I just sticked all the commands to a text file d:\commands.txt and run it as a normal batch script.

I used this script to convert lots of videos shot with my camera to more compatible format.

"" > d:\commands.txt
foreach ($file in get-childitem "j:\pictures" -recurse -include "*.avi"){
$ffmpegprog = "`"C:\Program Files (x86)\WinFF\ffmpeg.exe`"";
$ffmpegparams = " -threads 2 -f mp4 -vcodec libx264 -b 1500k -flags +loop -cmp +chroma -deblockalpha 0 -deblockbeta 0 -b 1500k -maxrate 2000k -bufsize 4M -bt 256k -refs 1 -bf 3 -coder 1 -me_method umh -me_range 16 -subq 7 -partitions +parti4x4+parti8x8+partp8x8+partb8x8 -g 250 -keyint_min 25 -level 30 -qmin 10 -qmax 51 -qcomp 0.6 -trellis 2 -sc_threshold 40 -i_qfactor 0.71 -acodec aac -ab 256k -ac 2 -ar 44100";
$command = ($ffmpegprog + " -i `"" + $file.FullName + "`"" + $ffmpegparams + " `"" + [System.IO.Path]::ChangeExtension($file.FullName, ".mp4") + "`"");
$("$command") >> D:\commands.txt

You can just copy-paste the file to a powershell window.

For some reason direct execution of the commands.txt batch file didn’t work so you can just run it throught type

type d:\commands.txt > d:\commands.bat


One thought on “Powershell script, executing commands to lots of files

  1. ssalonen sanoo:

    I’ve found that power shell is way too complex for me… I’m better using boo command line utilities and scripts. They provide python like language (you learn it very fast!) for .net framework.


Täytä tietosi alle tai klikkaa kuvaketta kirjautuaksesi sisään:


Olet kommentoimassa WordPress.com -tilin nimissä. Log Out /  Muuta )

Google photo

Olet kommentoimassa Google -tilin nimissä. Log Out /  Muuta )


Olet kommentoimassa Twitter -tilin nimissä. Log Out /  Muuta )


Olet kommentoimassa Facebook -tilin nimissä. Log Out /  Muuta )

Muodostetaan yhteyttä palveluun %s