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