当前位置:   article > 正文

pyvirtualcam使用OBS虚拟摄像头_obs virtualcam

obs virtualcam

思路

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

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

步骤

OBS虚拟摄像头安装

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

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

安装是virtualcam-install

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

Pyinstaller

  1. import cv2
  2. import os
  3. import random
  4. import pyvirtualcam
  5. class FakeCamera:
  6. def __init__(self, video_file, root_path):
  7. self.number = len(video_file)
  8. self.root = root_path
  9. self.video_file = video_file
  10. self.video = cv2.VideoCapture(self.root + self.video_file[random.randint(0, self.number - 1)])
  11. def read(self):
  12. ret, frame = self.video.read()
  13. if ret:
  14. return ret, frame
  15. else:
  16. # is video is done, select a new video file randomly
  17. self.video = cv2.VideoCapture(self.root + self.video_file[random.randint(0, self.number - 1)])
  18. # is video is done, restart the video
  19. self.video.set(cv2.CAP_PROP_POS_FRAMES, 0)
  20. return self.video.read()
  21. def release(self):
  22. self.video.release()
  23. # main funciton
  24. def play_fake_camera():
  25. video_file = os.listdir('./video/') # video path
  26. root_path = './video/'
  27. fake_camera = FakeCamera(video_file, root_path)
  28. print('Running fake camera')
  29. w = 1280
  30. h = 576
  31. # create virtual camera
  32. with pyvirtualcam.Camera(width=w, height=h, fps=24) as cam:
  33. while True:
  34. ret, frame = fake_camera.read()
  35. if not ret:
  36. break
  37. # turn frame to BGR
  38. img = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
  39. # send frame to virtual camera
  40. cam.send(img)
  41. # camera wait for next frame
  42. cam.sleep_until_next_frame()
  43. fake_camera.release()
  44. cv2.destroyAllWindows()
  45. # run the code
  46. play_fake_camera()

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

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

问题

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/832139
推荐阅读
相关标签
  

闽ICP备14008679号