-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrcpyConnection.py
More file actions
300 lines (295 loc) · 7.09 KB
/
Copy pathScrcpyConnection.py
File metadata and controls
300 lines (295 loc) · 7.09 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# 请帮我写个中文的 Python 脚本,批注也是中文:
# 无线 Scrcpy 启动脚本:通过 ADB 连接本地或远程设备,然后启动 Scrcpy 投屏。
# 命令类似:scrcpy -s 100.64.0.3:41153 --video-bit-rate=2M --max-fps=30 --video-codec=h265 --audio-codec=opus --keyboard=uhid --mouse=sdk --stay-awake
# 导入模块
import sys
import subprocess
# ==================== 全局配置 ====================
WIRELESS_IPS = [
"100.64.0.3",
"192.168.0.195",
]
SCRCPY_VIDEO_BITRATE = "2M"
SCRCPY_MAX_FPS = "30"
SCRCPY_VIDEO_CODEC = "h265"
SCRCPY_AUDIO_CODEC = "opus"
SCRCPY_KEYBOARD = "uhid"
SCRCPY_MOUSE = "sdk"
# ==================== 消息 ====================
TITLE = "Scrcpy 启动工具"
# ==================== subprocess 通用函数 ====================
def run_command(cmd, timeout=15):
"""
执行外部命令。
统一使用 UTF-8,
避免 Windows GBK 编码导致异常。
"""
return subprocess.run(
cmd,
capture_output=True,
text=True,
encoding="utf-8",
errors="replace",
timeout=timeout,
)
# ==================== ADB ====================
def run_adb_devices():
"""
获取 adb 设备列表。
"""
print("正在检查已连接设备...")
result = run_command(
["adb", "devices"],
timeout=10,
)
stdout = result.stdout or ""
stderr = result.stderr or ""
if result.returncode != 0:
raise RuntimeError(
stderr.strip()
)
print(stdout.strip())
return stdout
def adb_connect(address):
"""
连接无线 ADB。
"""
print(
f"正在连接 ADB 设备:{address}"
)
result = run_command(
[
"adb",
"connect",
address,
],
timeout=15,
)
stdout = (
result.stdout or ""
).strip()
stderr = (
result.stderr or ""
).strip()
if stdout:
print(stdout)
if stderr:
print(stderr)
def get_usb_device(devices):
"""
获取 USB ADB 设备。
排除:
IP:PORT 类型的无线设备。
"""
for line in devices.splitlines():
line = line.strip()
if not line:
continue
if line.startswith(
"List of devices"
):
continue
parts = line.split()
if len(parts) >= 2:
serial = parts[0]
state = parts[1]
if (
state == "device"
and ":" not in serial
):
return serial
return None
def is_device_online(address, devices):
"""
判断无线设备是否在线。
"""
for line in devices.splitlines():
if line.startswith(address):
if "\tdevice" in line:
return True
return False
# ==================== 用户选择 ====================
def choose_mode():
"""
选择连接模式。
"""
print()
print("请选择连接方式:")
print()
print("1. USB 本地连接(默认)")
print("2. ADB 无线连接")
print()
while True:
choice = input(
"请输入 (1/2,直接回车默认1):"
).strip()
if choice == "":
return "usb"
if choice == "1":
return "usb"
if choice == "2":
return "wifi"
print(
"输入错误,请重新输入。"
)
def choose_wireless_device():
"""
选择无线设备 IP。
返回 IP 地址(不含端口)。
"""
print()
print("请选择无线设备 IP:")
print()
for index, ip in enumerate(
WIRELESS_IPS,
start=1
):
print(
f"{index}. {ip}"
)
print()
while True:
choice = input(
"请输入编号(默认1):"
).strip()
if choice == "":
return WIRELESS_IPS[0]
if choice.isdigit():
index = int(choice) - 1
if (
0 <= index <
len(WIRELESS_IPS)
):
return WIRELESS_IPS[index]
print(
"输入错误,请重新输入。"
)
def ask_port():
"""
询问 ADB 端口号。
"""
print()
while True:
port = input(
"请输入 ADB 端口号(默认 41153):"
).strip()
if port == "":
return "41153"
if port.isdigit() and (
1 <= int(port) <= 65535
):
return port
print(
"无效端口号,请输入 1-65535。"
)
# ==================== Scrcpy ====================
def run_scrcpy(serial):
"""
启动 Scrcpy。
"""
print(
"正在启动 Scrcpy..."
)
cmd = [
"scrcpy",
"-s",
serial,
"--video-bit-rate",
SCRCPY_VIDEO_BITRATE,
"--max-fps",
SCRCPY_MAX_FPS,
"--video-codec",
SCRCPY_VIDEO_CODEC,
"--audio-codec",
SCRCPY_AUDIO_CODEC,
"--keyboard",
SCRCPY_KEYBOARD,
"--mouse",
SCRCPY_MOUSE,
"--stay-awake",
"--turn-screen-off",
]
result = subprocess.run(cmd)
if result.returncode == 0:
print(
"Scrcpy 已退出。"
)
else:
print(
f"Scrcpy 返回码:{result.returncode}"
)
# ==================== 主程序 ====================
def main():
print("=" * 50)
print(TITLE)
print("=" * 50)
mode = choose_mode()
# ----------------------------
# USB 本地连接
# ----------------------------
if mode == "usb":
devices = run_adb_devices()
serial = get_usb_device(
devices
)
if serial is None:
print(
"未找到 USB ADB 设备。"
)
return
# ----------------------------
# 无线连接
# ----------------------------
else:
ip = choose_wireless_device()
port = ask_port()
address = f"{ip}:{port}"
adb_connect(address)
devices = run_adb_devices()
if is_device_online(
address,
devices,
):
print(
f"设备已就绪:{address}"
)
else:
print(
"警告:设备未确认在线,继续尝试。"
)
serial = address
print(
f"使用设备:{serial}"
)
run_scrcpy(serial)
# ==================== 程序入口 ====================
if __name__ == "__main__":
if hasattr(
sys.stdout,
"reconfigure"
):
sys.stdout.reconfigure(
encoding="utf-8",
errors="replace",
)
try:
main()
except KeyboardInterrupt:
print(
"\n用户中断程序。"
)
except FileNotFoundError as e:
print(
f"\n找不到程序:{e.filename}"
)
except Exception as e:
print(
f"\n程序运行错误:{e}"
)
finally:
try:
input(
"\n按回车键退出..."
)
except EOFError:
pass