How to capture the screen as fast as possible thro

2019-01-30 15:35发布

问题:

This question already has an answer here:

  • Read binary stdout data from adb shell? 15 answers

Recently, I wrote a PC client which can display and control my android phone screen in real-time using adb. I use the monkey to control the device and it works fine. The problem is how to grab the phone screen and display it smoothly.

The first solution I have come up with is to continually grab the framebuffer through adb (like DDMS's screen capture function). Now when I do it, the performance is quite unacceptable. The frame rate captured from framebuffer is as low as 5 per second (the frame size is 800 * 480). My program looks like its hiccuping when I slide on the phone.

My program is written in java using ddmslib to grab framebuffer.

add:
I found it much slow to encoding the raw framebuffer data into .png format, otherwise this will be a fast way to transmit a compress raw image.

How can I improve the speed of capturing the screen to a smooth level?

回答1:

adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > screen.png

Source:

http://blog.shvetsov.com/2013/02/grab-android-screenshot-to-computer-via.html



回答2:

You could use:

$ adb shell screencap -p > sc.png

which unfortunately doesn't work (file is corrupted), so your option left is

$ adb shell screencap -p /mnt/sdcard/sc.png
$ adb pull /mnt/sdcard/sc.png

while this is relatively fast, it's probably not as fast as you need.



回答3:

if the slow part is raw to png conversion (time adb shell screencap -p /sdcard/x.png is considerably slower than time adb shell screencap /sdcard/nonpng.raw, as I have it in games) and png is not necessary, then you can do like here https://github.com/sowcow/shot. It converts raw data to simple ppm format and converts ppm to bmp using imagemagick with almost no overhead after it gets raw data

upd:

This shell script by max_plenert is a better example:

adb shell screencap /sdcard/mytmp/rock.raw
adb pull /sdcard/mytmp/rock.raw
adb shell rm /sdcard/mytmp/rock.raw

// remove the header
tail -c +13 rock.raw > rock.rgba

// extract width height and pixelformat:
hexdump -e '/4 "%d"' -s 0 -n 4 rock.raw
hexdump -e '/4 "%d"' -s 4 -n 4 rock.raw
hexdump -e '/4 "%d"' -s 8 -n 4 rock.raw

convert -size 480x800 -depth 8 rock.rgba rock.png


回答4:

In .bash_profile on my mac:

function droidShot() {
  /full_path_to_droid_sdk/platform-tools/adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > $1.png
}

usage:

droidshot whatever

and the file shows up in whatever.png



回答5:

Using this method, my app continues to run at it's full framerate

adb pull /dev/graphics/fb0

This will pull down the raw data from the frame buffer. It seems to be the same width as the screen, but the height is padded out. The frame buffer on my device seems to be in RGBA8888 format. To get exactly what is displayed on screen, you should ignore the alpha channel.

Note that this does take ~0.8 seconds to download for me (resoultion: 480x854) but if i have it downloading on a continuous loop, it doesn't affect the device's frame rate.



回答6:

This a slow solution, i am sure it is the faster way you may find in adb based solutions,

# this line require a superuser privilege:
adb shell "su -c 'cat /dev/graphics/fb0 > /sdcard/capture.raw'"

adb pull /sdcard/capture.raw

ffmpeg -loglevel panic -f rawvideo -pix_fmt bgr32 -s 320x480 -i \ 
    capture.raw -vcodec png -vframes 1 capture.png; 



回答7:

From docs :

Taking a device screenshot

The screencap command is a shell utility for taking a screenshot of a device display. While in a shell, the syntax is:

screencap <filename>

To use the screencap from the command line, type the following:

$ adb shell screencap /sdcard/screen.png

Here's an example screenshot session, using the adb shell to capture the screenshot and the pull command to download the file from the device:

$ adb shell
shell@ $ screencap /sdcard/screen.png
shell@ $ exit
$ adb pull /sdcard/screen.png

Recording a device screen

The screenrecord command is a shell utility for recording the display of devices running Android 4.4 (API level 19) and higher. The utility records screen activity to an MPEG-4 file.

Note: Audio is not recorded with the video file.

A developer can use this file to create promotional or training videos. While in a shell, the syntax is:

screenrecord [options] <filename>

To use screenrecord from the command line, type the following:

$ adb shell screenrecord /sdcard/demo.mp4

Stop the screen recording by pressing Ctrl-C, otherwise the recording stops automatically at three minutes or the time limit set by --time-limit.

To begin recording your device screen, run the screenrecord command to record the video. Then, run the pull command to download the video from the device to the host computer. Here's an example recording session:

$ adb shell
shell@ $ screenrecord --verbose /sdcard/demo.mp4
(press Ctrl-C to stop)
shell@ $ exit
$ adb pull /sdcard/demo.mp4

The screenrecord utility can record at any supported resolution and bit rate you request, while retaining the aspect ratio of the device display. The utility records at the native display resolution and orientation by default, with a maximum length of three minutes.

There are some known limitations of the screenrecord utility that you should be aware of when using it:

Some devices may not be able to record at their native display resolution. If you encounter problems with screen recording, try using a lower screen resolution. Rotation of the screen during recording is not supported. If the screen does rotate during recording, some of the screen is cut off in the recording.



回答8:

I have improved on @steveha's solution:

Create a shell script say: 'android-screenshot' with the following contents:

adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > screen-`date +%Y-%m-%d_%X`.png

Place this file in your local bin directory or any other $PATH you prefer.

This would allow you to save the files with a timestamp in the current directory.

Remember to make the shell script executable using

chmod +x android-screenshot


回答9:

Well some people said in Perl there is many ways to do one thing. :-) Anyway, this one may perform better (since there is no regular expression), and may resolve the issue in Cygwin in MS Windows (whose Perl define \n as CRLF):

adb shell screencap -p | perl -pe 'BEGIN { $/="\cM\cJ"; $\="\cJ"; } chomp;' > screen-$(date +%Y%m%d_%H%M%S).png


回答10:

There is a screenrecord program as of Android 4.4

adb shell screenrecord /data/local/tmp/test-video.m4v
adb pull /data/local/tmp/test-video.m4v ~/test-video.m4v


回答11:

adb shell screenrecord --bit-rate 8000000 --time-limit 30 /sdcard/example.mp4

You can change the bit rate and change the video duration also.



回答12:

adb shell screencap /sdcard/screen.png && adb pull /sdcard/screen.png my_screen01.png


标签: android adb