我一直只使用标准foreach
循环,它按预期执行。但是,我有一个 8 核处理器和 16GB RAM,我想用循环来利用它Parallel.foreach
。好吧,这个语法按预期工作
$filelist = Get-ChildItem G:\GoodFilesForServer\ -filter *.mkv
$num = $filelist | measure
$filecount = $num.count
$i = 0;
ForEach ($file in $filelist)
{
$Workflow:i++;
$oldfile = $file.DirectoryName + "\" + $file.BaseName + $file.Extension;
$newfile = $file.DirectoryName + "\" + $file.BaseName + ".mp4";
$progress = ($Workflow:i / $filecount) * 100
$progress = [Math]::Round($progress,2)
Clear-Host
Write-Host -------------------------------------------------------------------------------
Write-Host Handbrake Batch Encoding
Write-Host "Processing - $oldfile"
Write-Host "File $Workflow:i of $filecount - $progress%"
Write-Host -------------------------------------------------------------------------------
Start-Process "C:\Program Files\HandBrake\HandBrakeCLI.exe" -ArgumentList "-i `"$oldfile`" -t 1 --angle 1 -c 1 -o `"$newfile`" -f mp4 -O --decomb --modulus 16 -e x264 -q 32 --vfr -a 1 -E lame -6 dpl2 -R Auto -B 48 -D 0 --gain 0 --audio-fallback ffac3 --x264-preset=veryslow --x264-profile=high --x264-tune=`"animation`" --h264-level=`"4.1`" --verbose=0" -Wait -NoNewWindow
del $oldfile
}
很简单,但这并不是说Parallel
这种语法不起作用,并且没有显示任何错误来帮助我确定为什么它不起作用。
Workflow ParallelTest
{
$filelist = Get-ChildItem G:\GoodFilesForServer\ -filter *.mkv
$num = $filelist | measure
$filecount = $num.count
$i = 0;
ForEach -Parallel -ThrottleLimit 20 ($file in $filelist)
{
$Workflow:i++;
$oldfile = $file.DirectoryName + "\" + $file.BaseName + $file.Extension;
$newfile = $file.DirectoryName + "\" + $file.BaseName + ".mp4";
$progress = ($Workflow:i / $filecount) * 100
$progress = [Math]::Round($progress,2)
Clear-Host
Write-Host -------------------------------------------------------------------------------
Write-Host Handbrake Batch Encoding
Write-Host "Processing - $oldfile"
Write-Host "File $Workflow:i of $filecount - $progress%"
Write-Host -------------------------------------------------------------------------------
Start-Process "C:\Program Files\HandBrake\HandBrakeCLI.exe" -ArgumentList "-i `"$oldfile`" -t 1 --angle 1 -c 1 -o `"$newfile`" -f mp4 -O --decomb --modulus 16 -e x264 -q 32 --vfr -a 1 -E lame -6 dpl2 -R Auto -B 48 -D 0 --gain 0 --audio-fallback ffac3 --x264-preset=veryslow --x264-profile=high --x264-tune=`"animation`" --h264-level=`"4.1`" --verbose=0" -Wait -NoNewWindow
del $oldfile
}
}
Workflow
为了能够运行转换,我应该在语法中改变什么Parallel
(顺便说一句,如果ThrottleLimit
20 的尝试太疯狂,我们可以减少它)