| Index: build/android/screenshot.py
|
| diff --git a/build/android/screenshot.py b/build/android/screenshot.py
|
| index 86607cc0649349b5991cac456cdeb3e7e5e3e8b8..db39c5d35a6a86454222e63cbec44b9e897c5c30 100755
|
| --- a/build/android/screenshot.py
|
| +++ b/build/android/screenshot.py
|
| @@ -4,44 +4,85 @@
|
| # Use of this source code is governed by a BSD-style license that can be
|
| # found in the LICENSE file.
|
|
|
| -"""Takes and saves a screenshot from an Android device.
|
| +"""Takes a screenshot or a screen video capture from an Android device."""
|
|
|
| -Usage: screenshot.py [-s SERIAL] [[-f] FILE]
|
| -
|
| -Options:
|
| - -s SERIAL connect to device with specified SERIAL
|
| - -f FILE write screenshot to FILE (default: Screenshot.png)
|
| -"""
|
| -
|
| -from optparse import OptionParser
|
| +import logging
|
| +import optparse
|
| import os
|
| import sys
|
|
|
| from pylib import android_commands
|
| +from pylib import screenshot
|
| +
|
| +
|
| +def _PrintMessage(heading, eol='\n'):
|
| + sys.stdout.write('%s%s' % (heading, eol))
|
| + sys.stdout.flush()
|
| +
|
| +
|
| +def _CaptureScreenshot(adb, host_file):
|
| + host_file = adb.TakeScreenshot(host_file)
|
| + _PrintMessage('Screenshot written to %s' % os.path.abspath(host_file))
|
| +
|
| +
|
| +def _CaptureVideo(adb, host_file, options):
|
| + size = tuple(map(int, options.size.split('x'))) if options.size else None
|
| + recorder = screenshot.VideoRecorder(adb,
|
| + host_file,
|
| + megabits_per_second=options.bitrate,
|
| + size=size,
|
| + rotate=options.rotate)
|
| + try:
|
| + recorder.Start()
|
| + _PrintMessage('Recording. Press Enter to stop...', eol='')
|
| + raw_input()
|
| + finally:
|
| + recorder.Stop()
|
| + host_file = recorder.Pull()
|
| + _PrintMessage('Video written to %s' % os.path.abspath(host_file))
|
|
|
|
|
| def main():
|
| # Parse options.
|
| - parser = OptionParser(usage='screenshot.py [-s SERIAL] [[-f] FILE]')
|
| - parser.add_option('-s', '--serial', dest='serial',
|
| - help='connect to device with specified SERIAL',
|
| - metavar='SERIAL', default=None)
|
| - parser.add_option('-f', '--file', dest='filename',
|
| - help='write screenshot to FILE (default: %default)',
|
| - metavar='FILE', default='Screenshot.png')
|
| + parser = optparse.OptionParser(description=__doc__,
|
| + usage='screenshot.py [options] [filename]')
|
| + parser.add_option('-d', '--device', metavar='ANDROID_DEVICE', help='Serial '
|
| + 'number of Android device to use.', default=None)
|
| + parser.add_option('-f', '--file', help='Save result to file instead of '
|
| + 'generating a timestamped file name.', metavar='FILE')
|
| + parser.add_option('-v', '--verbose', help='Verbose logging.',
|
| + action='store_true')
|
| + video_options = optparse.OptionGroup(parser, 'Video capture')
|
| + video_options.add_option('--video', help='Enable video capturing. Requires '
|
| + 'Android KitKat or later', action='store_true')
|
| + video_options.add_option('-b', '--bitrate', help='Bitrate in megabits/s, '
|
| + 'from 0.1 to 100 mbps, %default mbps by default.',
|
| + default=4, type='float')
|
| + video_options.add_option('-r', '--rotate', help='Rotate video by 90 degrees.',
|
| + default=False, action='store_true')
|
| + video_options.add_option('-s', '--size', metavar='WIDTHxHEIGHT',
|
| + help='Frame size to use instead of the device '
|
| + 'screen size.', default=None)
|
| + parser.add_option_group(video_options)
|
| +
|
| (options, args) = parser.parse_args()
|
|
|
| - if not options.serial and len(android_commands.GetAttachedDevices()) > 1:
|
| + if options.verbose:
|
| + logging.getLogger().setLevel(logging.DEBUG)
|
| +
|
| + if not options.device and len(android_commands.GetAttachedDevices()) > 1:
|
| parser.error('Multiple devices are attached. '
|
| - 'Please specify SERIAL with -s.')
|
| + 'Please specify device serial number with --device.')
|
|
|
| if len(args) > 1:
|
| parser.error('Too many positional arguments.')
|
| - filename = os.path.abspath(args[0] if args else options.filename)
|
| + host_file = args[0] if args else options.file
|
| + adb = android_commands.AndroidCommands(options.device)
|
|
|
| - # Grab screenshot and write to disk.
|
| - ac = android_commands.AndroidCommands(options.serial)
|
| - ac.TakeScreenshot(filename)
|
| + if options.video:
|
| + _CaptureVideo(adb, host_file, options)
|
| + else:
|
| + _CaptureScreenshot(adb, host_file)
|
| return 0
|
|
|
|
|
|
|