Skip to content

Instantly share code, notes, and snippets.

@ZevEisenberg
Last active October 14, 2015 14:17
Show Gist options
  • Save ZevEisenberg/9808997 to your computer and use it in GitHub Desktop.
Save ZevEisenberg/9808997 to your computer and use it in GitHub Desktop.
Functions to take a screen video recording or screenshot on the currently-connected Android device and drop it on your Mac’s Desktop.
function androidScreenshot
{
# only do this if there is a device connected
adb shell exit 2>/dev/null
if [[ $? == 0 ]]; then
# Example filename: ~/Desktop/Android Screen 2013-09-11 12.32.16 PM.png
# perl line ending hacks from http://blog.shvetsov.com/2013/02/grab-android-screenshot-to-computer-via.html
dateString=`date +"%Y-%m-%d at %I.%M.%S %p"`
fileName="Android Screen $dateString.png"
adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > ~/Desktop/$fileName
else
echo "No android device connected."
fi
}
function androidVideo
{
function unsetLocalFunctions
{
unset -f unsetLocalFunctions
unset -f androidVideoPrintUsage
unset -f copyAndCleanUpVideo
}
function androidVideoPrintUsage
{
echo "Usage: adroidVideo [duration]"
echo
echo "Options:"
echo " -h Help"
echo " duration Record the video for this many seconds. Must be a positive integer."
echo
unsetLocalFunctions
return 0
}
if [[ $1 == "-h" ]]; then
echo "printing usage 2"
androidVideoPrintUsage
fi
local keepLooping=true
function copyAndCleanUpVideo
{
local thePath=$1
local theName=$2
# perl line ending hacks from http://blog.shvetsov.com/2013/02/grab-android-screenshot-to-computer-via.html
local deviceName=`adb shell getprop ro.product.model | perl -pe 's/\x0D\x0A/\x0A/g'`
echo "Copying video from your $deviceName..."
sleep 1
adb pull $thePath/$theName ~/Desktop/$theName > /dev/null 2>&1
adb shell rm $thePath/$theName
trap - SIGINT
keepLooping=false
echo "Done."
unsetLocalFunctions
return 0
}
# only do this if there is a device connected
adb shell exit 2>/dev/null
if [[ $? == 0 ]]; then
local dateString=`date +"%Y-%m-%d at %I.%M.%S %p"`
local fileName="Android Video $dateString.mp4"
local filePath=/sdcard/Movies
local bitRate=8000000 # 8 megabits
local regex='^[0-9]+$'
if [[ -z $1 ]]; then
# no parameters, so run until stopped
stty -echoctl # suppress ^C from being printed
adb shell screenrecord $filePath/$fileName > /dev/null &| # the pid of this background process will be $!
trap "kill -SIGINT $!; copyAndCleanUpVideo '$filePath' '$fileName'; stty echoctl" SIGINT
echo "Recording screen. Use Ctrl-C to stop recording."
while $keepLooping; do
done
elif [[ $1 =~ $regex && $1 != 0 ]]; then # $1 is an integer, so record the video
echo "Recording video..."
adb shell screenrecord --bit-rate $bitRate --time-limit $1 $filePath/$fileName > /dev/null &|
for n in {$1..0}; do
if [[ $n == 1 ]]; then
local timeUnit="second"
else
local timeUnit="seconds"
fi
echo -en "\r$n $timeUnit remaining."
sleep 1
done
echo -en "\r"
copyAndCleanUpVideo "$filePath" "$fileName"
else
echo "Duration is in seconds, and must be a positive integer."
echo
echo "printing usage 1"
androidVideoPrintUsage
return 1
fi
else
echo "No android device connected."
unsetLocalFunctions
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment