views:

41

answers:

2

So I've been using Handbrake command line to encode my video collection to store on my NAS so I can use it on my HTPC. I was looking for a way to output both to the screen so I can watch it's output as it's encoding, but also to a file so I can go back and look at a particular encoding session.

My solution for this was to use one Powershell window to run the encoding and output to a file, then another Powershell window to read the log file and display it on screen. This works, but I want to improve it, as it's not perfect. Because the read file script reads at a set interval, it misses lines. Also if I reduce the interval, it has an effect on system performance, making the encoding run a bit slower. Is there a way I can redirect the output of the first window to both a file and to the screen?

The first powershell script (the one that starts the encoding) called "Convert1.ps1" (run from the handbrake install directory):

net time \\ODIN |find "Current time"
./HandbrakeCLI.exe -i "<input file>" -o "<output file>" <handbrake parameters>

The second powershell script to output to a file, called "Start_Convert.ps1":

d:\Conversions\Convert.ps1 2>&1 | out-file d:\Conversions\Completed\Movies\9.29.2010.log

The third powershell script to read from that log file, called "Watch_Output.ps1":

while (1)
{
(Get-Content d:\Conversions\Completed\Movies\9.29.2010.log)[-1]
Start-sleep 5
}

I'd like, ideally, to get this all down to one powershell window running a single script to start the encoding, output to a file, and display it on screen.

Edit (Adding Solution): 2 different ways to do it, I'm going with the latter since it is simpler.

Way #1 - Start-Job Resulting script to start my conversions:

Start-Job -Name VideoConvert -ScriptBlock { d:\Conversions\Convert.ps1 2>&1 | out-file d:\Conversions\Movies\Movie.log }
Get-FileTail -Wait Encoding Unicode -Path D:\Conversions\Completed\Movies\Movie.log

Way #2 - Tee-Object Resulting script to start my conversions:

d:\Conversions\Convert.ps1 2>&1 |Tee-Object -File D:\Conversions\Completed\Movies\Movie.log

Thanks again all. This works just like I wanted it to work.

A: 

What you really want to do is to grab the tail.exe binary from the UnxUtils package and run it as follows:

tail -f D:\Conversions\Completed\Movies\9.29.1010.log

Unfortunately you will need to do that in a second command prompt (or PowerShell) window, but it will "follow" that file and display the lines from the log as they are added.

If you really want to keep that in a single window, you will need PowerShell v2 and you can start the encoding as a job (allowing it to run in the background) and then follow that command with a call to tail for displaying the results:

Start-Job -ScriptBlock { ./HandbrakeCLI.exe -i "in" -o "out" "params" > log.txt }
tail -f log.txt
Goyuix
Ah, that makes sense. Thanks, I'll give it a try. I'll be back if I can get it to work correctly.
Thor79
Ok, only one problem, the output that tail produces is double spaced.
Thor79
What about the tail function in Powershell Community Extensions - http://pscx.codeplex.com/?
fenster
Ah, thank you fenster, that worked. Had to specify the encoding as Unicode to get it to work, but it works perfectly.
Thor79
+1  A: 

I would use Tee-Object for this:

./HandbrakeCLI.exe -i infile -o outfile ... 2>&1 | Tee-Object -File movie.log

Added in the 2>&1 to capture errors to the log as well as the screen.

Keith Hill
I tried that initially, but the problem with that is, I am doing these commands in batches, and in those batches I do stuff like record the date and time the covnersion started and stopped as well as create directories and other various file tasks. I want to capture everything from the entire batch of conversions (usually like 10-20 video conversions at a time) so that I can diagnose any problems that occured if I get errors somewhere along the line. So I need a single log file for an entire batch and Goyuix's solution worked though I used fenster's suggestion for a tail function.
Thor79
Actually wait...that does work better...though I changed it to work on the convert.ps1 file instead.
Thor79
Ahhh yes... Tee-Object. Great suggestion!
Goyuix