Skip to content

Instantly share code, notes, and snippets.

@FlyingFathead
Last active June 3, 2024 09:07
Show Gist options
  • Save FlyingFathead/a6ee6d1f677a4602318db26087efd1d7 to your computer and use it in GitHub Desktop.
Save FlyingFathead/a6ee6d1f677a4602318db26087efd1d7 to your computer and use it in GitHub Desktop.
usb camera identifier and lister for debian/ubuntu linux
#!/bin/bash
# This script is designed to identify and list all USB cameras and audio devices connected to a system.
# It provides detailed information about each device, including vendor ID, product ID, serial number,
# device description, and supported video modes for cameras. It also identifies audio devices and
# lists their card and device numbers, names, and descriptions.
# Required Packages:
# - udevadm: Provides information about device attributes.
# - v4l2-ctl: A utility to control video4linux devices and list video formats.
# - lsusb: Lists USB devices and provides descriptions for connected devices.
# - arecord: A command-line sound recorder and player for ALSA soundcard driver, used to list audio devices.
# Installation for required packages:
# $ sudo apt-get install v4l-utils
# $ sudo apt-get install usbutils
# $ sudo apt-get install alsa-utils
# Directory to store camera information
CAMERA_INFO_DIR="/tmp/camera_info"
mkdir -p $CAMERA_INFO_DIR
# Clear previous camera info
rm -f $CAMERA_INFO_DIR/*
# Log file for debugging
LOG_FILE="/tmp/list_usb_cameras.log"
echo "Starting camera identification..." > $LOG_FILE
# Update the lsusb information
LSUSB_INFO=$(lsusb)
# Find all video devices and check if they are USB cameras
for video_device in /dev/video*; do
# Get device attributes
udevadm info --name=$video_device --query=all > $CAMERA_INFO_DIR/$(basename $video_device).info
# Check if the device is a USB camera
if grep -q 'ID_USB_DRIVER=uvcvideo' $CAMERA_INFO_DIR/$(basename $video_device).info; then
echo "$video_device is a USB camera" >> $LOG_FILE
# Get vendor, product ID, and serial number (if available)
vendor_id=$(grep 'ID_VENDOR_ID=' $CAMERA_INFO_DIR/$(basename $video_device).info | cut -d'=' -f2)
product_id=$(grep 'ID_MODEL_ID=' $CAMERA_INFO_DIR/$(basename $video_device).info | cut -d'=' -f2)
serial=$(grep 'ID_SERIAL_SHORT=' $CAMERA_INFO_DIR/$(basename $video_device).info | cut -d'=' -f2)
# Determine the mode
formats=$(v4l2-ctl --device=$video_device --list-formats-ext 2>/dev/null)
echo "$formats" > $CAMERA_INFO_DIR/$(basename $video_device).formats
modes=$(echo "$formats" | grep -oP "(?<=')[A-Z0-9]+(?=')" | tr '\n' ',' | sed 's/,$//')
# Skip devices without video modes
if [ -z "$modes" ]; then
echo "$video_device does not have valid video modes, skipping..." >> $LOG_FILE
echo "Device $video_device (Vendor ID: $vendor_id, Product ID: $product_id) is likely an audio device." >> $LOG_FILE
continue
fi
# Fetch a more descriptive name from lsusb
device_description=$(echo "$LSUSB_INFO" | grep "$vendor_id:$product_id" | cut -d' ' -f7-)
# Print camera information
echo "Camera: $video_device" >> $LOG_FILE
echo " Vendor ID: $vendor_id" >> $LOG_FILE
echo " Product ID: $product_id" >> $LOG_FILE
echo " Serial: $serial" >> $LOG_FILE
echo " Description: $device_description" >> $LOG_FILE
echo " Modes: $modes" >> $LOG_FILE
fi
done
# Check for audio devices
echo "Checking for audio devices..." >> $LOG_FILE
arecord -l | grep '^card' | while read -r line; do
card=$(echo "$line" | awk '{print $2}' | tr -d ':')
device=$(echo "$line" | awk '{print $5}' | tr -d ':')
name=$(echo "$line" | awk -F '[][]' '{print $2}')
description=$(echo "$line" | awk -F '[][]' '{print $4}')
echo "Audio Device - Card: $card, Device: $device, Name: $name, Description: $description" >> $LOG_FILE
done
echo "Camera and audio identification completed." >> $LOG_FILE
# Output the log file content
cat $LOG_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment