问题
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.
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/3822650/how-can-i-output-handbrake-output-to-both-the-screen-and-to-a-file