Matt McManis
Matt McManis

Reputation: 4675

Launch process with priority

I'm running FFmpeg in Command Prompt with low priority.

start /low /b ffmpeg -i "C:\video.mpg" -c:v libx264 -crf 25 "C:\video.mp4"

How can I set FFmpeg's priority in PowerShell for low, below normal, normal, above normal, high?

Using the CMD command above in PowerShell, I get the errors:

Parameter cannot be processed because the parameter name 'i' is ambiguous.
Start-Process : A positional parameter cannot be found that accepts argument 'ffmpeg'.

Testing Solutions

I'm trying to use the first example in ArcSet's answer.

Start-Process ffmpeg -NoNewWindow -Wait -ArgumentList '-i "C:\video.mpg" -c:v libx264 -crf 25 "C:\video.mp4"' -PassThru Set-ProcessPriority -ProcessId $Process.id -Priority AboveNormal

I'm getting the error:

A positional parameter cannot be found that accepts argument 'Set-ProcessPriority'

Upvotes: 2

Views: 6095

Answers (1)

ArcSet
ArcSet

Reputation: 6860

Using ether Set-ProcessPriority (function at bottom)

$Process = Start-Process "C:\ffmpeg\bin\ffmpeg.exe" -ArgumentList "-i `"C:\ffmpeg\Test\Test.mpg`" -c:v libx264 -crf 25 `"C:\ffmpeg\Test\Test.mp4`"" -PassThru
Set-ProcessPriority -ProcessId $Process.id -Priority BelowNormal

Or setting it in the properties of a process

$Process = Start-Process "C:\ffmpeg\bin\ffmpeg.exe" -ArgumentList "-i `"C:\ffmpeg\Test\Test.mpg`" -c:v libx264 -crf 25 `"C:\ffmpeg\Test\Test.mp4`"" -NoNewWindow -PassThru
$Process.PriorityClass = [System.Diagnostics.ProcessPriorityClass]::BelowNormal

If you need to verify its set to the Priority you requested you can call the variable you stored the process in and check

$Process.PriorityClass

As a side note you will see the char ` used. This is a escape char and is used so you can use quotes inside quotes

"Hey `"There`" People"

that would return

Hey "There" People
  • Making a edit because years ago I wrote a module that had this command in it and forgot to include it....heck i forgot it was a module command I created

Function

Function Set-ProcessPriority {
    Param(
        [Parameter(ParameterSetName='PID')]
        [int]$ProcessId,
        [Parameter(ParameterSetName='Process', ValueFromPipeline)]
        [System.Diagnostics.Process[]]$Process,
        [Parameter(Mandatory)]
        [System.Diagnostics.ProcessPriorityClass]$Priority,
        [Switch]$PassThru
    )

    If($PSCmdlet.ParameterSetName -eq 'PID'){
        Write-Verbose "Getting Process for $ProcessId"
        $Process = Get-Process -Id $ProcessId
    }

    $Process | %{
        Write-Verbose "Setting PriorityClass for $($_.Name) ($($_.Id)) to $Priority"
        $_.PriorityClass = $Priority
    }

    If($PassThru){
        $Process
    }
}

Usage

Get-Process SnippingTool | Select ID, Name, PriorityClass  

   Id Name         PriorityClass
   -- ----         -------------
16856 SnippingTool        Normal

Get-Process SnippingTool | 
    Set-ProcessPriority -Priority AboveNormal -Verbose -PassThru | 
    Select ID, Name, PriorityClass

VERBOSE: Setting PriorityClass for SnippingTool (16856) to AboveNormal

   Id Name         PriorityClass
   -- ----         -------------
16856 SnippingTool   AboveNormal

Upvotes: 5

Related Questions