Capturing a webcam in FFmpeg is the first step in any streaming setup. The command depends on the platform: on Linux, the Video4Linux2 API; on macOS, the AVFoundation framework; on Windows, the DirectShow API. Each one has a different device name format, but the principle is the same.

The command

ffmpeg -f v4l2 -framerate 30 -video_size 1280x720 -i /dev/video0 -c:v libx264 -crf 20 out.mp4

What it does

The command opens the first V4L2 device (`/dev/video0`) at 30 fps and 720p, decodes the raw frames, and encodes them with libx264. The output is a normal MP4 file. For a live stream, replace the output with an RTMP URL. For macOS, the command is `ffmpeg -f avfoundation -framerate 30 -video_size 1280x720 -i "0:0" ...` (the first `0` is the video device index, the second is the audio device).

Requirements

A webcam or capture card that the OS recognizes. The platform-specific input format (`v4l2`, `avfoundation`, `dshow`). No special build, no hardware acceleration needed for the capture, though NVENC or VideoToolbox can speed up the encode.

Things to avoid

  • Using a device name that does not exist. On Linux, run `v4l2-ctl --list-devices` to see the available cameras; on macOS, run `ffmpeg -f avfoundation -list_devices true -i ""`.
  • Capturing at a resolution the camera does not support. A 4K webcam at 30 fps may not produce a 4K signal; check the datasheet for the supported modes.
  • Re-encoding the audio multiple times. If the source has audio, capture it in the same command with `-i` for a second device, and use `-c:a copy` to keep the audio untouched.
  • Forgetting the `-framerate` flag. The default is 25 fps, which may not match the camera's native rate, and the resulting video may have dropped or duplicated frames.