'''
Scrcpy多线程投屏
脚本说明
除了支持单设备同时投屏,也支持多个设备同时投屏
运行失败,优先在cmd中输入adb devices检查设备是否正常连接
'''
import subprocess
import threading
def start_scrcpy(device_id):
try :
# 直接在Python脚本中启动scrcpy进程
process = subprocess.Popen([ 'scrcpy' , '-s' , device_id], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
# 这里可以添加代码来监控进程的状态或者输出
except Exception as e:
print (f "Failed to start scrcpy for device {device_id}: {e}" )
def get_device_ids():
result = subprocess.run([ 'adb' , 'devices' ], stdout = subprocess.PIPE, text = True )
device_ids = []
for line in result.stdout.splitlines():
if '\tdevice' in line:
device_id = line.split( '\t' )[ 0 ].strip()
if device_id:
device_ids.append(device_id)
return device_ids
def main():
device_ids = get_device_ids()
if len (device_ids) = = 0 :
print ( "No devices connected." )
else :
# 为每个设备启动一个线程来运行scrcpy
threads = []
for device_id in device_ids:
thread = threading.Thread(target = start_scrcpy, args = (device_id,))
thread.start()
threads.append(thread)
# 可选:等待所有线程完成(在这个场景中可能不需要,因为scrcpy进程是长期的)
# for thread in threads:
# thread.join()
main()
# if __name__ == "__main__":
# main()
|