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:


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:

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