0

With the help of Google, I put together a Powershell script for Handbrake automation.

What it does:

  1. Some files are downloaded automatically via RSS. They are placed in the source folder.
  2. The Powershell script executes Handbrake, encoding starts and successfully gets over.
  3. If no new files arrive in the source folder, the script exits.

The issue is with the last item. When the source folder is empty the Powershell script exits, but I want it to continue running and process more files when they arrive, until I kill it. When a new file is added, it should automatically start encoding.

Code is in PasteBin which has more comments but should be easy to infer what the script does:

$inputpath = "I:\S"
$outputpath = "I:\E"

$movies = ls $inputpath

foreach($movie in $movies){
    $name = $movie.basename

    if(!(test-path -path "$outputpath\$name.mkv")){
        C:\"Program Files"\handbrake\HandBrakeCLI.exe -i "$inputpath\$movie" -o "$outputpath\$name.mkv" `
        -e x264 -b 1000 -2 -T -a 1,1 -E mp3 -B 112 --mixdown stereo -f mkv --detelecine --decomb `
        --loose-anamorphic -m -x rc-lookahead=30:ref=4:bframes=3:me=umh:subme=9:analyse=none:deblock=1:0:0:8x8dct=1
    }
}
Palec
  • 12,743
  • 8
  • 69
  • 138
Arnav Attri
  • 51
  • 1
  • 4
  • So are you just looking for logic that accounts for when a folder is blank? Might work better if you show us the code you are having trouble with. – Matt Dec 23 '14 at 00:49
  • I see your code but don't understand your issue. If the source folder is empty there is nothing to process so the script is working correctly. What do you want to happen? – Matt Dec 23 '14 at 14:00
  • Yes, the thing is, if source folder is empty it exits! I don't want it to exit as in the meantime source files being downloaded from private tracker! When the new file is added, it should automatically start encoding, but as we know, it exits after the source folder gets empty! I just want it to stay working even if source folder get empty and then start processing as soon as the NEW file is received in source folder. – Arnav Attri Dec 23 '14 at 14:48

1 Answers1

1

In your comments you are describing System.IO.FileSystemWatcher and Register-ObjectEvent to be used as a file watcher. Never played with it much until now but here is a sample of what you are looking for.

$inputpath = "I:\S" 
$outputpath = "I:\E"

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $searchPath
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true

$created = Register-ObjectEvent $watcher "Created" -Action {
    $name = $eventArgs.basename

    if(!(test-path -path "$outputpath\$name.mkv")){
        C:\"Program Files"\handbrake\HandBrakeCLI.exe -i "$($eventArgs.FullName)" -o "$outputpath\$name.mkv" `
        -e x264 -b 1000 -2 -T -a 1,1 -E mp3 -B 112 --mixdown stereo -f mkv --detelecine --decomb `
        --loose-anamorphic -m -x rc-lookahead=30:ref=4:bframes=3:me=umh:subme=9:analyse=none:deblock=1:0:0:8x8dct=1
    }    
}

Based off a forum post here. Searching for System.IO.FileSystemWatcher and Register-ObjectEvent might give more context for you. Also you might need to check the handbrake code as it looked wrong in your pastebin and I attempted to improve it for readability.

Matt
  • 45,022
  • 8
  • 78
  • 119