用Python脚本PyInstaller打造轻量级scrcpy启动器含文件传输在Android开发与调试过程中scrcpy无疑是一款神器——它能够通过USB或Wi-Fi将手机屏幕投射到电脑上并支持低延迟的鼠标键盘操作。但原生的scrcpy命令行工具缺乏一些实用功能比如文件传输而且每次启动都需要输入复杂的参数。市面上虽然有不少GUI封装工具但往往体积臃肿功能冗余。本文将带你用Python脚本封装scrcpy的核心功能并通过PyInstaller打包成独立的可执行文件。最终产物体积不到7MB却集成了投屏、录屏、文件传输等实用功能完全可以替代那些50MB的GUI工具。1. 为什么需要自定义scrcpy启动器scrcpy本身已经非常优秀但在日常使用中仍有一些痛点缺乏文件传输功能虽然adb本身支持文件传输但每次都需要手动输入命令启动参数复杂息屏模式、录屏模式等需要记忆不同的参数组合依赖命令行非技术用户可能对命令行感到畏惧GUI工具臃肿第三方GUI封装往往体积庞大且功能不一定符合个人需求我们的Python脚本方案具有以下优势特性原生scrcpy第三方GUI我们的方案文件传输❌ 不支持✅ 支持✅ 支持体积~15MB~50MB~7MB启动便捷性需要命令行一键启动一键启动可定制性低中等高依赖项需要adb环境自带adb自带adb2. 核心功能设计我们的启动器将实现以下核心功能基础投屏模式普通显示触控操作息屏模式手机屏幕关闭但仍可操作录屏功能自动保存为带时间戳的mp4文件文件传输通过拖放操作将文件传输到手机2.1 项目结构准备在开始编码前需要准备好以下目录结构scrcpy_launcher/ ├── main.py # 主脚本文件 ├── adb.exe # adb工具 ├── scrcpy.exe # scrcpy主程序 └── file/ # 录屏保存目录提示确保scrcpy和adb的可执行文件与脚本放在同一目录下这样可以避免路径问题。3. Python脚本实现下面是完整的Python实现代码我们分段解析关键部分import os from datetime import datetime import subprocess def send_command(cmd): 执行adb/scrcpy命令 subprocess.Popen(cmd, shellTrue, stdinsubprocess.PIPE, stdoutsubprocess.PIPE, stderrsubprocess.PIPE) def transfer_file(): 处理文件传输 while True: filepath input(拖入要传输的文件) if not filepath: break filename os.path.basename(filepath) cmd f.\\adb.exe push \{filepath}\ /sdcard/Download/scrcpy/{filename} os.system(cmd)这段代码定义了两个核心函数send_command()用于执行scrcpy命令transfer_file()处理文件传输逻辑支持拖放操作3.1 模式选择与执行def normal_mode(): print(普通投屏模式) send_command(scrcpy) transfer_file() def screen_off_mode(): print(息屏投屏模式) send_command(scrcpy -S) transfer_file() def record_mode(): print(录屏模式) timestamp datetime.now().strftime(%Y%m%d_%H%M%S) send_command(fscrcpy -r file/{timestamp}.mp4) transfer_file() def record_screen_off_mode(): print(息屏录屏模式) timestamp datetime.now().strftime(%Y%m%d_%H%M%S) send_command(fscrcpy -S -r file/{timestamp}.mp4) transfer_file()每种模式都对应不同的scrcpy参数组合普通模式scrcpy息屏模式scrcpy -S录屏模式scrcpy -r 文件名息屏录屏组合使用-S和-r参数3.2 用户交互与主逻辑if __name__ __main__: print(scrcpy启动器 v1.0) print(请选择模式:) print(1. 普通模式) print(2. 息屏模式) print(3. 录屏模式) print(4. 息屏录屏模式) choice input(输入数字选择(默认2): ).strip() if choice 1: normal_mode() elif choice 3: record_mode() elif choice 4: record_screen_off_mode() else: # 默认选择息屏模式 screen_off_mode()4. 使用PyInstaller打包将Python脚本打包成exe可以方便分享和使用以下是详细步骤4.1 安装PyInstallerpip install pyinstaller4.2 打包配置创建打包配置文件build.spec# build.spec block_cipher None a Analysis([main.py], pathex[.], binaries[], datas[(adb.exe, .), (scrcpy.exe, .)], hiddenimports[], hookspath[], runtime_hooks[], excludes[], win_no_prefer_redirectsFalse, win_private_assembliesFalse, cipherblock_cipher, noarchiveFalse) pyz PYZ(a.pure, a.zipped_data, cipherblock_cipher) exe EXE(pyz, a.scripts, [], exclude_binariesTrue, namescrcpy_launcher, debugFalse, bootloader_ignore_signalsFalse, stripFalse, upxTrue, consoleTrue) coll COLLECT(exe, a.binaries, a.zipfiles, a.datas, stripFalse, upxTrue, namescrcpy_launcher)关键配置说明datas将adb.exe和scrcpy.exe包含在最终打包文件中upxTrue使用UPX压缩可执行文件减小体积consoleTrue保留控制台窗口方便调试4.3 执行打包pyinstaller --onefile --clean build.spec打包完成后你会在dist目录下得到scrcpy_launcher.exe文件体积通常在6-8MB之间。5. 高级功能扩展基础功能已经足够日常使用但我们可以进一步扩展5.1 添加更多adb功能def adb_shell(): 进入adb shell os.system(.\\adb.exe shell) def screenshot(): 截图并保存到电脑 timestamp datetime.now().strftime(%Y%m%d_%H%M%S) os.system(f.\\adb.exe exec-out screencap -p screenshot_{timestamp}.png) print(f截图已保存为 screenshot_{timestamp}.png)5.2 支持无线连接def wireless_connect(): 切换到无线连接模式 ip input(输入手机IP地址: ) port input(输入端口(默认5555): ) or 5555 # 先通过USB连接执行adb tcpip os.system(f.\\adb.exe tcpip {port}) # 断开USB连接 os.system(f.\\adb.exe connect {ip}:{port}) print(f已连接到 {ip}:{port}现在可以断开USB线)5.3 配置文件支持添加config.ini支持自定义参数[default] resolution 1280 bitrate 8M max_fps 60然后在Python代码中读取配置import configparser config configparser.ConfigParser() config.read(config.ini) resolution config.get(default, resolution, fallback1280) bitrate config.get(default, bitrate, fallback8M) max_fps config.get(default, max_fps, fallback60)6. 实际使用技巧经过几个月的实际使用我总结出一些实用技巧文件传输优化在手机Download目录创建scrcpy文件夹所有传输文件都放在这里方便管理录屏管理定期清理file目录中的录屏文件避免占用过多空间无线连接在家使用时可以设置为无线模式摆脱线缆束缚快捷键组合scrcpy本身支持很多快捷键如Ctrlo返回Ctrlh主页Ctrls切换息屏模式遇到无法触控的问题时可以尝试以下adb命令修复adb shell pm grant com.android.shell android.permission.WRITE_SECURE_SETTINGS这个轻量级启动器已经成为了我日常开发的必备工具特别是它的文件传输功能大大提升了工作效率。相比那些庞大的GUI工具这个6MB的小程序反而更能满足我的实际需求。