-
Notifications
You must be signed in to change notification settings - Fork 4
/
obssocket.py
191 lines (169 loc) · 6.51 KB
/
obssocket.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/python3
import obsws_python as obsws
#import base64
import numpy as np
from PIL import Image
import traceback, os, io
import logging, logging.handlers
import base64
os.makedirs('log', exist_ok=True)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
hdl = logging.handlers.RotatingFileHandler(
f'log/{os.path.basename(__file__).split(".")[0]}.log',
encoding='utf-8',
maxBytes=1024*1024*2,
backupCount=1,
)
hdl.setLevel(logging.DEBUG)
hdl_formatter = logging.Formatter('%(asctime)s %(filename)s:%(lineno)5d %(funcName)s() [%(levelname)s] %(message)s')
hdl.setFormatter(hdl_formatter)
logger.addHandler(hdl)
class OBSSocket():
def __init__(self,hostIP,portNum,passWord,inf_source,dst_screenshot):
self.host = hostIP
self.port = portNum
self.passwd = passWord
self.picw=1920
self.pich=1080
self.inf_source = inf_source
self.dst_screenshot = dst_screenshot
self.ws = obsws.ReqClient(host=self.host,port=self.port,password=self.passwd)
self.active = True
self.ev = obsws.EventClient(host=self.host,port=self.port,password=self.passwd)
self.ev.callback.register([self.on_exit_started,])
logger.debug(f'host:{self.host}, port:{self.port}, pass:{self.passwd}')
def close(self):
try:
del self.ws
return True
except Exception:
logger.debug(traceback.format_exc())
return False
def change_scene(self,name:str):
try:
self.ws.set_current_program_scene(name)
except Exception:
pass
def get_scenes(self):
try:
res = self.ws.get_scene_list()
ret = res.scenes
return res.scenes
except Exception:
logger.debug(traceback.format_exc())
return []
def get_sources(self, scene):
ret = []
try:
allitem = self.ws.get_scene_item_list(scene).scene_items
for x in allitem:
if x['isGroup']:
grp = self.ws.get_group_scene_item_list(x['sourceName']).scene_items
for y in grp:
ret.append(y['sourceName'])
ret.append(x['sourceName'])
except Exception:
logger.debug(traceback.format_exc())
ret.reverse()
return ret
def change_text(self, source, text):
try:
res = self.ws.set_input_settings(source, {'text':text}, True)
except Exception:
logger.debug(traceback.format_exc())
def save_screenshot(self):
#logger.debug(f'dst:{self.dst_screenshot}')
try:
res = self.ws.save_source_screenshot(self.inf_source, 'png', self.dst_screenshot, self.picw, self.pich, 100)
return res
except Exception:
logger.debug(traceback.format_exc())
return False
def save_screenshot_dst(self, dst):
try:
res = self.ws.save_source_screenshot(self.inf_source, 'png', dst, self.picw, self.pich, 100)
return res
except Exception:
logger.debug(traceback.format_exc())
return False
# 設定されたソースを取得し、PIL.Image形式で返す
def get_screenshot(self):
b = self.ws.get_source_screenshot(self.inf_source, 'jpeg', self.picw, self.pich, 100).image_data
b = b.split(',')[1]
c = base64.b64decode(b) # バイナリ形式のはず?
tmp = io.BytesIO(c)
img = Image.open(tmp)
return img
def enable_source(self, scenename, sourceid): # グループ内のitemはscenenameにグループ名を指定する必要があるので注意
try:
res = self.ws.set_scene_item_enabled(scenename, sourceid, enabled=True)
return True
except Exception as e:
return False
def disable_source(self, scenename, sourceid):
try:
res = self.ws.set_scene_item_enabled(scenename, sourceid, enabled=False)
return True
except Exception as e:
return False
def refresh_source(self, sourcename):
try:
self.ws.press_input_properties_button(sourcename, 'refreshnocache')
except Exception:
pass
def on_exit_started(self, _):
print("OBS closing!")
self.active = False
self.ev.unsubscribe()
def search_itemid(self, scene, target):
ret = scene, None # グループ名, ID
try:
allitem = self.ws.get_scene_item_list(scene).scene_items
for x in allitem:
if x['sourceName'] == target:
ret = scene, x['sceneItemId']
if x['isGroup']:
grp = self.ws.get_group_scene_item_list(x['sourceName']).scene_items
for y in grp:
if y['sourceName'] == target:
ret = x['sourceName'], y['sceneItemId']
except:
pass
return ret
def get_scene_collection_list(self):
"""OBSに設定されたシーンコレクションの一覧をListで返す
Returns:
list: シーンコレクション名の文字列
"""
try:
return self.ws.get_scene_collection_list().scene_collections
except Exception:
logger.debug(traceback.format_exc())
return []
def set_scene_collection(self, scene_collection:str):
"""シーンコレクションを引数で指定したものに変更する。
Args:
scene_collection (str): シーンコレクション名
Returns:
bool: 成功ならTrue,失敗したらFalse
"""
try:
self.ws.set_current_scene_collection(scene_collection)
return True
except Exception:
logger.debug(traceback.format_exc())
return False
if __name__ == "__main__":
#a = OBSSocket('localhost', '4455', 'panipaninoakuma','たぬきお休み.png','tmp.png')
#a.save_screenshot('メインモニタ', 'png', 'C:\\Users\\katao\\OneDrive\\デスクトップ\\hoge.png')
#tmp = a.get_screenshot('メインモニタ', 'png')
#print(a.search_itemid('2. DP_NEW', 'history_cursong'))
#for i in range(100):
# img = a.get_screenshot()# image_data # base64文字列
#img.save('tmp.png')
a = OBSSocket('localhost', '4455', 'panipaninoakuma','ゲーム画面_FHD','tmp.png')
a.picw=2560
a.pich=1440
b = a.get_screenshot()
b.save('tmp.png')