如何在Azure DevOps任务中后台运行FFmpeg录屏命令?
Solution: Run FFmpeg in the Background for Azure DevOps Pipeline
Great question! Running FFmpeg in the background is totally doable to let your Azure DevOps pipeline tasks continue while recording the desktop. Let’s break down the steps based on the agent OS you’re using (since your command uses .\ffmpeg, I’ll focus on Windows first, which is the most likely scenario here):
1. Start FFmpeg as a Background Process
You need to launch FFmpeg without blocking the pipeline step. Here are the best ways for common Azure DevOps task types:
PowerShell Task (Recommended)
Use Start-Process to run FFmpeg in the background, and save its process ID (PID) to a file so you can stop it later:
# Start FFmpeg in background and save PID to a file $ffmpegProcess = Start-Process -FilePath ".\ffmpeg" -ArgumentList "-f gdigrab -framerate 10 -i desktop vid.avi" -NoNewWindow -PassThru $ffmpegProcess.Id | Out-File -FilePath "$(Agent.TempDirectory)\ffmpeg_pid.txt"
-NoNewWindow: Prevents a separate command window from opening (critical for pipeline agents).-PassThru: Returns the process object so we can capture its PID.$(Agent.TempDirectory): Uses Azure DevOps' built-in temp path to store the PID file safely.
Cmd Task
If you prefer using a Cmd task, use the /B flag to run FFmpeg in the background:
start /B .\ffmpeg -f gdigrab -framerate 10 -i desktop vid.avi
Note: Stopping the process later is trickier with Cmd, so PowerShell is the better option for full control.
2. Stop FFmpeg at the End of Your Pipeline
To ensure the recording stops when your pipeline finishes (and avoid leaving FFmpeg running on the agent), add a final step to terminate the process:
PowerShell Stop Step
# Read the saved PID and stop the FFmpeg process $pid = Get-Content -Path "$(Agent.TempDirectory)\ffmpeg_pid.txt" if ($pid) { Stop-Process -Id $pid -Force Write-Host "Successfully stopped FFmpeg process with ID $pid" }
-Force: Ensures the process is terminated even if it's unresponsive, preventing corrupted video files.
Key Notes for Azure DevOps Pipelines
- Agent Permissions: If using a self-hosted Windows agent, make sure the agent service runs under an account with desktop interaction privileges (gdigrab requires access to the desktop session).
- FFmpeg Availability: Ensure FFmpeg is installed on the agent, or add a prior step to download it (e.g., using
Invoke-WebRequestin PowerShell to fetch the latest build). - Video File Storage: The
vid.avifile will be saved in the pipeline's working directory. You can add a step to upload it to Azure Artifacts or blob storage if you need to retain it.
内容的提问来源于stack exchange,提问作者Maciejjy

