思路
安装一个虚拟摄像头,播放视频来替换摄像头内容,用代码控制播放的内容。
打包通过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
检查是否安装,或者是否被其他程序占用