Skip to content
Linux

FFmpeg Webcam Capture on Linux: Images and Video

Capture a webcam image or record video with FFmpeg on Linux using V4L2, then compare practical fswebcam, OpenCV, and GStreamer alternatives.

Published Updated

FFmpeg can capture a photo or record webcam video directly from a Linux V4L2 device such as /dev/video0. The quickest commands are:

# Capture one frame
ffmpeg -f v4l2 -video_size 1280x720 -i /dev/video0 -frames:v 1 photo.jpg

# Record 10 seconds of H.264 video
ffmpeg -f v4l2 -video_size 1280x720 -framerate 30 -i /dev/video0 \
  -t 10 -c:v libx264 output.mp4

This guide explains those FFmpeg options and also covers fswebcam, OpenCV, and GStreamer alternatives.

Prerequisites

Before diving into the examples, ensure you have the necessary tools installed on your Linux system. You can install them using your distribution’s package manager. For Ubuntu/Debian, you can use:

sudo apt update
sudo apt install fswebcam ffmpeg libopencv-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-tools v4l-utils

1. Using fswebcam

fswebcam is a simple command-line tool for capturing images from a webcam.

Installing fswebcam

sudo apt install fswebcam

Capturing a Single Image

fswebcam -r 640x480 --no-banner image.jpg

Capturing Images at Regular Intervals

for i in {1..5}; do
    fswebcam -r 640x480 --no-banner image_$i.jpg
    sleep 2
done

This script captures 5 images with a 2-second interval between each capture.

2. Using FFmpeg

FFmpeg is a powerful multimedia framework that can record, convert, and stream audio and video.

First, find the correct webcam device and its supported formats:

v4l2-ctl --list-devices
ffmpeg -f v4l2 -list_formats all -i /dev/video0

Choose a resolution and frame rate listed by the device. Requesting an unsupported combination can produce an Invalid argument error or cause FFmpeg to select a fallback format.

Capturing a Single Image

ffmpeg -f v4l2 -video_size 640x480 -i /dev/video0 -frames:v 1 image.jpg

Capturing a Video

ffmpeg -f v4l2 -video_size 640x480 -i /dev/video0 -c:v libx264 -t 10 output.mp4

3. Using OpenCV

OpenCV is a library of programming functions mainly aimed at real-time computer vision.

Installing OpenCV

sudo apt install libopencv-dev python3-opencv

Python Script to Capture an Image

import cv2

# Initialize the webcam
cap = cv2.VideoCapture(0)

# Check if the webcam is opened correctly
if not cap.isOpened():
    raise IOError("Cannot open webcam")

# Capture a frame
ret, frame = cap.read()

if not ret:
    raise IOError("Failed to capture image")

# Save the frame as an image
cv2.imwrite('image.jpg', frame)

# Release the webcam
cap.release()

Python Script to Capture a Video

import cv2

# Initialize the webcam
cap = cv2.VideoCapture(0)

# Check if the webcam is opened correctly
if not cap.isOpened():
    raise IOError("Cannot open webcam")

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))

# Capture and save 10 seconds of video
for _ in range(200):  # 200 frames at 20 FPS = 10 seconds
    ret, frame = cap.read()
    if not ret:
        break
    out.write(frame)

# Release everything
cap.release()
out.release()
cv2.destroyAllWindows()

4. Using GStreamer with Video4Linux

GStreamer is a pipeline-based multimedia framework that links together a wide variety of media processing systems to complete complex workflows.

Capturing a Single Image

gst-launch-1.0 v4l2src device=/dev/video0 ! video/x-raw,width=640,height=480 ! jpegenc ! filesink location=image.jpg

Capturing a Video

gst-launch-1.0 v4l2src device=/dev/video0 ! video/x-raw,width=640,height=480 ! videoconvert ! x264enc ! mp4mux ! filesink location=output.mp4

Conclusion

In this blog post, we explored four different methods to capture images and videos from a webcam on a Linux system. Each method has its own advantages and use cases:

Choose the method that best fits your project requirements and coding preferences. Happy coding!

FFmpegFFmpeg webcamFFmpeg webcam capture LinuxLinux webcam captureFFmpeg video recordingV4L2OpenCV webcamfswebcam