pyvirtualcam使用OBS虚拟摄像头

思路

安装一个虚拟摄像头,播放视频来替换摄像头内容,用代码控制播放的内容。

打包通过python打包,同时控制obs的安装。

步骤

OBS虚拟摄像头安装

卸载和安装OBS虚拟摄像头的脚本

obs-studio\data\obs-plugins\win-dshow\virtualcam-uninstall.bat

安装是virtualcam-install

背景图placeholder.png,替换后要重启

Pyinstaller

import cv2
import os
import random
import pyvirtualcam

class FakeCamera:
    def __init__(self, video_file, root_path):
        
        self.number = len(video_file)
        self.root = root_path

        self.video_file = video_file

        self.video = cv2.VideoCapture(self.root + self.video_file[random.randint(0, self.number - 1)])

    def read(self):
        ret, frame = self.video.read()
        if ret:
            return ret, frame
        else:
            # is video is done, select a new video file randomly
            self.video = cv2.VideoCapture(self.root + self.video_file[random.randint(0, self.number - 1)])
            # is video is done, restart the video
            self.video.set(cv2.CAP_PROP_POS_FRAMES, 0)
            
            return self.video.read()

    def release(self):
        self.video.release()


# main funciton
def play_fake_camera():
    video_file = os.listdir('./video/')  # video path
    root_path = './video/'
    fake_camera = FakeCamera(video_file, root_path)
    print('Running fake camera')

    w = 1280
    h = 576

    # create virtual camera
    with pyvirtualcam.Camera(width=w, height=h, fps=24) as cam:
        while True:
            ret, frame = fake_camera.read()
            if not ret:
                break
            # turn frame to BGR
            img = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
            
            # send frame to virtual camera
            cam.send(img)

            # camera wait for next frame
            cam.sleep_until_next_frame()
    fake_camera.release()
    cv2.destroyAllWindows()

# run the code
play_fake_camera()

先简单的用pyinstaller打包:pyinstaller -F -w fakecameracv2.py

代码写的video文件夹记得建,放入对应分辨率的视频。

问题

RuntimeError: 'obs' backend: virtual camera output could not be started

检查是否安装,或者是否被其他程序占用

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值