pwsh里用ffmpeg录制屏幕
有的时候不想开obs这种大软件录制视频,就用这种ffmpeg的快捷方式,录制出来效果也还不错。
全屏录制
ffmpeg录制需要提供屏幕的大小,可以通过Get-CimInstance -Namespace root\cimv2 -ClassName Win32VideoController 获取屏幕对象$Screen,然后获取x y的值,最终保存为ScreenRecording_$Timestamp.mp4文件。
Set-Alias recorder-screen Start-ScreenRecorder function Start-ScreenRecorder { param( [int]$ScreenIndex = 0, [string]$OutputDirectory = ".\", [string]$Name ) $Timestamp = Get-Date -Format "yyyyMMdd_HHmmss" $OutputFileName = "ScreenRecording_$Timestamp.mp4" if ($Name) { $OutputFileName = $Name } $OutputFilePath = Join-Path -Path $OutputDirectory -ChildPath $OutputFileName $Screen = Get-CimInstance -Namespace root\cimv2 -ClassName Win32_VideoController | Select-Object -Index $ScreenIndex $ScreenWidth = $Screen.CurrentHorizontalResolution $ScreenHeight = $Screen.CurrentVerticalResolution ffmpeg -f gdigrab -framerate 30 -video_size $ScreenWidth"x"$ScreenHeight -i desktop $OutputFilePath }
半屏幕录制
同理,录制半屏也是通过获取屏幕的x y,然后通过ffmpeg 的 -filter参数选出自己需要的部分,下面就是录制右半边屏幕的参数。
-filter:v "crop=in_w/2:in_h:in_w/2:0"
Set-Alias recorder-screen-right-half Start-ScreenRecorder-Right-Half function Start-ScreenRecorder-Right-Half { param( [int]$ScreenIndex = 0, [string]$OutputDirectory = ".\" ) $Timestamp = Get-Date -Format "yyyyMMdd_HHmmss" $OutputFileName = "ScreenRecording_$Timestamp.mp4" $OutputFilePath = Join-Path -Path $OutputDirectory -ChildPath $OutputFileName $Screen = Get-CimInstance -Namespace root\cimv2 -ClassName Win32_VideoController | Select-Object -Index $ScreenIndex $ScreenWidth = $Screen.CurrentHorizontalResolution $ScreenHeight = $Screen.CurrentVerticalResolution ffmpeg -f gdigrab -framerate 30 -video_size $ScreenWidth"x"$ScreenHeight -i desktop -filter:v "crop=in_w/2:in_h:in_w/2:0" $OutputFilePath }