> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/di-14-zhang-duo-mei-ti-he-wai-bu-she-bei/di-14.3-jie-she-xiang-tou.md).

# 14.3 摄像头

本节将介绍如何为 FreeBSD 系统配置摄像头。

Video for Linux 2（V4L2）是第二代 V4L。V4L2 是 Linux 内核中视频设备的内核驱动框架。

FreeBSD 访问和配置摄像头需要安装以下工具：

| Port                 | 说明                                                                  |
| -------------------- | ------------------------------------------------------------------- |
| multimedia/webcamd   | 守护进程，从 Linux USB 网络摄像头和 DVB 驱动程序移植而来，支持使用数百种不同的 USB 摄像头和 DVB USB 设备 |
| multimedia/pwcview   | 可用于查看摄像头的视频流                                                        |
| multimedia/mpv       | 也可用于查看摄像头的 V4L2 视频流                                                 |
| multimedia/v4l-utils | 用于配置和测试 Video4Linux 设备                                              |

## 安装摄像头软件

要使用 pkg 安装所需的实用工具，请执行：

```sh
# pkg install webcamd pwcview mpv v4l-utils
```

使用 Ports 安装：

```sh
# cd /usr/ports/multimedia/webcamd && make install clean
# cd /usr/ports/multimedia/pwcview && make install clean
# cd /usr/ports/multimedia/mpv && make install clean
# cd /usr/ports/multimedia/v4l-utils && make install clean
```

## 服务管理

启用 webcamd 服务，以便在系统启动时自动启动：

```sh
# service webcamd enable
```

重新加载设备状态服务：

```sh
# service devd restart
```

## 用户管理

用户必须属于 `webcamd` 组。要将用户 ykla 添加到 `webcamd` 组，请执行以下命令：

```sh
# pw groupmod webcamd -m ykla
```

## 获取摄像头列表

安装完这些工具后，可以使用 webcamd 显示可用的摄像头列表：

```sh
# webcamd -l
```

输出应该类似于以下内容：

```sh
webcamd [-d ugen1.2] -N Jieli-Technology-USB-Composite-Device -S unknown -M 0
```

发现了可用的摄像头“Jieli-Technology-USB-Composite-Device”。

临时测试摄像头：

```sh
$ webcamd -d ugen1.2
```

请保持其在前台运行，便于后续配置。

将生成设备文件 **/dev/video0** 和 **/dev/video1**。

## 查看摄像头配置

使用 **v4l2-ctl** 工具列出摄像头支持的参数：

```sh
$ v4l2-ctl --list-formats-ext -d /dev/video0

ioctl: VIDIOC_ENUM_FMT

        Type: Video Capture



        [0]: 'MJPG' (Motion-JPEG, compressed)

                Size: Discrete 1920x1080

                        Interval: Discrete 0.033s (30.000 fps)

                        Interval: Discrete 0.040s (25.000 fps)

                Size: Discrete 1280x720

                        Interval: Discrete 0.033s (30.000 fps)

                        Interval: Discrete 0.040s (25.000 fps)

                Size: Discrete 640x480

                        Interval: Discrete 0.033s (30.000 fps)

                        Interval: Discrete 0.040s (25.000 fps)

                Size: Discrete 640x360

                        Interval: Discrete 0.033s (30.000 fps)

                        Interval: Discrete 0.040s (25.000 fps)

                Size: Discrete 352x288

                        Interval: Discrete 0.033s (30.000 fps)

                        Interval: Discrete 0.040s (25.000 fps)

        [1]: 'YUYV' (YUYV 4:2:2)

                Size: Discrete 640x480

                        Interval: Discrete 0.033s (30.000 fps)

                Size: Discrete 640x360

                        Interval: Discrete 0.033s (30.000 fps)

                Size: Discrete 352x288

                        Interval: Discrete 0.033s (30.000 fps)

                Size: Discrete 320x240

                        Interval: Discrete 0.033s (30.000 fps)
```

以上详细列出了该摄像头支持的视频格式、分辨率以及在不同分辨率下的帧率（FPS）。

## 检查摄像头

### 使用 multimedia/pwcview 检查摄像头

可以使用 multimedia/pwcview 检查摄像头是否正常工作。

临时禁用 SDL 的 Xv 硬件加速，避免黑屏等意外情况，如果测试通过可移除该环境变量。

```sh
$ export SDL_VIDEO_YUV_HWACCEL=0
```

再次运行 pwcview（分辨率 1600x1200\@30 帧）：

```sh
$ pwcview -f 30 -s uxga
```

此后 multimedia/pwcview 将显示摄像头画面：

![pwcview 显示的摄像头画面](/files/5A3Y3WbvRIQZeOtIinpU)

### 使用 mpv 检查摄像头

可以使用 multimedia/mpv 检查摄像头是否正常工作。

```sh
$ mpv av://v4l2:/dev/video0 --vo=x11
```

以上命令使用 `--vo=x11` 禁用硬件加速，避免意外情况，如果测试通过可移除该参数。

此后 multimedia/mpv 将显示摄像头画面：

![mpv 显示的摄像头画面](/files/EdHTLAjoZ4GiRVBaFUVn)

## 永久设置摄像头

确认摄像头工作正常后，可以固化上述配置。

通过执行以下命令配置可用的摄像头：

```sh
# sysrc webcamd_0_flags="-d ugen1.2"
```

> **注意**
>
> 如果这是即插即用的 USB 摄像头，在其连接的 USB 端口发生变动后，`webcamd -l` 的输出（尤其是设备标识符）将发生变化，可能需要同步更新 **/etc/rc.conf**。

必须通过执行以下命令启动 webcamd 服务：

```sh
# service webcamd restart
```

输出应类似于以下内容：

```sh
Starting webcamd.
webcamd 2652 - - Attached to ugen1.2[0]
```

## 在线测试摄像头

使用“摄像头测试”网站在线测试摄像头：<https://www.onlinemictest.com/webcam-test>。

![在线测试摄像头的显示画面](/files/PQqoXrZNCJONxoDonQwm)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/di-14-zhang-duo-mei-ti-he-wai-bu-she-bei/di-14.3-jie-she-xiang-tou.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
