This page is about how to use the Logitech C270 Webcam with Linux, in this case Debian Buster. This is a cheap webcam I bought to play with video but at the time I did not know whether it would be easy to use with Linux or require a lot of hacking. In short, this camera is immediately recognised by V4L2 (Video4Linux) which means it is not difficult to get working.

Quick Tests

At this stage the camera seems to be working well.

Use of ffmpeg

Using ffmpeg for recording images or video seems to be a convenient solution for many tasks.

List v4l devices:

ffmpeg -f v4l2 -list_formats all -i /dev/video0

Change the /dev/video0 part to whatever worked for VLC. We can see the camera supports raw acquisition in YUYV 4:2:2 colour space or you can use the onboard MJPEG encoder:

[video4linux2,v4l2 @ 0x5631fbb20380] Raw       :     yuyv422 :           YUYV 4:2:2 : 640x480 160x120 176x144 320x176 320x240 352x288 432x240 544x288 640x360 752x416 800x448 800x600 864x480 960x544 960x720 1024x576 1184x656 1280x720 1280x960
[video4linux2,v4l2 @ 0x5631fbb20380] Compressed:       mjpeg :          Motion-JPEG : 640x480 160x120 176x144 320x176 320x240 352x288 432x240 544x288 640x360 752x416 800x448 800x600 864x480 960x544 960x720 1024x576 1184x656 1280x720 1280x960

Grab 4 seconds of webcam as an anim gif:

ffmpeg -f v4l2 -framerate 25 -video_size 640x480 -i /dev/video0 -t 00:00:04 output.gif

Wait 4 seconds then grab a single image:

ffmpeg -ss 4 -f v4l2 -framerate 25 -video_size 640x480 -i /dev/video0 -frames:v 1 output.jpg

Why wait 4 seconds? The camera needs to start and set auto gain.

Simple Webcam Page

If you are running a webserver that has PHP access then you can create an online webcam by modifying the following scripts and copying them to suitable locations.

webcam.php

<html>
 <head>
  <title>My Webcam</title>
  <meta http-equiv="refresh" content="5">
 </head>
 <body>

  <?php exec("/path/to/grabframe.sh"); ?>

  <h1><?php echo date("Y-m-d H:i:s",time()); ?></h1>

  <span style="text-align:center">
   <img src="webcamframe.jpeg">
  </span>

 </body>
</html>

grabframe.sh

#!/bin/bash

cd /location/of/above/php/script
ffmpeg -y -ss 4 -f v4l2 -framerate 25 -video_size 640x480 -i /dev/video0 -frames:v 1 webcamframe.jpeg

Using gstreamer

For gstreamer it is assumed you have installed all the necessary codec packages and extensions.

If you know the video device will be /dev/video0 then you can skip this parameter in the following as video0 is the default. The frame rate will probably be a bit choppy without any set up:

gst-launch-1.0 v4l2src device=/dev/video0 ! xvimagesink

Setting some parameters will give better resolution:

gst-launch-1.0 -v v4l2src device=/dev/video0 ! video/x-raw,width=1280,height=720 ! xvimagesink

In raw mode the frame rate is limited unless you drop resolution, like so:

gst-launch-1.0 -v v4l2src device=/dev/video0 ! video/x-raw,framerate=30/1,width=640,height=480 ! xvimagesink

Switching to MJPEG mode allows those higher resolutions:

gst-launch-1.0 -v v4l2src device=/dev/video0 ! image/jpeg,framerate=30/1,width=1280,height=720 ! jpegdec ! videoconvert ! autovideosink