The Raspberry Pi and camera module can be connected directly via the CSI connection on the main board. A dedicated command application is also available, which allows you to take pictures by using the command rpicam-still.
However, this is only available with the camera module connected to the CSI, and cannot be used with a general USB-connected web camera.
rpicam-still command:
○ Camera module with CSI connection
× Web camera with general USB connection
So how should a webcam connected via USB take pictures?
Substitute other software.
Although there is software that can take pictures with GUI, we will show you how to operate it with commands (CUI) so that you can incorporate it into your programs and scripts.
Separate software was used for still images and video.

The Raspberry Pi used is a ComputeModule 5 and the webcam is a Logitech C270n.
The C720n is an older release but still available at the time of writing; it supports up to 720P (1280×720) HD quality and has a built-in microphone. The picture and sound quality is just right for video conferencing. It is also reasonably priced, so it was just the right product for the Raspberry Pi.
We will now take you through the process of taking still images and recording video in turn.
Take still pictures with fswebcam
The still image to be taken by the web camera uses the software fswebcam, which is also introduced in the official Raspberry Pi.
It is easy to install with apt.
sudo apt install fswebcam
Add the user (in this case raspida) to the video group. Replace the name with your current logged in user name, etc. and read:
Adding a logged in user to the video group will allow you to run without permission errors.
sudo usermod -a -G video raspida
The basic format for filming is simple as follows
fswebcam test.jpg
However, the default (standard) resolution with no options specified is a small resolution (352×288).

Actual resolution (352×288)
You can see a thin bar at the bottom and the date and time in the lower right corner. If the text is garbled, installing noto-fonts will fix the problem.
If the text is garbled, install additional fonts:
sudo apt install fonts-noto-cjk
There are many options, but let’s start with the resolution. See fswebcam --helpfor more information.
-roption allows the resolution to be specified.
fswebcam -r 1280x720 test.jpg
Next is the --no-banneroption.
This will be an option to stop the bar-like output that appears with the date at the bottom of the image.
With these two options, we were able to capture very common images.
fswebcam -r 1280x720 --no-banner ファイル名.jpg

Resolution 1280×720 with no banner
Put the date in the file name
fswebcam does not provide file naming functions such as date or sequential numbering. The official version introduces a shell script to use instead.
By executing the shell script, you can take a picture with the date file name.
The description is three lines. Create a new sh file in an appropriate location as follows and give it execute permission.
nano webcam.sh
chmod +x webcam.sh
The content of sh is to use the fswebcam command, have it take a picture at 1280×720, and name the file to be saved with the date.
#!/bin/bash
DATE=$(date +"%Y-%m-%d_%H-%M-%S")
fswebcam -r 1280x720 --no-banner $DATE.jpg
If you have moved to the same directory as the sh file you created, you can execute the following shell command to take a still image.
./webcam.sh
This will save an image file with the date name each time it is taken.
Video with ffmpeg
Next is the video.
This one can be substituted by using ffmpeg.
It is installed from the beginning on the Raspberry Pi OS. (version 8:5.1.6-0+deb12u1+rpt3)
When recording video with a webcam (USB) connected to a Raspberry Pi, you will be running ffmpeg with various options.
The options are not related to Raspberry Pi, but to ffmpeg and video format. For details, please check ffmpeg related separately.
This time we used C270n and tried the following command options first.
Since the camera is equipped with a microphone, audio is also recorded together.
Finally, the output file is named output.mkv. You can change the name as you wish.
Press Qin the terminal to stop recording.
ffmpeg -f alsa -i pulse -f v4l2 -input_format mjpeg -i /dev/video0 -c:v copy -c:a aac output.mkv
The input source is the audio source.-f alsa -i pulseand the video is-f v4l2 -i /dev/video0Specified by
Video Format.-input_format mjpegto mjpeg format, and the device number is/dev/video0In,-c:v copycopies video directly from the input source (without re-encoding),-c:a aacconverts the audio to AAC format and the name of the video file to be created at the end.
There is an order in which the options are placed.
The basic rule is that they may be placed before or after the input option -i. If there is an error or the option is not working, double check the insertion position.
With this as the basis, let’s specify the frame rate and then the screen resolution.
ffmpeg -f alsa -i pulse -f v4l2 -input_format mjpeg -framerate 30 -video_size 800x600 -i /dev/video0 -c:v copy -c:a aac output_800x600.mkv
The frame rate is 30 fps at -framerate 30and the screen resolution is -video_size 800x600just inserted after -input_format mjpeg.
H.264 format mp4
This time, instead of mjpeg, I will use raw yuyv422 as the input source and then have it converted to h264 to create an H264 mp4.
This will keep the file size down.
ffmpeg -f alsa -i pulse -f v4l2 -input_format yuyv422 -framerate 30 -video_size 800x600 -i /dev/video0 -c:v libx264 -vf format=yuv422p -c:a aac output.mp4
Even the terminal output shows that it is encoded in H264, AAC 48 kHz, 128 kbps.
Stream #0:0: Video: h264 (avc1 / 0x31637661), yuv420p(tv, progressive), 800x600, q=2-31, 20 fps, 10240 tbn
Metadata:
encoder : Lavc59.37.100 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s
Example of final command
The ffmpeg command itself has a lot of options. You can change the image quality, consider the file size, and other options.
Since this is not directly related to the Raspberry Pi, we only show the options we used here, but you can check ffmpeg for any options you are interested in.
To record video with the webcam, we finally did the following with the webcam connected to the Raspberry Pi.
It was encoded in H264 and the audio used Opus with high quality compression. Some abbreviations are used for options.
ffmpeg -f alsa -i pulse -f v4l2 -input_format yuyv422 -r 30 -s 600x360 -i /dev/video0 -c:v libx264 -vf format=yuv422p -c:a libopus -b:a 128k output.mp4
Although the screen size is rather small, a 30-second video fits into about 1.8 MB at this setting.
The raw video source is compressed and converted (encoded) to H.264 and the raw audio to Opus format at 128 kbps.
Stream #1:0 -> #0:0 (rawvideo (native) -> h264 (libx264))
Stream #0:0 -> #0:1 (pcm_s16le (native) -> opus (libopus))
I’m not sure how to replace it with a different webcam, such as the location of the input source used within the ffmpeg command.
For this reason, it is a good idea to have information on webcams.
To know how to specify the device and the supported formats, we recommend that you run a few commands after connecting the webcam with the USB cable to find out.
Below is the procedure for examining the part of the web camera to be specified by the ffmpeg command.
Check the device information of the webcam.
After connecting the webcam, try looking at the information with the lsusbcommand.
This is a common method for USB-connected devices.
lsusb
This is how I found the ID of the Webcam C270 on the third line: 046d:0825.
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 003: ID 046d:c52b Logitech, Inc. Unifying Receiver
Bus 003 Device 002: ID 046d:0825 Logitech, Inc. Webcam C270
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 1c4f:0027 SiGma Micro USB Keyboard
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
ベンダーID:プロダクトIDThe name of the company is represented by
For other products, Logitech (Logitech, Inc.) is 046d.
Look up device number
For Linux, find out the device number (/dev/video*) that is assigned when the camera device is connected.
In general, if only one webcam is connected, it is video0.
sudo udevadm info --query=all /dev/video1 | grep 'VENDOR_ID\|MODEL_ID\|SERIAL_SHORT'
Information output after execution:
E: ID_MODEL_ID=0825
E: ID_SERIAL_SHORT=1811AF40
E: ID_VENDOR_ID=046d
E: ID_USB_MODEL_ID=0825
E: ID_USB_SERIAL_SHORT=1811AF40
E: ID_USB_VENDOR_ID=046d
We can see that the vendor ID and model ID we looked up earlier are the same. We are now confident that video0 is C720n.
If this is video1, then the ffmpeg command must be changed as well.
If you have multiple cameras connected to the Raspberry Pi, use the following command to identify them by their vendor ID or the notation “USB”.
v4l2-ctl --list-devices
UVC Camera (046d:0825) (usb-1000480000.usb-1.4.2):
/dev/video0
/dev/video1
/dev/media3
Now, we will proceed assuming a web camera connected to /dev/video0 with a USB cable.
Find out what resolutions your camera supports.
Let’s check it out with another option of the v4l2-ctl command mentioned earlier.
v4l2-ctl --list-formats
v4l2-ctl --list-formats
ioctl: VIDIOC_ENUM_FMT
Type: Video Capture
[0]: 'YUYV' (YUYV 4:2:2)
[1]: 'MJPG' (Motion-JPEG, compressed)
In addition, commands with “ext” will show the details.
v4l2-ctl --list-formats-ext
'YUYV' (YUYV 4:2:2)
Size: Discrete 640x360
Interval: Discrete 0.033s (30.000 fps)
'MJPG' (Motion-JPEG, compressed)
Size: Discrete 1280x720
Interval: Discrete 0.033s (30.000 fps)
Thus, I found that the C270 only supports up to 640×360 or less in ‘YUYV’ (YUYV 4:2:2) format if you want a frame rate of 30. It’s a bit small.
‘ MJPG’ (Motion-JPEG, compressed) format supports even 1270x.
One more thing: check the format in which the webcam is capturing.
v4l2-ctl --get-fmt-video
Format Video Capture:
Width/Height : 640/360
Pixel Format : 'YUYV' (YUYV 4:2:2)
Field : None
Bytes per Line : 1280
Size Image : 460800
Colorspace : sRGB
Transfer Function : Rec. 709
YCbCr/HSV Encoding: ITU-R 601
Quantization : Default (maps to Limited Range)
Flags :
You can also change the format settings.
If you change it with the command and record it, this content should also change.
The v4l2-ctl command has a lot of options, etc. Check it out at v4l2-ctl --help.
The same C720n webcam as in this case can be used for both CM5 and Pi 5 with no problem using the commands described here.
Different web cameras perform differently, so please check the format supported by your camera before specifying it in the command.
We have shown you how to use a Raspberry Pi to take pictures with a common USB-connected webcam.
We used fswebcam for still images and ffmpeg to capture and record video.
If you shoot on command, you can use it in your programs and scripts; we were able to make the webcam work the same way, as well as the official camera module that connects to CSI.
Official camera modules can be controlled more from the Raspberry Pi.
A dedicated command application is also available, and the manual is on the official website.
However, if you want to use a general-purpose web camera easily, please refer to this issue.
Find out in Mediainfo
mediainfoI learned about a packaged application called There is a command line tool and a GUI version, and it was convenient to get media information easily.
Since the information is about the video file you shot, it doesn’t make sense before you shot it, but it’s useful for tweaking options and comparing saved videos.
I tend to forget what options were specified, so it was useful for trial and error to change options for better image quality, balance with file size, etc.
sudo apt install mediainfo mediainfo-gui
command, you can use the mediainfo command + filename to output.
I specified a file that was shot as a movie. You can see the format information and other details.
mediainfo output.mp4
I installed the GUI version with it. It is registered in the menu and can be used by simply selecting a video file.

It is a multi-platform package, so it runs on Windows, macOS, and various Linuxes other than the Raspberry Pi OS.
Web camera with GUI

Finally, if you want to use a USB-connected webcam on your Raspberry Pi desktop screen, I recommend Guvcview, which works well with the Raspberry Pi.
The apt package is easy to install.
sudo apt install guvcview
You do not need to configure anything, it is already captured when you start Guvcview after connecting the USB cable.
If you want to use it on a Raspberry Pi OS desktop, try it once.
Article contributed by Raspida
Raspberry Pi information site that even non-engineers can enjoy using raspida.com a Raspberry Pi information site that even non-engineers can enjoy and handle. He also contributes technical blog articles to the PiLink site on the Raspberry Pi for industrial use.

