Search code examples
pythonadbscreenshot

compress adb screencap on python


Yes, I can get the png stream from the shell

adb exec-out screencap -p

python code like this

start_time = time()

pipe = subprocess.Popen("adb exec-out screencap -p", stdout=subprocess.PIPE, shell=True)
img_bytes = pipe.stdout.read()
read_time = time()

img = cv2.imdecode(np.frombuffer(img_bytes, np.uint8), cv2.IMREAD_COLOR)
end_time = time()

print('stream size', len(img_bytes))
print('read cost', read_time - start_time)
print('decode cost', end_time - read_time)
print('screencap cost', end_time - start_time)

Yet it is still too slow

size 2452217
read cost 2.630615234375
decode cost 0.0625
screencap cost 2.693115234375

Can I compress the screencap before output ?


Solution

  • If your device supports it, try using:

    adb exec-out screenrecord --output-format=h264 -
    

    This will instead output a h264 movie stream, which will be compressed significantly less demanding on both the adb host and mobile device.

    For more information, check out Use adb screenrecord command to mirror Android screen to PC via USB

    Additionally, there are multiple arguments you can specify as well:

    --help  Displays command syntax and options
    --size WidthxHeight     Sets the video size: 1280x720. The default value is the device's native display resolution (if supported), 1280x720 if not. For best results, use a size supported by your device's Advanced Video Coding (AVC) encoder.
    --bit-rate rate     Sets the video bit rate for the video, in megabits per second. The default value is 4Mbps.
    --time-limit time   Sets the maximum recording time, in seconds. The default and maximum value is 180 (3 minutes).
    --rotate    Rotates the output 90 degrees. This feature is experimental.
    --verbose   Displays log information on the command-line screen. If you do not set this option, the utility does not display any information while running.
    

    Source: https://developer.android.com/studio/command-line/adb#screenrecord