Skip to content

Instantly share code, notes, and snippets.

@bryanberger
Last active February 14, 2024 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanberger/187074d7f39299061a7e13cf14b0cb7f to your computer and use it in GitHub Desktop.
Save bryanberger/187074d7f39299061a7e13cf14b0cb7f to your computer and use it in GitHub Desktop.
Captures the screen of an Android device using an adb command and then pulls the recorded file and removes it
#!/bin/bash
echo "$(basename $0)"
KEEP_ON_PHONE=false
FILE_NAME="screen_recording_$(date +"%Y-%m-%d_%H-%M")"
TARGET_DIR_PHONE="//sdcard"
TARGET_DIR_COMP="$(pwd)"
printUsage() {
echo "Usage: adb-record [-k] [-f <FILE_NAME>] [-p <TARGET_DIR_PHONE>] [-c <TARGET_DIR_COMP>]"
echo " -k KEEP_ON_PHONE: Keep the video file on the phone. Default '${KEEP_ON_PHONE}'."
echo " -f FILE_NAME: Name of the video file. No extension. Default screen_recording_<date>_<time>.mp4."
echo " -p TARGET_DIR_PHONE: Target directory on the phone. Default '${TARGET_DIR_PHONE}'."
echo " -c TARGET_DIR_COMP: Target directory on the computer. Default pwd."
}
while getopts "hkf:p:c:" OPT; do
case "${OPT}" in
k)
KEEP_ON_PHONE=true
;;
f)
FILE_NAME="${OPTARG}"
;;
p)
TARGET_DIR_PHONE="${OPTARG}"
;;
c)
TARGET_DIR_COMP="${OPTARG}"
;;
*)
printUsage
exit 1
;;
esac
done
FULL_PATH_PHONE="${TARGET_DIR_PHONE}/${FILE_NAME}.mp4"
FULL_PATH_COMP="${TARGET_DIR_COMP}/${FILE_NAME}.mp4"
mkdir -p "${TARGET_DIR_COMP}"
function finish() {
# Check if the screen recording process is running
local screenRecordPID=$(adb shell pidof screenrecord)
if [ -n "$screenRecordPID" ]; then
echo "Screen recording process (PID: $screenRecordPID) is still running. Waiting for it to finish..."
adb shell wait $screenRecordPID
echo "Screen recording process has finished."
else
echo "Screen recording process is not running."
fi
# Pull the recorded screen video from the device
adb pull "${FULL_PATH_PHONE}" "${FULL_PATH_COMP}"
# Optionally remove the screen recording file from the device
if ! ${KEEP_ON_PHONE} ; then
adb shell "rm '${FULL_PATH_PHONE}'"
fi
}
trap finish SIGINT
echo "Press Ctrl+C to stop screen recording."
adb shell "screenrecord '${FULL_PATH_PHONE}'"
@bryanberger
Copy link
Author

Modified version of https://github.com/starikcetin/adb-helpers/blob/master/adb-record.sh that waits for the screenrecord to complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment