Skip to content

Instantly share code, notes, and snippets.

@Kimau
Created September 13, 2025 13:07
Show Gist options
  • Save Kimau/e5ff62abf010d93d7481a723f1145f3f to your computer and use it in GitHub Desktop.
Save Kimau/e5ff62abf010d93d7481a723f1145f3f to your computer and use it in GitHub Desktop.
[Win10][ffmpeg][ps] Silly little Screencap using FFMPEG and powershell to get coords
# Script to capture a screen region using mouse clicks to define the area.
# To stop the recording, return to this PowerShell window and press 'q'.
# Add the necessary assembly to get cursor position.
Add-Type -AssemblyName System.Windows.Forms
# --- Step 1: Capture the coordinates of the top-left corner (Point A) ---
Write-Host "Please position your mouse cursor at the TOP-LEFT corner of the region you want to capture."
Write-Host "Then press any key to record the coordinates..."
# Wait for a key press to capture the coordinates
$null = [System.Console]::ReadKey($true)
$pointA = [System.Windows.Forms.Cursor]::Position
Write-Host "Top-left coordinates captured: X=$($pointA.X), Y=$($pointA.Y)"
# --- Step 2: Capture the coordinates of the bottom-right corner (Point B) ---
Write-Host "Now, position your mouse cursor at the BOTTOM-RIGHT corner of the region you want to capture."
Write-Host "Then press any key to record the coordinates..."
# Wait for a key press to capture the coordinates
$null = [System.Console]::ReadKey($true)
$pointB = [System.Windows.Forms.Cursor]::Position
Write-Host "Bottom-right coordinates captured: X=$($pointB.X), Y=$($pointB.Y)"
# --- Step 3: Calculate the video size and offsets ---
# Ensure the coordinates are in the correct order for calculation (top-left vs bottom-right).
$offset_x = [Math]::Min($pointA.X, $pointB.X)
$offset_y = [Math]::Min($pointA.Y, $pointB.Y)
# Calculate width and height. Use absolute values to handle clicks in any order.
$width = [Math]::Abs($pointA.X - $pointB.X)
$height = [Math]::Abs($pointA.Y - $pointB.Y)
# Check for valid dimensions to prevent ffmpeg errors
if ($width -le 0 -or $height -le 0) {
Write-Host "`nError: Invalid region selected. Please ensure your two clicks define a valid area with a width and height greater than zero."
Write-Host "Press any key to exit the script."
$null = [System.Console]::ReadKey($true)
exit
}
$video_size = "$($width)x$($height)"
Write-Host "`nCalculated Capture Region:`nOffset X: $offset_x`nOffset Y: $offset_y`nVideo Size: $video_size"
# --- Step 4: Construct and run the ffmpeg command ---
Write-Host "`nStarting ffmpeg recording..."
Write-Host "To stop the recording, go to this PowerShell window and press 'q'."
# Define the output file name with a timestamp to avoid overwrites
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$output_file = "screen_capture_$timestamp.mkv"
# Construct the full command arguments as an array to ensure they are passed correctly.
$ffmpeg_arguments = @(
"-f", "gdigrab",
"-framerate", "30",
"-offset_x", "$offset_x",
"-offset_y", "$offset_y",
"-video_size", "$video_size",
"-i", "desktop",
"`"$output_file`""
)
# Use the splatting operator (@) to pass the arguments correctly.
# This will run in the foreground and block the script until you press 'q' to stop the recording.
ffmpeg.exe @ffmpeg_arguments
Write-Host "`nRecording session finished. File saved to: $output_file`n"
Write-Host "Press any key to exit the script."
$null = [System.Console]::ReadKey($true)
@Kimau
Copy link
Author

Kimau commented Sep 13, 2025

This is a dumb little script for ease of use because I hated the bloated tools.

  1. Assumes you have ffmpeg in your path
  2. Setup shortcut with
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "C:\Users\evilk\Downloads\screencap.ps1"
    Set working director to downloads

Close the window when your done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment