【问题标题】:is there an API to check if Mac's Microphone or video camera is in use? [closed]是否有 API 可以检查 Mac 的麦克风或摄像机是否正在使用? [关闭]
【发布时间】:2020-05-07 15:16:07
【问题描述】:

是的,我意识到当摄像机打开时我可以只看绿灯。这不是重点。

我想编写一个小实用程序,在麦克风或摄像机在使用时发出通知。我对知道什么应用程序正在使用它没有任何兴趣。我只想知道麦克风/摄像头是打开还是关闭。

这是给我作为父母的。我在想我可以得到其中一个变色 LED 灯,然后当相机/麦克风打开时,我的应用程序可以检测到它,然后向灯发送信号以改变颜色。然后当我的一个孩子走进来时,他们会看到灯是“红色的”(意思是,请勿打扰),他们会知道我正在参加电话会议。

【问题讨论】:

  • 这能回答你的问题吗? How to detect microphone usage on OS X?
  • 我也有同样的想法。想问这个人是否会开源他的应用程序objective-see.com/products/oversight.html
  • 其实,也许只是能够从 Oversight 解析日志来得到我们需要的东西
  • 我最近对How to detect microphone usage on OS X的回答给出了一个可以检测麦克风状态的PyObjC代码sn-p。对于获取摄像头状态,有一个等效的 sn-p 代码,但我也推荐is-camera-on-cli,它可以轻松安装并提供 CLI 以及 API 来检测摄像头。
  • 您可以使用/usr/bin/log stream --predicate 'eventMessage contains "Post event kCameraStream"' 来检查网络摄像头的状态。我刚刚创建了github.com/henrik242/OnAir,它围绕它使用 MQTT 消息打开/关闭灯泡到我的智能家居设置。

标签: macos video-conferencing


【解决方案1】:

我有几乎完全相同的问题要解决。这是我的原型解决方案。它监视 AppleCamera 进程的线程数。在测试的macbook上,线程的基数似乎是3。当应用程序使用摄像头时,计数增加到4。我也计划实现麦克风检查。我确信我的代码可以更紧凑,我可以将 shell 命令简化为单行,但我更喜欢可读性。

import subprocess
import pywemo

DEVICE_NAME = "BatSignal"


def count_camera_threads():
    command = ["pgrep", "AppleCamera"]
    process = subprocess.run(command, capture_output=True, text=True)
    pid = process.stdout.replace("\n", "")
    command = ["ps", "M", pid]
    process = subprocess.run(command, capture_output=True, text=True)
    lines = process.stdout
    count = len(lines.splitlines()) - 2
    return count


def get_device(name):
    devices = pywemo.discover_devices()
    for device in devices:
        if device.name == name:
            return device
    return None


if __name__ == "__main__":
    device = get_device(DEVICE_NAME)
    if device is None:
        exit(f"Unable to find '{DEVICE_NAME}' on network")
    while True:
        if count_camera_threads() > 3:
            device.on()
        else:
            device.off()
        time.sleep(1)

【讨论】:

    猜你喜欢
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 2014-09-18
    • 2014-02-24
    相关资源
    最近更新 更多