| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 | 2 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 4 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 6 | 5 |
| 7 """Takes a screenshot or a screen video capture from an Android device.""" | |
| 8 | |
| 9 import logging | |
| 10 import optparse | |
| 11 import os | |
| 12 import sys | 6 import sys |
| 13 | 7 |
| 14 from devil.android import device_blacklist | 8 from devil.android.tools import screenshot |
| 15 from devil.android import device_errors | |
| 16 from devil.android import device_utils | |
| 17 from pylib import screenshot | |
| 18 | |
| 19 def _PrintMessage(heading, eol='\n'): | |
| 20 sys.stdout.write('%s%s' % (heading, eol)) | |
| 21 sys.stdout.flush() | |
| 22 | |
| 23 | |
| 24 def _CaptureScreenshot(device, host_file): | |
| 25 host_file = device.TakeScreenshot(host_file) | |
| 26 _PrintMessage('Screenshot written to %s' % os.path.abspath(host_file)) | |
| 27 | |
| 28 | |
| 29 def _CaptureVideo(device, host_file, options): | |
| 30 size = (tuple(int(i) for i in options.size.split('x')) if options.size | |
| 31 else None) | |
| 32 recorder = screenshot.VideoRecorder(device, | |
| 33 megabits_per_second=options.bitrate, | |
| 34 size=size, | |
| 35 rotate=options.rotate) | |
| 36 try: | |
| 37 recorder.Start() | |
| 38 _PrintMessage('Recording. Press Enter to stop...', eol='') | |
| 39 raw_input() | |
| 40 finally: | |
| 41 recorder.Stop() | |
| 42 host_file = recorder.Pull(host_file) | |
| 43 _PrintMessage('Video written to %s' % os.path.abspath(host_file)) | |
| 44 | |
| 45 | |
| 46 def main(): | |
| 47 # Parse options. | |
| 48 parser = optparse.OptionParser(description=__doc__, | |
| 49 usage='screenshot.py [options] [filename]') | |
| 50 parser.add_option('-d', '--device', metavar='ANDROID_DEVICE', help='Serial ' | |
| 51 'number of Android device to use.', default=None) | |
| 52 parser.add_option('--blacklist-file', help='Device blacklist JSON file.') | |
| 53 parser.add_option('-f', '--file', help='Save result to file instead of ' | |
| 54 'generating a timestamped file name.', metavar='FILE') | |
| 55 parser.add_option('-v', '--verbose', help='Verbose logging.', | |
| 56 action='store_true') | |
| 57 video_options = optparse.OptionGroup(parser, 'Video capture') | |
| 58 video_options.add_option('--video', help='Enable video capturing. Requires ' | |
| 59 'Android KitKat or later', action='store_true') | |
| 60 video_options.add_option('-b', '--bitrate', help='Bitrate in megabits/s, ' | |
| 61 'from 0.1 to 100 mbps, %default mbps by default.', | |
| 62 default=4, type='float') | |
| 63 video_options.add_option('-r', '--rotate', help='Rotate video by 90 degrees.', | |
| 64 default=False, action='store_true') | |
| 65 video_options.add_option('-s', '--size', metavar='WIDTHxHEIGHT', | |
| 66 help='Frame size to use instead of the device ' | |
| 67 'screen size.', default=None) | |
| 68 parser.add_option_group(video_options) | |
| 69 | |
| 70 (options, args) = parser.parse_args() | |
| 71 | |
| 72 if len(args) > 1: | |
| 73 parser.error('Too many positional arguments.') | |
| 74 host_file = args[0] if args else options.file | |
| 75 | |
| 76 if options.verbose: | |
| 77 logging.getLogger().setLevel(logging.DEBUG) | |
| 78 | |
| 79 blacklist = (device_blacklist.Blacklist(options.blacklist_file) | |
| 80 if options.blacklist_file | |
| 81 else None) | |
| 82 | |
| 83 devices = device_utils.DeviceUtils.HealthyDevices(blacklist) | |
| 84 if options.device: | |
| 85 device = next((d for d in devices if d == options.device), None) | |
| 86 if not device: | |
| 87 raise device_errors.DeviceUnreachableError(options.device) | |
| 88 else: | |
| 89 if len(devices) > 1: | |
| 90 parser.error('Multiple devices are attached. ' | |
| 91 'Please specify device serial number with --device.') | |
| 92 elif len(devices) == 1: | |
| 93 device = devices[0] | |
| 94 else: | |
| 95 raise device_errors.NoDevicesError() | |
| 96 | |
| 97 if options.video: | |
| 98 _CaptureVideo(device, host_file, options) | |
| 99 else: | |
| 100 _CaptureScreenshot(device, host_file) | |
| 101 return 0 | |
| 102 | |
| 103 | 9 |
| 104 if __name__ == '__main__': | 10 if __name__ == '__main__': |
| 105 sys.exit(main()) | 11 sys.exit(screenshot.main()) |
| OLD | NEW |