结论:用 usbipd-win 在 Windows 上共享摄像头,再在 WSL2 中 attach 并在 Linux 里配置权限与测试即可得到 /dev/video0。下面是一份从零开始的可复制教程。

必备条件

  • Windows 11 或支持的 Windows 10,已安装 WSL2;WSL 内核需 ≥ 5.10.60.1Microsoft Learn Github

在 Windows 上安装与共享 USB 设备

  1. 安装 usbipd-win(可用 winget):
winget install --interactive --exact dorssel.usbipd-win
  1. 以管理员打开 PowerShell,把摄像头标记为 Shared:
usbipd list # 获取BUSID
usbipd bind --busid <BUSID>

PS C:\WINDOWS\system32> usbipd list
Connected:
BUSID  VID:PID    DEVICE                                                        STATE
1-2    2bdf:0280  HIK 1080P Camera, HIK 1080P Camera-Audio                      Shared
1-14   8087:0029  英特尔(R) 无线 Bluetooth(R)                                   Not shared
2-1    0d8c:0014  USB Audio Device, USB 输入设备                                Not shared
2-3    093a:2510  USB 输入设备                                                  Not shared
2-4    05ac:024f  USB 输入设备                                                  Not shared
3-3    2f4c:1000  USB 输入设备                                                  Not shared

Persisted:
GUID                                  DEVICE

 

  1. 把设备 attach 到 WSL(可指定发行版或默认):
usbipd attach --busid <BUSID> --wsl Ubuntu-24.04

PS C:\WINDOWS\system32> usbipd attach --busid 1-2 --wsl Ubuntu-24.04
usbipd: info: Selecting a specific distribution is no longer required. Please file an issue if you believe that the default selection mechanism is not working for you.
usbipd: info: Using WSL distribution 'Ubuntu-24.04' to attach; the device will be available in all WSL 2 distributions.
usbipd: info: Loading vhci_hcd module.
usbipd: info: Detected networking mode 'nat'.
usbipd: info: Using IP address 172.29.64.1 to reach the host.
WSL wsl: �hKm0R localhost �NtM�n
                                   �FO*g\��P0R WSL0NAT !j_
                                                             N�v WSL
WSL N/ec localhost �Nt0
WSL

成功后 Windows 会加载 vhci_hcd 并转发设备。 Github Microsoft Developer Blogs


在 WSL 内确认与安装工具

在 WSL 里执行:

uname -a
sudo apt update
sudo apt install -y usbutils v4l-utils linux-tools-virtual hwdata
lsusb
dmesg | tail -n 50
ls -l /dev/video*
v4l2-ctl --list-devices

若能看到摄像头并出现 /dev/video0,说明已成功。 Github


权限与测试

  • 把用户加入 video 组:
sudo usermod -aG video $USER
  • 或添加 udev 规则让设备可被普通用户访问:
sudo tee /etc/udev/rules.d/99-video.rules <<'EOF'
SUBSYSTEM=="video4linux", KERNEL=="video[0-9]*", MODE="0666", GROUP="video"
EOF
sudo udevadm control --reload-rules
sudo udevadm trigger
  • 测试抓帧:
ffmpeg -f v4l2 -i /dev/video0 -frames:v 1 test.jpg
python3 -c "import cv2;print(cv2.VideoCapture('/dev/video0').isOpened())"

常见问题与解决

  • 看见 lsusb 但无 /dev/video*:更新 WSL 内核 wsl --updatewsl --shutdown,或使用带 V4L2/UVC 的自定义内核。 Microsoft Learn Github
  • 还原设备usbipd detach --busid <BUSID>usbipd unbind --busid <BUSID>