Capture USB Webcam Video with FFmpeg on Linux

This guide provides a straightforward, step-by-step walkthrough for capturing video from a USB webcam on a Linux system using FFmpeg. You will learn how to identify your webcam device, check its supported video formats and resolutions, and execute the exact FFmpeg commands needed for both raw recording and compressed streaming.


Identify Your Webcam Device

Before running FFmpeg, you need to find the device node assigned to your USB webcam. Linux maps these devices to the /dev directory.

You can list all available video devices by running the following command in your terminal:

ls /dev/video*

Typically, your primary webcam will appear as /dev/video0. If your laptop has an integrated webcam and you plugged in an external USB webcam, the new camera will likely be /dev/video2 or higher.

To verify which device corresponds to your USB webcam, you can use the v4l-utils package tool:

v4l2-ctl --list-devices

Check Supported Formats and Resolutions

Webcams support specific pixel formats (like YUYV, MJPEG, or H.264) and resolutions. Forcing FFmpeg to capture at an unsupported resolution will result in an error.

Query your device's capabilities with this command:

v4l2-ctl --device=/dev/video0 --list-formats-ext

Look through the output to note the available formats (e.g., mjpeg or yuyv422) and the exact resolutions (e.g., 1920x1080, 1280x720) your camera supports.


Basic FFmpeg Video Capture Command

Once you know your device path and a supported resolution, you can record a basic video. Linux utilizes the video4linux2 (or v4l2) input device demuxer to handle webcam streams.

Run the following command to record video without audio:

ffmpeg -f v4l2 -video_size 1280x720 -i /dev/video0 output.mp4

Breakdown of the Command:

  • -f v4l2: Forces FFmpeg to use the Video4Linux2 input format.
  • -video_size 1280x720: Sets the frame resolution (match this to your camera's capabilities).
  • -i /dev/video0: Specifies the input source path.
  • output.mp4: The name and format of the resulting video file.

Advanced Capture: Adding Audio and Encoding

For a production-ready recording, you will likely want to capture audio simultaneously and compress the video using a widely compatible codec like H.264.

Linux systems usually use ALSA or PulseAudio/PipeWire for sound management. You can capture audio from your microphone using the alsa or pulse grabbers.

Use this optimized command to record both synchronized audio and high-quality encoded video:

ffmpeg -f v4l2 -framerate 30 -video_size 1920x1080 -i /dev/video0 -f pulse -i default -c:v libx264 -preset ultrafast -c:a aac recording.mp4

Breakdown of Advanced Flags:

  • -framerate 30: Tells FFmpeg to capture at 30 frames per second.
  • -f pulse -i default: Grabs audio from your system's default PulseAudio/PipeWire microphone input.
  • -c:v libx264: Encodes the video stream into H.264 format.
  • -preset ultrafast: Lowers CPU usage during live recording to prevent dropped frames.
  • -c:a aac: Encodes the audio stream into AAC format.

To stop recording at any time, press Ctrl + C in your terminal window, and FFmpeg will safely close and save the file.