1 #!/usr/bin/env python 2 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 5 # found in the LICENSE file. 6 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 13 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)) 43 44 45 def main(): 46 # Parse options. 47 parser = optparse.OptionParser(description=__doc__, 48 usage='screenshot.py [options] [filename]') 49 parser.add_option('-d', '--device', metavar='ANDROID_DEVICE', help='Serial ' 50 'number of Android device to use.', default=None) 51 parser.add_option('-f', '--file', help='Save result to file instead of ' 52 'generating a timestamped file name.', metavar='FILE') 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 68 (options, args) = parser.parse_args() 69 70 if options.verbose: 71 logging.getLogger().setLevel(logging.DEBUG) 72 73 if not options.device and len(android_commands.GetAttachedDevices()) > 1: 74 parser.error('Multiple devices are attached. ' 75 'Please specify device serial number with --device.') 76 77 if len(args) > 1: 78 parser.error('Too many positional arguments.') 79 host_file = args[0] if args else options.file 80 adb = android_commands.AndroidCommands(options.device, api_strict_mode=True) 81 82 if options.video: 83 _CaptureVideo(adb, host_file, options) 84 else: 85 _CaptureScreenshot(adb, host_file) 86 return 0 87 88 89 if __name__ == '__main__': 90 sys.exit(main()) 91