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

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.0

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.0
File Size Uploaded
adb_scr_py-0.3.0.tar.gz 147.2 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for adb-scr-py 0.3.0
File
adb_scr_py-0.3.0-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.0-cp314-cp314-macosx_15_0_arm64.whl CPython 3.14 CPython 3.14 macOS 15.0+ ARM64 Details
adb_scr_py-0.3.0-cp313-cp313-macosx_15_0_arm64.whl CPython 3.13 CPython 3.13 macOS 15.0+ ARM64 Details
adb_scr_py-0.3.0-cp312-cp312-macosx_15_0_arm64.whl CPython 3.12 CPython 3.12 macOS 15.0+ ARM64 Details
adb_scr_py-0.3.0-cp311-cp311-macosx_15_0_arm64.whl CPython 3.11 CPython 3.11 macOS 15.0+ ARM64 Details
adb_scr_py-0.3.0-cp310-cp310-macosx_15_0_arm64.whl CPython 3.10 CPython 3.10 macOS 15.0+ ARM64 Details

Total release size: 1.1 MB

Release files / adb_scr_py-0.3.0.tar.gz

Download URL adb_scr_py-0.3.0.tar.gz
Size 147.2 kB
Tags Source
SHA-256 checksum
How to use checksums
9af6fcd29acfedf69736121b9b7afcba03bf05847c19639df0bed5423fc0cab0
BLAKE2b-256 checksum
How to use checksums
8b1ee30a9cd13938eb3fd8d583a83e7baed566c93befd5da459ce3b64e30e577
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.0-cp314-cp314t-macosx_15_0_arm64.whl

Download URL adb_scr_py-0.3.0-cp314-cp314t-macosx_15_0_arm64.whl
Size 156.0 kB
Tags CPython 3.14 CPython 3.14 free-threading macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
cbfb1421838a48b676a184f879235538f123c4e34f60e03217ce8beff606f574
BLAKE2b-256 checksum
How to use checksums
cccb3622160ef125cdb71eeac4f98ee34550f789d5de310e2e0698e075587f95
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.0-cp314-cp314-macosx_15_0_arm64.whl

Download URL adb_scr_py-0.3.0-cp314-cp314-macosx_15_0_arm64.whl
Size 155.9 kB
Tags CPython 3.14 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
d79683a926b5f9beb492fa775c492b8e8a607dfab5b29fe45bbc7b69cedc9a77
BLAKE2b-256 checksum
How to use checksums
a72b06e6dabb8a0d40693f7cb0e4616c4792d2d00026686a9ff912b39aae1634
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.0-cp313-cp313-macosx_15_0_arm64.whl

Download URL adb_scr_py-0.3.0-cp313-cp313-macosx_15_0_arm64.whl
Size 155.9 kB
Tags CPython 3.13 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
b929dd1e6c4626fb8693016a5582234d4ca53af0626c0892021b097e98221c1f
BLAKE2b-256 checksum
How to use checksums
5e566e32b9cd765928778453831ae9edec31949d3925d3ce2f43f272c7615c9c
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.0-cp312-cp312-macosx_15_0_arm64.whl

Download URL adb_scr_py-0.3.0-cp312-cp312-macosx_15_0_arm64.whl
Size 155.9 kB
Tags CPython 3.12 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
6c1d7c291b22f7420484e8749d6ca192c6a8368a3671b293e40ee16ef46a3148
BLAKE2b-256 checksum
How to use checksums
572e6e758098b5861bbadf54b59f612f75b1b73fbd59134c93dd13875f97422c
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.0-cp311-cp311-macosx_15_0_arm64.whl

Download URL adb_scr_py-0.3.0-cp311-cp311-macosx_15_0_arm64.whl
Size 156.0 kB
Tags CPython 3.11 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
fa45bd9b7da777b5993ca62fa8fd9ae089dcb83a99b438703bf31fb405612ae5
BLAKE2b-256 checksum
How to use checksums
418ee0731ba49bcd1aa6670b7d961f26aa700701a6b4569e28393bcf3e362ffa
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.0-cp310-cp310-macosx_15_0_arm64.whl

Download URL adb_scr_py-0.3.0-cp310-cp310-macosx_15_0_arm64.whl
Size 156.0 kB
Tags CPython 3.10 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
4d033295ffcb67e552c9745c8a91efef708991536b13413d3cd7921f292deadf
BLAKE2b-256 checksum
How to use checksums
91f22ab24edcd1292d8f880af35a0fafabad620c70611e9b2ba1ea9d47539be0
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

0.3.1

13 release files

This release

0.3.0 This release

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