python+adb realizes screen capture/recording function

When testing some IoT products using the Android system, but the system interface is not as convenient to operate as a mobile phone, you can consider using the adb command to capture/record the screen of the test product, adb shell screenrecord can realize the screen recording function of the Android system Then use the screencap command. View help command, parameter --help

adb shell screenrecord --help

Parameter Description:

  1. Start recording command:

adb shell screenrecord /sdcard/demo.mp4

Note: Record the screen of the mobile phone, the video format is mp4, and it is stored in the SD card of the mobile phone. The default recording time is 180s.

screenrecord is a shell command that supports Android4.4 (API level 19) and above, supports video format: mp4

2. Specify the video resolution size, parameter --size

adb shell screenrecord --size 1280*720 /sdcard/demo.mp4

Note: Record video with a resolution of 1280*720. If you do not specify the resolution of the mobile phone by default, for the best results, please use the size supported by Advanced Video Coding (AVC) on the device

3. Specify the bit rate of the video, parameter --bit-rate

adb shell screenrecord --bit-rate 6000000 /sdcard/demo.mp4

Description: Specify the bit rate of the video to be 6Mbps, if not specified, the default is 4Mbps. You can increase the bit rate to improve video quality or reduce the bit rate to make the file smaller

4. Rotate 90 degrees, parameter: --rotate

adb shell screenrecord --rotate /sdcard/demo.mp4

Note: This function is experimental. It is tested on the nexus6 device. When the recorded video is played, it is also rotated by 90 degrees. The experience is not very friendly.

5. Export video:

adb pull /sdcard/demo.mp4 D:/

Note: The location of the exported video is in the root directory of the D disk, and the name is demo.mp4

It can also be combined with python to implement the code as follows:

import os
import datetime
import subprocess


class ADB:
    def __init__(self, device, target_path):
        self.target_path = target_path
        subprocess.Popen(f"adb connect {device}")   # adb连接安卓设备

    def screenshot(self):
        # 使用 && 隔开。
        file_name = datetime.datetime.now().strftime("%Y-%m-%d %H-%M-%S")
        step1 = rf"cd {self.target_path}"
        step2 = f"adb shell screencap /sdcard/{file_name}.png"
        step3 = f"adb pull sdcard/{file_name}.png ./"   # 将截屏图片传到电脑设备
        command = fr"{step1} && {step2} && {step3}"
        os.popen(command)

    def video(self, record_time):   # 录制时间单位为秒
        # 采集
        file_name = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
        step1 = rf"cd {self.target_path}"
        step2 = f"adb shell screenrecord  --time-limit {record_time} /sdcard/{file_name}.mp4"
        command = fr"{step1} && {step2}"
        os.system(command)

        # 拉取录屏文件到电脑设备
        step4 = rf"adb pull sdcard/{file_name}.mp4 ./"
        command2 = fr"{step1} && {step4}"
        os.system(command2)


if __name__ == '__main__':
    deviceIp = "10.32.3.51"
    target_path = r"C:\Users\DM\Desktop\录屏视频"
    adb = ADB(deviceIp, target_path)
    # adb.screenshot()
    adb.video(60)

Supongo que te gusta

Origin blog.csdn.net/qq_38571773/article/details/128817480
Recomendado
Clasificación