Skip to content

Capturing Images and Video from Webcams in Linux

Haikel Fazzani Haikel Fazzani
2025-07-28

In this blog post, I’ll show you how to accomplish this using four different tools/frameworks: fswebcam, FFmpeg, OpenCV, and GStreamer with Video4Linux. Each method has its advantages, and I’ll provide simple code examples to get you started.

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.

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!

Linux webcam capture FFmpeg webcam OpenCV webcam fswebcam tutorial GStreamer video recording V4L2 Linux

More Insights.