This code will record the video which is grabbed from an USB webcam and save it to a file. In parallel, it will output a preview to your screen by piping ffmpeg’s output into ffplay. The code is tested on a Raspberry Pi 5, the webcam in question is a Logitec c920. The output to screen has some noticeable lag and low resolution but it’s enough to help as a control.
ffmpeg -y -f v4l2 -input_format mjpeg -video_size 1280x720 -framerate 30 -i /dev/video0 -vcodec libx264 -an output.mp4 -f nut - | ffplay -i -