You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何高效直接访问并修改OBS用户的音视频流?

直接对接OBS的高效实时流处理方案

1. OBS WebSocket API(Python友好的直接对接方案)

OBS 28及以上版本默认内置obs-websocket插件,无需额外安装即可直接通过API控制OBS的源、获取帧数据,完全绕开RTMP中转服务器。你可以实时抓取指定源的帧,处理后通过虚拟摄像头回传给OBS,或直接修改OBS的输出配置。

依赖安装

pip install obs-websocket-py opencv-python numpy pyvirtualcam

代码示例

import obswebsocket
from obswebsocket import requests
import cv2
import numpy as np
import base64
import pyvirtualcam

# 连接OBS WebSocket(需在OBS设置中开启WebSocket并设置密码)
client = obswebsocket.obsws("localhost", 4455, "your_obs_ws_password")
client.connect()

def get_obs_source_frame(source_name):
    # 请求获取指定源的截图
    screenshot_resp = client.call(requests.TakeSourceScreenshot(
        sourceName=source_name,
        imageFormat="png"
    ))
    # 解码base64数据为OpenCV帧
    img_data = base64.b64decode(screenshot_resp["imageData"])
    nparr = np.frombuffer(img_data, np.uint8)
    return cv2.imdecode(nparr, cv2.IMREAD_COLOR)

def process_frame(frame):
    # 示例处理:添加实时水印
    cv2.putText(
        frame,
        "Processed in Python",
        (50, 50),
        cv2.FONT_HERSHEY_SIMPLEX,
        1,
        (0, 255, 0),
        2
    )
    return frame

# 将处理后的帧推送到虚拟摄像头,OBS中添加虚拟摄像头作为输入源
with pyvirtualcam.Camera(width=1920, height=1080, fps=30) as cam:
    while True:
        raw_frame = get_obs_source_frame("Video Capture Device")
        processed_frame = process_frame(raw_frame)
        # 转换为pyvirtualcam要求的RGB格式
        cam.send(cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB))
        cam.sleep_until_next_frame()

client.disconnect()

2. 虚拟摄像头双向捕获(极简本地方案)

让OBS将输出流发送到虚拟摄像头,Python直接从虚拟摄像头读取帧处理,再输出到另一个虚拟摄像头供OBS使用。全程本地流转,延迟极低,无需任何网络依赖。

代码示例

import cv2
import pyvirtualcam

# 从OBS虚拟摄像头读取帧(设备编号根据实际情况调整)
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
cap.set(cv2.CAP_PROP_FPS, 30)

# 输出处理后的帧到新虚拟摄像头
with pyvirtualcam.Camera(width=1920, height=1080, fps=30) as cam:
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        # 示例处理:高斯模糊
        processed_frame = cv2.GaussianBlur(frame, (15,15), 0)
        cam.send(cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB))
        cam.sleep_until_next_frame()

cap.release()

3. OBS内置Python脚本(极致性能方案)

通过OBS的obs-python插件,直接在OBS进程内运行Python脚本,在帧渲染回调中实时修改帧数据。全程无外部进程交互,性能拉满,适合低延迟要求的场景。

代码示例(OBS脚本)

import obspython as obs
import numpy as np
import cv2

def script_description():
    return "OBS内部实时帧处理脚本"

def script_load(settings):
    # 注册视频滤镜回调
    obs.obs_register_source_video_filter(create_frame_filter, filter_props)

def create_frame_filter(settings, source):
    filter = obs.obs_source_create_private("video_filter", "Python Frame Processor", settings)
    obs.obs_source_set_video_filter_callback(filter, process_video_frame)
    return filter

def filter_props(props):
    return props

def process_video_frame(filter, frame):
    # 从OBS帧中提取数据转为numpy数组
    data = obs.obs_frame_get_data(frame)
    width = obs.obs_frame_get_width(frame)
    height = obs.obs_frame_get_height(frame)
    linesize = obs.obs_frame_get_linesize(frame, 0)
    
    img = np.ndarray(
        shape=(height, width, 3),
        dtype=np.uint8,
        buffer=data,
        strides=(linesize, 3, 1)
    )
    
    # 示例处理:颜色反转
    img = cv2.bitwise_not(img)
    
    # 将修改后的数据写回OBS帧
    obs.obs_frame_set_data(frame, img.tobytes())
    return frame

使用方式:将脚本放入OBS的Scripts文件夹,在OBS脚本面板启用后,添加到目标视频源的滤镜列表中。

方案对比

  • WebSocket方案:灵活性高,适合外部Python程序跨设备控制OBS,延迟较低,上手简单。
  • 虚拟摄像头方案:零配置成本,纯本地流转,延迟极小,适合快速验证。
  • 内置脚本方案:性能最优,无外部依赖,适合对延迟和稳定性要求极高的场景。

内容的提问来源于stack exchange,提问作者dumbQuestions

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 03:02:38