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 and saves a screenshot from an Android device. | 7 """Takes a screenshot or a screen video capture from an Android device.""" |
8 | 8 |
9 Usage: screenshot.py [-s SERIAL] [[-f] FILE] | 9 import logging |
10 | 10 import optparse |
11 Options: | |
12 -s SERIAL connect to device with specified SERIAL | |
13 -f FILE write screenshot to FILE (default: Screenshot.png) | |
14 """ | |
15 | |
16 from optparse import OptionParser | |
17 import os | 11 import os |
18 import sys | 12 import sys |
19 | 13 |
20 from pylib import android_commands | 14 from pylib import android_commands |
| 15 from pylib import screenshot |
| 16 |
| 17 |
| 18 def _PrintMessage(heading, eol='\n'): |
| 19 sys.stdout.write('%s%s' % (heading, eol)) |
| 20 sys.stdout.flush() |
| 21 |
| 22 |
| 23 def _CaptureScreenshot(adb, host_file): |
| 24 host_file = adb.TakeScreenshot(host_file) |
| 25 _PrintMessage('Screenshot written to %s' % os.path.abspath(host_file)) |
| 26 |
| 27 |
| 28 def _CaptureVideo(adb, host_file, options): |
| 29 size = tuple(map(int, options.size.split('x'))) if options.size else None |
| 30 recorder = screenshot.VideoRecorder(adb, |
| 31 host_file, |
| 32 megabits_per_second=options.bitrate, |
| 33 size=size, |
| 34 rotate=options.rotate) |
| 35 try: |
| 36 recorder.Start() |
| 37 _PrintMessage('Recording. Press Enter to stop...', eol='') |
| 38 raw_input() |
| 39 finally: |
| 40 recorder.Stop() |
| 41 host_file = recorder.Pull() |
| 42 _PrintMessage('Video written to %s' % os.path.abspath(host_file)) |
21 | 43 |
22 | 44 |
23 def main(): | 45 def main(): |
24 # Parse options. | 46 # Parse options. |
25 parser = OptionParser(usage='screenshot.py [-s SERIAL] [[-f] FILE]') | 47 parser = optparse.OptionParser(description=__doc__, |
26 parser.add_option('-s', '--serial', dest='serial', | 48 usage='screenshot.py [options] [filename]') |
27 help='connect to device with specified SERIAL', | 49 parser.add_option('-d', '--device', metavar='ANDROID_DEVICE', help='Serial ' |
28 metavar='SERIAL', default=None) | 50 'number of Android device to use.', default=None) |
29 parser.add_option('-f', '--file', dest='filename', | 51 parser.add_option('-f', '--file', help='Save result to file instead of ' |
30 help='write screenshot to FILE (default: %default)', | 52 'generating a timestamped file name.', metavar='FILE') |
31 metavar='FILE', default='Screenshot.png') | 53 parser.add_option('-v', '--verbose', help='Verbose logging.', |
| 54 action='store_true') |
| 55 video_options = optparse.OptionGroup(parser, 'Video capture') |
| 56 video_options.add_option('--video', help='Enable video capturing. Requires ' |
| 57 'Android KitKat or later', action='store_true') |
| 58 video_options.add_option('-b', '--bitrate', help='Bitrate in megabits/s, ' |
| 59 'from 0.1 to 100 mbps, %default mbps by default.', |
| 60 default=4, type='float') |
| 61 video_options.add_option('-r', '--rotate', help='Rotate video by 90 degrees.', |
| 62 default=False, action='store_true') |
| 63 video_options.add_option('-s', '--size', metavar='WIDTHxHEIGHT', |
| 64 help='Frame size to use instead of the device ' |
| 65 'screen size.', default=None) |
| 66 parser.add_option_group(video_options) |
| 67 |
32 (options, args) = parser.parse_args() | 68 (options, args) = parser.parse_args() |
33 | 69 |
34 if not options.serial and len(android_commands.GetAttachedDevices()) > 1: | 70 if options.verbose: |
| 71 logging.getLogger().setLevel(logging.DEBUG) |
| 72 |
| 73 if not options.device and len(android_commands.GetAttachedDevices()) > 1: |
35 parser.error('Multiple devices are attached. ' | 74 parser.error('Multiple devices are attached. ' |
36 'Please specify SERIAL with -s.') | 75 'Please specify device serial number with --device.') |
37 | 76 |
38 if len(args) > 1: | 77 if len(args) > 1: |
39 parser.error('Too many positional arguments.') | 78 parser.error('Too many positional arguments.') |
40 filename = os.path.abspath(args[0] if args else options.filename) | 79 host_file = args[0] if args else options.file |
| 80 adb = android_commands.AndroidCommands(options.device) |
41 | 81 |
42 # Grab screenshot and write to disk. | 82 if options.video: |
43 ac = android_commands.AndroidCommands(options.serial) | 83 _CaptureVideo(adb, host_file, options) |
44 ac.TakeScreenshot(filename) | 84 else: |
| 85 _CaptureScreenshot(adb, host_file) |
45 return 0 | 86 return 0 |
46 | 87 |
47 | 88 |
48 if __name__ == '__main__': | 89 if __name__ == '__main__': |
49 sys.exit(main()) | 90 sys.exit(main()) |
OLD | NEW |