在powershell中限制Start-Process运行的数量

时间:2018-03-04 06:46:19

标签: powershell ffmpeg powershell-v2.0 start-job start-process

我试图限制从Powershell运行的Start-Process的数量,但我似乎无法让它发挥作用。

我尝试按照此流程执行:https://exchange12rocks.org/2015/05/24/how-to-limit-a-number-of-powershell-jobs-running-simultaneously/Run N parallel jobs in powershell

但是这些是针对Jobs not Processes,我想从-Wait中删除Start-Process

我对脚本的担心是,如果文件夹中有1000个音频文件,那么FFMpeg会使系统崩溃。

# get the folder for conversion
function mbAudioConvert {
    [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
    [System.Windows.Forms.Application]::EnableVisualStyles()

    $fileBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
    $fileBrowser.SelectedPath = "B:\"
    $fileBrowser.ShowNewFolderButton = $false
    $fileBrowser.Description = "Select the folder with the audio which you wish to convert to Avid DNxHD 120 25P 48kHz"

    $mbLoop     = $true
    $mbCount    = 0001
    $mbMaxJob   = 4

    while( $mbLoop ) {
        if( $fileBrowser.ShowDialog() -eq "OK" ) {
            $mbLoop     = $false


            $mbImage    = ( Get-Item -Path "C:\Users\user\Desktop\lib\AudioOnly.jpg" )
            $mbff32     = ( Get-Item -Path "C:\Users\user\Desktop\lib\ffmpeg32.exe" )
            $mbff64     = ( Get-Item -Path "C:\Users\user\Desktop\lib\ffmpeg64.exe" )

            $mbFolder   = $fileBrowser.SelectedPath
            $mbItemInc  = ( ls $mbFolder\* -Include *.mp3, *.MP3, *.wav*, *.WAV*, *.ogg, *.OGG, *.wma, *.WMA, *.flac, *.FLAC, *.m4a, *.M4a )
            $mbProgress = ( Get-ChildItem -Path $mbItemInc )

            $mbHasRaw   = ( $mbFolder + "\RAW" )

            if( !( Test-Path -Path $mbHasRaw ) ) {
                # force create a RAW folder if it does not exist
                New-Item -ItemType Directory -Force -Path "$mbHasRaw"
            }


            foreach( $mbItem in $mbItemInc ) {

                $mbCheck    = $false

                # output the progress
                # Suggestion: You might want to consider updating this after starting the job and do the final update after running ex. Get-Job | Wait-Job to make the progress-bar stay until all processes are finished
                #Write-Progress -Activity "Counting files for conversion" -status "Currently processing: $mbCount" -percentComplete ($mbCount / $mbItemInc.count*100)

                # limit the run number
                while ($mbCheck -eq $false) {

                    if( (Get-Job -State 'Running').count -lt $mbMaxJob) {

                        $mbScriptBlock = {
                            $mbItemName = $using:mbItem.BaseName

                            $mbNewItem  = ( $using:mbFolder + "\RAW\" + $mbItemName + ".mov" )
                            $mbArgs     = " -loop 1 -i $using:mbImage -i $using:mbItem -shortest -c:v dnxhd -b:v 120M -s 1920x1080 -pix_fmt yuv422p -r 25 -c:a pcm_s16le -ar 48k -af loudnorm=I=-12 $mbNewItem"

                            Start-Process -FilePath $using:mbff32 -ArgumentList $mbArgs -NoNewWindow -Wait
                        }

                        Start-Job -ScriptBlock $mbScriptBlock

                        #The job-thread doesn't know about $mbCount, better to increment it after starting the job
                        $mbCount++
                        $mbCheck  = $true          
                    }

                }
            }

        } else {

            $mbResponse = [System.Windows.Forms.MessageBox]::Show("You have exited out of the automation process!", "User has cancelled")
            if( $mbResponse -eq "OK" ) {
                return
            }
        }
    }

    $fileBrowser.SelectedPath
    $fileBrowser.Dispose()
}

# call to function
mbAudioConvert

1 个答案:

答案 0 :(得分:0)

  1. 您可以修改$mbCheck,但是while循环正在测试$Check,这意味着当$Check -eq $false$false时,{@ 1}} {while}循环永远不会执行$Check未定义
  2. 在作业脚本块外部创建的变量需要作为参数传递,或者您需要使用using:变量作用域传递它们(PowerShell 3.0或更高版本)。已将其添加到示例中未定义的$mbItem$mbff32$mbImage$mbFolder
  3. $mbMaxJob未定义。正在运行的作业检查将永远不会成立,并且不会启动任何流程
  4. $mbCount未定义。进度条不起作用
  5. echo "$mbCount. $mbNewItem"除非您在某个时刻使用Receive-Job来获取作业的输出,否则不会返回任何内容
  6. 尝试:

    #DemoValues
    $mbItemInc = 1..10 | % { New-Item -ItemType File -Name "File$_.txt" }
    $mbff32 = "something32"
    $mbFolder = "c:\FooFolder"
    $mbImage = "BarImage"
    $mbMaxJob = 2
    $mbCount = 0
    
    foreach( $mbItem in $mbItemInc ) {
    
        $mbCheck    = $false
    
        # output the progress
        # Suggestion: You might want to consider updating this after starting the job and do the final update after running ex. Get-Job | Wait-Job to make the progress-bar stay until all processes are finished
        Write-Progress -Activity "Counting files for conversion" -status "Currently processing: $mbCount" -percentComplete ($mbCount / $mbItemInc.count*100)
    
        # limit the run number
        while ($mbCheck -eq $false) {
    
            if ((Get-Job -State 'Running').count -lt $mbMaxJob) {
    
                $mbScriptBlock = {
    
                     Param($mbItem, $mbFolder, $mbImage, $mbff32)
                    #Filename without extension is already available in a FileInfo-object using the BaseName-property
                    $mbItemName = $mbItem.BaseName
    
                    $mbNewItem  = ( $mbFolder + "\RAW\" + $mbItemName + ".mov" )
                    $mbArgs     = "-loop 1 -i $mbImage -i $mbItem -shortest -c:v dnxhd -b:v 120M -s 1920x1080 -pix_fmt yuv422p -r 25 -c:a pcm_s16le -ar 48k -af loudnorm=I=-12 $mbNewItem"
    
                    Start-Process -FilePath $mbff32 -ArgumentList $mbArgs -NoNewWindow -Wait
                }
    
                Start-Job -ScriptBlock $mbScriptBlock -ArgumentList $mbItem, $mbFolder, $mbImage, $mbff32
    
                #The job-thread doesn't know about $mbCount, better to increment it after starting the job
                $mbCount++
                $mbCheck  = $true          
            }
    
        }
    }