Skip to main content

adb_scr_py

通过 ADB 和 scrcpy 控制 Android 设备,并使用 macOS VideoToolbox 解码屏幕视频。Python 导入名为 adb_scr

支持 USB 和网络调试连接、点击/滑动/长按/粘贴、单指及多指手势、应用启动与停止、按需 JPEG 截图,以及 H.264/AAC MP4 屏幕录制。内部保留 BGRA8 原始帧通路,供 NumPy/OpenCV 消费;截图和录制都直接使用原生解码帧。

安装

  • Python 3.10 或更高版本;本地开发使用普通 3.14。支持 free-threaded CPython,已验证 3.14t;需要对应 ABI 的构建,详见 Python 兼容性
  • macOS,使用 VideoToolbox 硬件解码器。源码支持构建 arm64/x86_64,具体 wheel 可用性以发布文件为准。
  • 已安装 ADB,设备已授权 USB 调试,或已具备 ADB 网络调试条件。
uv add adb_scr_py

快速开始

将示例序列号替换为自己的设备。网络设备使用 AndroidDevice("192.168.1.100:5555", "tcp")

import asyncio
from pathlib import Path

from adb_scr import AndroidDevice, deinit_lib, init_lib, set_screen_record_fps


async def main() -> None:
    await init_lib()
    device = AndroidDevice("YOUR_DEVICE_SERIAL", "usb")
    try:
        set_screen_record_fps(30)  # 后续启动会话的上限,不改变正在运行的会话
        if not await device.connect():
            print(device.last_disconnect_reason)
            return
        print(device.get_screen_size())  # connect 已等待有效的视频元数据

        # 解码首帧可能晚于连接成功;使用截止时间,而不是固定 sleep 猜测。
        deadline = asyncio.get_running_loop().time() + 5
        while device.is_connected:
            jpg = await device.get_screenshot_jpg(quality=90)
            if jpg is not None:
                await asyncio.to_thread(Path("screenshot.jpg").write_bytes, jpg)
                break
            if asyncio.get_running_loop().time() >= deadline:
                print("等待首帧超时")
                break
            await asyncio.sleep(0.05)
    finally:
        try:
            await device.disconnect()
        finally:
            await deinit_lib()


asyncio.run(main())

截图、裁剪与缩放

get_screenshot_jpg() 支持可选的质量、缩放比例和原图裁剪区域:

jpeg = await device.get_screenshot_jpg()  # quality=75, scale=1.0, roi=None
jpeg = await device.get_screenshot_jpg(
    quality=85, scale=0.5, roi=(100, 200, 600, 400)
)  # 裁剪原图区域,再缩小为 300 × 200

ROI 使用原图左上角坐标 (x, y, width, height),必须完全位于图内;None 表示整图。先裁剪再缩放,比例可大于 1。三个参数都有默认值,无参数或只传质量的现有调用无需修改;参数范围、取整和异常见 API 文档

连接与断连

from adb_scr import AndroidDevice, ConnectionOptions

options = ConnectionOptions(
    connect_timeout=30,
    io_timeout=5,
    close_timeout=5,
    probe_interval=5,
    probe_failures=3,
)
device = AndroidDevice("YOUR_DEVICE_SERIAL", "usb", options=options)

EOF、接收/发送失败、服务端进程退出或设备存活探测连续失败会触发会话清理。await device.wait_disconnected() 等待清理完成并返回原因;device.is_connected 可读取当前状态。库不自动重连,调用方可在设备恢复后再次 await device.connect()

静态画面可能没有新视频帧,控制上行也可能长期无数据,因此不设置普通空闲读取超时。默认每 5 秒通过设备端 shell true 探测 transport,连续 3 次失败后断开;probe_interval=None 可禁用。探测不能证明编码器持续产帧,也不是精确的断连检测时限。

屏幕录制

连接成功且首帧已经解码后:

await device.start_recording("capture.mp4")
try:
    await asyncio.sleep(5)  # 此期间仍可截图或操作设备
finally:
    await device.stop_recording()  # 等待 MP4 封装完成后再读取文件

开始时使用缓存画面立即生成关键帧,停止时补足静止画面的持续时间;录制期间没有新视频包也能生成有效文件。视频使用 VideoToolbox 硬件 H.264 编码,码率和画质采用系统默认设置;音频直接封装手机回传的 AAC,不重新编码。输出尺寸固定为首帧尺寸,旋转后等比适配并居中留黑。不覆盖已有文件,也不自动创建父目录。

连接时自动读取 Android API 级别并开启支持的音源:Android 13+ 使用 playback + audio_dup 保留手机播放声音;Android 11–12L 使用 output,采集期间手机静音(从连接开始,即使尚未录制);Android 11 启动时还需要解锁屏幕。Android 10 及以下不启用音频。服务端明确禁用音频时保留视频连接,开始录制会记录警告并生成纯视频文件。应用可以限制音频采集,系统行为见 scrcpy 音频说明

每台设备同时最多一份录制,重复开始会报错;重复停止安全。断连会自动结束录制,异步写入错误可通过 stop_recording() 获取。接口异常与时间精度见 录制 API

MCP 服务

可选的 adb_scr_mcp 包提供 FastAPI + Streamable HTTP 服务,lifespan 自动初始化 库,并在 Ctrl+C/SIGTERM 时等待设备、录制和 ADB 清理完成。启动后由客户端显式连接设备。

uv run --python 3.14 --locked --extra mcp adb-scr-mcp --port 8000

客户端地址为 http://127.0.0.1:8000/mcp。提供设备枚举/连接、JPEG 截图、触控、 应用控制及 MP4 录制工具。安装、参数和退出流程见 MCP 服务文档

API 索引

入口 用途
await init_lib(adb_path=None) 初始化共享 ADB daemon
await list_devices() 获取 ADB 列出的序列号
set_screen_record_fps(fps) 设置后续会话的帧率上限
ConnectionOptions(...) 配置连接、I/O、关闭及存活探测
AndroidDevice(...) 设备会话、截图及控制 API
await device.start_recording(output_file) / await device.stop_recording() 开始录制及等待 MP4 文件完成
GestureAction / GestureActionNode 通过 pointer_id 区分手指的单指/多指手势序列
await deinit_lib() 所有设备关闭后停止共享 daemon

完整签名、参数单位、失败行为和示例见 API 参考(仓库内见 docs/api.md)。原生 API 的类型及说明随包内 .pyi 提供。

使用边界

  • 一个设备实例应在同一 asyncio 事件循环内使用。设备锁和解码器锁保护各自操作;并非所有公开方法共用一把锁,也不提供跨事件循环共享保证。
  • connect() 成功表示流和尺寸就绪,截图仍可能因为首帧未到而返回 None
  • 控制方法返回不代表手机 UI 已执行动作;断连时不能保证手势抬起消息送达。
  • 设备用完后显式 await disconnect(),再 await deinit_lib()。停止全局 daemon 会影响其他 ADB 客户端。
  • 网络操作超时会开始取消与清理,实际返回可能稍晚。原生销毁等待硬件和队列完成,没有强制释放在用内存的超时。
  • 不提供固定截图吞吐保证或历史帧回放。录制从调用时的缓存画面开始,不包含调用前的历史视频。

开发文档

架构 · 控制流程与 BGRA8 · Python 兼容性

仓库开发文档与测试不进入 sdist/wheel。项目通过 setuptools 构建原生扩展;CMake 仅用于 IDE 索引。

许可证与依赖

MIT License。屏幕传输使用 scrcpy;媒体处理使用 Apple VideoToolbox、AVFoundation/CoreMedia、AudioToolbox、Accelerate/vImage、ImageIO/CoreGraphics 和 Core Image/Metal。

Release files for adb-scr-py 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for adb-scr-py 0.3.1
File Size Uploaded
adb_scr_py-0.3.1.tar.gz 158.7 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for adb-scr-py 0.3.1
File
adb_scr_py-0.3.1-cp314-cp314t-macosx_26_0_arm64.whl CPython 3.14 CPython 3.14 free-threading macOS 26.0+ ARM64 Details
adb_scr_py-0.3.1-cp314-cp314t-macosx_15_0_arm64.whl CPython 3.14 CPython 3.14 free-threading macOS 15.0+ ARM64 Details
adb_scr_py-0.3.1-cp314-cp314-macosx_26_0_arm64.whl CPython 3.14 CPython 3.14 macOS 26.0+ ARM64 Details
adb_scr_py-0.3.1-cp314-cp314-macosx_15_0_arm64.whl CPython 3.14 CPython 3.14 macOS 15.0+ ARM64 Details
adb_scr_py-0.3.1-cp313-cp313-macosx_26_0_arm64.whl CPython 3.13 CPython 3.13 macOS 26.0+ ARM64 Details
adb_scr_py-0.3.1-cp313-cp313-macosx_15_0_arm64.whl CPython 3.13 CPython 3.13 macOS 15.0+ ARM64 Details
adb_scr_py-0.3.1-cp312-cp312-macosx_26_0_arm64.whl CPython 3.12 CPython 3.12 macOS 26.0+ ARM64 Details
adb_scr_py-0.3.1-cp312-cp312-macosx_15_0_arm64.whl CPython 3.12 CPython 3.12 macOS 15.0+ ARM64 Details
adb_scr_py-0.3.1-cp311-cp311-macosx_26_0_arm64.whl CPython 3.11 CPython 3.11 macOS 26.0+ ARM64 Details
adb_scr_py-0.3.1-cp311-cp311-macosx_15_0_arm64.whl CPython 3.11 CPython 3.11 macOS 15.0+ ARM64 Details
adb_scr_py-0.3.1-cp310-cp310-macosx_26_0_arm64.whl CPython 3.10 CPython 3.10 macOS 26.0+ ARM64 Details
adb_scr_py-0.3.1-cp310-cp310-macosx_15_0_arm64.whl CPython 3.10 CPython 3.10 macOS 15.0+ ARM64 Details

Total release size: 2.2 MB

Release files / adb_scr_py-0.3.1.tar.gz

Download URL adb_scr_py-0.3.1.tar.gz
Size 158.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4bdeeb5ab2dc5381c1379f5a5c34c76418cc6270ca15032cf8e2b812a5104286
BLAKE2b-256 checksum
How to use checksums
27b2f749b1c1ce1bd7c0e989c8ab335dafc67c480cb32442ca48f57b60062e8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release files / adb_scr_py-0.3.1-cp314-cp314t-macosx_26_0_arm64.whl

Download URL adb_scr_py-0.3.1-cp314-cp314t-macosx_26_0_arm64.whl
Size 169.2 kB
Tags CPython 3.14 CPython 3.14 free-threading macOS 26.0+ ARM64
SHA-256 checksum
How to use checksums
711017297fc2ec9a2f684ba926d81aaf54bb290fbf23cc11d2dd5c0462ecbded
BLAKE2b-256 checksum
How to use checksums
e74d9cae0c411b8cc6cbd84d34047d5545f453417995aeeb0d49f66bfb5e1cf1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release files / adb_scr_py-0.3.1-cp314-cp314t-macosx_15_0_arm64.whl

Download URL adb_scr_py-0.3.1-cp314-cp314t-macosx_15_0_arm64.whl
Size 169.1 kB
Tags CPython 3.14 CPython 3.14 free-threading macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
0aa4470475aa7133b880e105cb6654cec97e5e10f91de2b6ff7ec0585a86dcc8
BLAKE2b-256 checksum
How to use checksums
0bf903ff20d090cca1a6bcfd3954e750a3d5422c90bd455913f09146e74eb8df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release files / adb_scr_py-0.3.1-cp314-cp314-macosx_26_0_arm64.whl

Download URL adb_scr_py-0.3.1-cp314-cp314-macosx_26_0_arm64.whl
Size 169.1 kB
Tags CPython 3.14 macOS 26.0+ ARM64
SHA-256 checksum
How to use checksums
a923175b565932d35fcc0ff226e08e0ba1f15fa41597a5202a5021fbf108930d
BLAKE2b-256 checksum
How to use checksums
fe3c3e224a19d686cb8521d3356e8a573321d20c1deb6647ec6f8eda5843889c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release files / adb_scr_py-0.3.1-cp314-cp314-macosx_15_0_arm64.whl

Download URL adb_scr_py-0.3.1-cp314-cp314-macosx_15_0_arm64.whl
Size 169.1 kB
Tags CPython 3.14 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
cc1a1f9ea40e75f75627f1d0faa711cdcc8eb41aa7ab0a0327d260fd502c9e69
BLAKE2b-256 checksum
How to use checksums
c4f68d9e75345dbffc277549841bbced6cd731bbaeecb942b545504f102f52f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release files / adb_scr_py-0.3.1-cp313-cp313-macosx_26_0_arm64.whl

Download URL adb_scr_py-0.3.1-cp313-cp313-macosx_26_0_arm64.whl
Size 169.1 kB
Tags CPython 3.13 macOS 26.0+ ARM64
SHA-256 checksum
How to use checksums
aae6f02b7837b0216223979859e0e0dd83699ac4c8917edf829a04e81bd58452
BLAKE2b-256 checksum
How to use checksums
4b7258825f56563ff012c08e5b7f0f5ba6213beae3f0031a1cb5d6452372a428
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release files / adb_scr_py-0.3.1-cp313-cp313-macosx_15_0_arm64.whl

Download URL adb_scr_py-0.3.1-cp313-cp313-macosx_15_0_arm64.whl
Size 169.1 kB
Tags CPython 3.13 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
ce4c57e910f69ba8f552a81c47c33121d968ca586cb9beec0d5e2ee10399bc9b
BLAKE2b-256 checksum
How to use checksums
cf86ee96e30d7b0ccccf8bec7c5e7fc0d9ee0cc806b7333c4359af5a1ff91bd5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release files / adb_scr_py-0.3.1-cp312-cp312-macosx_26_0_arm64.whl

Download URL adb_scr_py-0.3.1-cp312-cp312-macosx_26_0_arm64.whl
Size 169.1 kB
Tags CPython 3.12 macOS 26.0+ ARM64
SHA-256 checksum
How to use checksums
02e202418037ea56785e50613b6c65c3c9e1639a939e0f85eb5d366394b6c367
BLAKE2b-256 checksum
How to use checksums
27ea9b52835ada98ff31154f77777bf78f3fbff3b1a8412b5b7cd42f9b06db79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release files / adb_scr_py-0.3.1-cp312-cp312-macosx_15_0_arm64.whl

Download URL adb_scr_py-0.3.1-cp312-cp312-macosx_15_0_arm64.whl
Size 169.1 kB
Tags CPython 3.12 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
c4278c8ca43fcc5fdb60e841f28278ca8e9ba5bd7bba6d75bd32cf5fac956328
BLAKE2b-256 checksum
How to use checksums
adcf99b528acf635b8ac815930c71e016d63fe0f8d918b5c7169fdb9aea26d2c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release files / adb_scr_py-0.3.1-cp311-cp311-macosx_26_0_arm64.whl

Download URL adb_scr_py-0.3.1-cp311-cp311-macosx_26_0_arm64.whl
Size 169.1 kB
Tags CPython 3.11 macOS 26.0+ ARM64
SHA-256 checksum
How to use checksums
241c2ac1fdd282338a397ff252a29c4ea3d72b5b153e3bc2056dd158460bfbda
BLAKE2b-256 checksum
How to use checksums
072a0a2c211f41691f8572b65f72abecf8b068b376cb7b4b66298c614314f175
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release files / adb_scr_py-0.3.1-cp311-cp311-macosx_15_0_arm64.whl

Download URL adb_scr_py-0.3.1-cp311-cp311-macosx_15_0_arm64.whl
Size 169.1 kB
Tags CPython 3.11 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
ad9d346416132e9055acc8cb45d153dca1b4035a04b88b80d6c6e0ad22ffc636
BLAKE2b-256 checksum
How to use checksums
4ea219598ab4d69873fae50741c853e0b02866e487be11e520caf8e510911dc0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release files / adb_scr_py-0.3.1-cp310-cp310-macosx_26_0_arm64.whl

Download URL adb_scr_py-0.3.1-cp310-cp310-macosx_26_0_arm64.whl
Size 169.1 kB
Tags CPython 3.10 macOS 26.0+ ARM64
SHA-256 checksum
How to use checksums
f67e3ec198d5e900a02178f3bf43b24d2d00e2183b56fe25bb291fd02bd7f43b
BLAKE2b-256 checksum
How to use checksums
cd4af7e901a71bfc464d64dc3ad0172a53cdd3623ac96b6498e5c291d7321868
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release files / adb_scr_py-0.3.1-cp310-cp310-macosx_15_0_arm64.whl

Download URL adb_scr_py-0.3.1-cp310-cp310-macosx_15_0_arm64.whl
Size 169.1 kB
Tags CPython 3.10 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
b51768823f46bf9e95069e28f108689ad3e6595292f896c870681b6fe998c997
BLAKE2b-256 checksum
How to use checksums
d05e1bb75c55e795561455fd393353d9b4eb4fee6b9b1068ee1e3e8a6ce11f15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release history Release notifications | RSS feed

0.4.0

21 release files

0.3.2

13 release files

This release

0.3.1 This release

13 release files

0.3.0

7 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page