[翻译完成][tech]Lock your camera to a specific USB port in OBS

This commit is contained in:
Hans zhao 2023-05-02 17:36:27 +08:00 committed by GitHub
parent 4c4bb58989
commit ba3de1b74e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 149 additions and 172 deletions

View File

@ -1,172 +0,0 @@
[#]: subject: "Lock your camera to a specific USB port in OBS"
[#]: via: "https://opensource.com/article/22/1/cameras-usb-ports-obs"
[#]: author: "Seth Kenlon https://opensource.com/users/seth"
[#]: collector: "lujun9972"
[#]: translator: "hanszhao80"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
Lock your camera to a specific USB port in OBS
======
To standardize a complex camera setup, you can impose some special rules
on how cameras get assigned to locations in the Linux filesystem.
![Person using a laptop][1]
If you [stream with OBS][2] with multiple cameras on Linux, you might notice that cameras are loaded as they are detected during boot. You probably don't give it much thought, normally, but if you have a permanent streaming setup with complex OBS templates, you need to know which camera in the physical world is going to show up in which screen in the virtual one. In other words, you don't want to assign one device as Camera A today only to have it end up as Camera B tomorrow.
To standardize a complex camera setup, you can impose some special rules on how cameras get assigned to locations in the Linux filesystem.
### The udev subsystem
The system dealing with hardware peripherals on Linux is called udev. It detects and manages all devices you plug into your computer. You're probably not aware of it because it doesn't draw too much attention to itself, although you've certainly interacted with it when you plug in a USB thumb drive to open on your desktop or attached a printer.
### Hardware detection
Assume you have two USB cameras: One on the left of your computer and one on the right. The left camera is shooting a close-up, the right camera is shooting a long shot, and you switch between the two during your stream. In OBS, you add each camera to your **Sources** panel and, intuitively, call one **camLEFT** and the other **camRIGHT**.
Assuming the worst-case scenario, say you have two of the _same_ cameras: They're the same brand and the same model number. This is the worst-case scenario because when two pieces of hardware are identical, there's little chance each one has any kind of unique ID for your computer to differentiate them from one another.
There's a solution to this puzzle, though, and it just requires a little investigation using some simple terminal commands.
#### 1\. Get the vendor and product IDs
First, plugin just one camera into the USB port you want it assigned to. Then issue this command:
```
$ lsusb
Bus 006 Device 002: ID 0951:1666 Kingston Technology DataTraveler G4
Bus 005 Device 003: ID 03f0:3817 Hewlett-Packard LaserJet P2015 series
Bus 003 Device 006: ID 045e:0779 Microsoft Corp. LifeCam HD-3000
Bus 003 Device 002: ID 8087:0025 Intel Corp. 
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 046d:c216 Logitech, Inc. Dual Action Gamepad
Bus 001 Device 002: ID 048d:5702 Integrated Technology Express, Inc. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
[...]
```
You can usually search specifically for the string "cam" to narrow down the results because most (but not all) cameras report as a camera.
```
$ lsusb | grep -i cam
Bus 003 Device 006: ID 045e:0779 Microsoft Corp. LifeCam HD-3000
```
There's a lot of information here. The ID is listed as `045e:0779`. The first number is the vendor ID, and the second is the product ID. Write those down because you'll need them later.
#### 2\. Get the USB identifier
You've also obtained the device path to the camera: bus 3, device 6. There's a saying in Linux that "everything is a file," and indeed USB devices are described to udev as a file path starting with `/dev/bus/usb/` and ending with the bus (003 in this case) and the device (006 in this case). Look at the bus and device numbers in the `lsusb` output. They tell you that this camera is located on `/dev/bus/usb/003/006`.
You can use the `udevadm` command to obtain the kernel's designator for this USB device:
```
$ sudo udevadm info \
\--attribute-walk \
/dev/bus/usb/003/006 | grep "KERNEL="
   KERNEL=="3-6.2.1"
```
The kernel USB identifier in this example is `3-6.2.1`. Write down the identifier for your system because you'll also need it later.
#### 3\. Repeat for each camera
Attach the other camera (or cameras, if you have more than two) to the USB port you want it assigned to. This is _different_ than the USB port you used for the other camera!
Repeat the process, obtaining the vendor and product ID (if the cameras are the same make and model, these should be the same as the first one) and the kernel USB identifier.
```
$ lsusb | grep -i cam
Bus 001 Device 004: ID 045e:0779 Microsoft Corp. LifeCam HD-3000
$ sudo udevadm info \
\--attribute-walk \
/dev/bus/usb/001/004 | grep "KERNEL="
   KERNEL=="1-6"
```
In this example, I've determined that I have my cameras attached to 1-6 and 3-6.2.1 (the first one is a USB port on my machine, the other one is a hub plugged into the monitor plugged into my machine, which is why one is more complex than the other.)
### Write a udev rule
You have everything you need, so now you can write a rule to tell udev to give each camera a consistent identifier when one is found at a specific USB port.
Create and open a file called `/etc/udev/rules.d/50-camera.conf`, and enter these two rules, using the vendor and product IDs and kernel identifiers appropriate for your own system:
```
SUBSYSTEM=="usb", KERNEL=="1-6", ATTR{idVendor}=="045e", ATTR{idProduct}=="0779", SYMLINK+="video100"
SUBSYSTEM=="usb", KERNEL=="3-6.2.1", ATTR{idVendor}=="045e", ATTR{idProduct}=="0779", SYMLINK+="video101"
```
These rules tell udev that when it finds a device matching a specific vendor and product ID at those specific USB locations, to create a symlink (sometimes also called an "alias") named `video100` and `video101`. The symlinks are mostly arbitrary. I give them high numbers, so they're easy to spot and because the number must not clash with existing devices. If you actually do have more than 101 cameras attached to your computer, use `video200` and `video201` just to be safe (and get in contact! I'd love to learn more about _that_ project).
### Reboot
Reboot your computer. You can leave the cameras attached for now, but it doesn't actually matter. Once udev has a rule loaded, it follows those rules whether a device was attached during boot or is plugged in later.
Many people say that Linux never needs to reboot, but udev loads its rules during boot, and besides, you want to prove that your udev rules are working across reboots.
Once your computer is back up and running, take a look in `/dev/video`, where cameras are registered:
```
$ ls -1 /dev/video*
/dev/video0
/dev/video1
/dev/video100
/dev/video101
/dev/video2
/dev/video3
```
As you can see, there are entries at `video100` and `video101`. Today, these are symlinks to `/dev/video2` and `/dev/video3`, but tomorrow they may be symlinks to `/dev/video1` and `/dev/video2`, or any other combination based on when Linux detected and assigned them a file.
![Two camera angles][3]
(Photo by [Jeff Siepman][4])
You can use the symlinks in OBS, though, so that camLEFT is always camLEFT, and camRIGHT is always camRIGHT.
--------------------------------------------------------------------------------
via: https://opensource.com/article/22/1/cameras-usb-ports-obs
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[hanszhao80](https://github.com/hanszhao80)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/laptop_screen_desk_work_chat_text.png?itok=UXqIDRDD (Person using a laptop)
[2]: https://opensource.com/life/15/12/real-time-linux-video-editing-with-obs-studio
[3]: https://opensource.com/sites/default/files/uploads/obs-udev.jpg (Two camera angles)
[4]: https://unsplash.com/@jeffsiepman?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText

View File

@ -0,0 +1,149 @@
[#]: subject: "Lock your camera to a specific USB port in OBS"
[#]: via: "https://opensource.com/article/22/1/cameras-usb-ports-obs"
[#]: author: "Seth Kenlon https://opensource.com/users/seth"
[#]: collector: "lujun9972"
[#]: translator: "hanszhao80"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
在 OBS 中将摄像头锁定到特定的 USB 端口
======
为了使复杂的摄像头设置标准化,你可以对 Linux 文件系统中摄像头的位置分配施加一些特殊规则。
![使用笔记本电脑的人][1]
如果在 Linux 上用多个摄像头 [使用 OBS 进行直播][2],你可能会注意到摄像头会在开机时按照它们被检测到的顺序加载。通常情况下你可能不会太在意,但如果你有一个永久的直播设置和复杂的 OBS 模板,你需要知道物理世界中哪个摄像头将会显示在虚拟世界的哪个屏幕上。换句话说,你不希望今天将一个设备分配为“摄像头 A”而明天它却成为“摄像头 B”。
为了使复杂的摄像头设置标准化,你可以对 Linux 文件系统中摄像头的位置分配施加一些特殊规则。
### udev 子系统
在 Linux 上处理硬件外设的系统称为 udev。它检测和管理你接入计算机的所有设备。你可能没有意识到它的存在因为它不会吸引太多注意力。尽管当你插入 USB 闪存驱动器以在桌面上打开它或连接打印机时,你肯定与它互动过。
### 硬件检测
假设你有两个 USB 摄像头:一个在电脑左侧,另一个在右侧。左侧摄像头拍摄近景,右侧摄像头拍摄远景,并且在直播过程中你需要切换两个摄像头。在 OBS 中,你将每个摄像头添加到 **<ruby><rt>Sources</rt></ruby>** 面板中,并直观地将它们命名为 **camLEFT****camRIGHT**
设想一种最坏的场景,你有两个 _相同的_ 摄像头:它们是同一品牌和同一型号。这是最坏的情况,因为当两个硬件设备完全相同时,它们几乎不可能有任何独特的 ID以便你的电脑能够将它们区分开来。
不过,这个难题有解决办法,只需要一些简单的终端命令进行一些调查。
#### 1. 获取厂商和产品 ID
首先,将一个摄像头插入你想要它分配到的 USB 端口。然后输入以下命令:
```
$ lsusb
Bus 006 Device 002: ID 0951:1666 Kingston Technology DataTraveler G4
Bus 005 Device 003: ID 03f0:3817 Hewlett-Packard LaserJet P2015 series
Bus 003 Device 006: ID 045e:0779 Microsoft Corp. LifeCam HD-3000
Bus 003 Device 002: ID 8087:0025 Intel Corp. 
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 046d:c216 Logitech, Inc. Dual Action Gamepad
Bus 001 Device 002: ID 048d:5702 Integrated Technology Express, Inc. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
[...]
```
你通常可以专门搜索字符串 `cam` 以缩小结果范围,因为大多数(但不是所有)摄像头都会报告为摄像头。
```
$ lsusb | grep -i cam
Bus 003 Device 006: ID 045e:0779 Microsoft Corp. LifeCam HD-3000
```
这里有很多信息。ID 被列为 `045e:0779`。第一个数字是供应商 ID第二个数字是产品 ID。把它们写下来因为稍后你会需要它们。
#### 2. 获取 USB 标识符
你还获取了摄像头的设备路径:总线 3设备 6。在 Linux中有一句话“一切皆文件”实际上USB 设备被描述为以 `/dev/bus/usb/` 开始,以总线(本例中为 003和设备本例中为 006结尾的文件路径。查看 `lsusb` 输出中的总线和设备号。它们告诉你该摄像头位于 `/dev/bus/usb/003/006`
你可以使用 `udevadm` 命令获取此 USB 设备的内核代号:
```
$ sudo udevadm info --attribute-walk /dev/bus/usb/003/006 | grep "KERNEL="
   KERNEL=="3-6.2.1"
```
这个例子中的内核 USB 标识符是 `3-6.2.1`。把你系统中的标识符记下来,因为之后也会用到它。
#### 3. 为每个摄像头重复该过程
将另一个摄像头(如果你有多个摄像头,则为每个摄像头)连接到要分配给它的 USB 端口。这与你用于另一个摄像头的 USB 端口是不同的!
重复该过程,获取供应商和产品 ID如果摄像头是相同的品牌和型号则应与第一个摄像头相同以及内核 USB 标识符。
```
$ lsusb | grep -i cam
Bus 001 Device 004: ID 045e:0779 Microsoft Corp. LifeCam HD-3000
$ sudo udevadm info --attribute-walk dev/bus/usb/001/004 | grep "KERNEL="
   KERNEL=="1-6"
```
在这个例子中,我已经确定我的摄像头连接到了 1-6 和 3-6.2.1(第一个是我的机器上的 USB 端口,另一个是插在我的机器上的显示器插口的集线器,这就是为什么一个比另一个更复杂的原因)。
### 编写一个 udev 规则
你已经有了所需的一切,因此现在可以编写一个规则,告诉 udev 在特定的 USB 端口找到一个摄像头时给它一个一致的标识符。
创建并打开一个名为 `/etc/udev/rules.d/50-camera.conf` 的文件,并输入这两个规则,使用适合你自己系统的厂商和产品 ID 和内核标识符:
```
SUBSYSTEM=="usb", KERNEL=="1-6", ATTR{idVendor}=="045e", ATTR{idProduct}=="0779", SYMLINK+="video100"
SUBSYSTEM=="usb", KERNEL=="3-6.2.1", ATTR{idVendor}=="045e", ATTR{idProduct}=="0779", SYMLINK+="video101"
```
这些规则告诉 udev当在特定的 USB 位置找到与特定供应商和产品 ID 匹配的设备时,创建一个名为 `video100``video101` 的符号链接(有时也称为“别名”)。符号链接大多是任意的。我使用大数值数字,这样它们就容易被发现,并且数字不能与现有设备冲突。如果实际上有超过 101 个摄像头连接到计算机上,请使用 `video200``video201` 以确保安全(记得联系我!我很想了解 _那个_ 项目)。
### 重启
重新启动计算机。你现在可以让摄像头保持连接,但实际上这并不重要。一旦 udev 加载了规则,它就会遵循这些规则,无论设备是否在启动期间附加或稍后插入。
许多人说 Linux 从不需要重启,但是 udev 在引导期间加载其规则,而且此外,你想保证你的 udev 规则在重新启动时也起作用。
计算机重新启动后,请查看摄像头注册的 `/dev/video` 目录:
```
$ ls -1 /dev/video*
/dev/video0
/dev/video1
/dev/video100
/dev/video101
/dev/video2
/dev/video3
```
正如你所看到的,在 `video100``video101` 有条目。今天,这些是指向 `/dev/video2``/dev/video3` 的符号链接,但明天它们可能是指向 `/dev/video1``/dev/video2` 或任何其他基于 Linux 检测和分配文件的组合。
![两个摄像头角度][3]
(Photo by [Jeff Siepman][4])
你可以在 OBS 中使用这些符号链接,这样 camLEFT 始终是 camLEFTcamRIGHT 始终是 camRIGHT。
--------------------------------------------------------------------------------
via: https://opensource.com/article/22/1/cameras-usb-ports-obs
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[hanszhao80](https://github.com/hanszhao80)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/laptop_screen_desk_work_chat_text.png?itok=UXqIDRDD (Person using a laptop)
[2]: https://opensource.com/life/15/12/real-time-linux-video-editing-with-obs-studio
[3]: https://opensource.com/sites/default/files/uploads/obs-udev.jpg (Two camera angles)
[4]: https://unsplash.com/@jeffsiepman?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText