How can i crop part of a video while recording from desktop in real time using ffmpeg?
Two ways:

#1 By specifying gdigrab options:

ffmpeg -f gdigrab -framerate 24 -video_size 960x540 -offset_x 240 -offset_y 450 -i desktop -preset ultrafast -pix_fmt yuv420p out.mp4


This captures a 960x540 video whose top-left corner is at (240,450) measured from the top-left corner of the display.

#2 By cropping it post-capture

ffmpeg -f gdigrab -framerate 24 -i desktop -vf crop=960:540:240:450 -preset ultrafast -pix_fmt yuv420p out.mp4


The former is preferable.