Skip to content

Instantly share code, notes, and snippets.

@PaulKinlan
Last active May 26, 2022 11:20
Show Gist options
  • Star 62 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save PaulKinlan/2fdb0c8a6b6f6a646f87 to your computer and use it in GitHub Desktop.
Save PaulKinlan/2fdb0c8a6b6f6a646f87 to your computer and use it in GitHub Desktop.
Screen Record for Android
#! /bin/bash
mkdir -p ./backgrounds
function get_google_device_art {
local device=$1
# Get the Google Device backgrounds
curl "https://developer.android.com/distribute/marketing-tools/device-art-resources/$1/port_back.png" > "./backgrounds/$1_port_back.png"
curl "https://developer.android.com/distribute/marketing-tools/device-art-resources/$1/port_fore.png" > "./backgrounds/$1_port_fore.png"
curl "https://developer.android.com/distribute/marketing-tools/device-art-resources/$1/port_shadow.png" > "./backgrounds/$1_port_shadow.png"
}
get_google_device_art 'nexus_5x'
get_google_device_art 'nexus_6'
get_google_device_art 'nexus_6p'
get_google_device_art 'nexus_9'
get_google_device_art 'pixel'
get_google_device_art 'pixel_xl'
get_google_device_art 'pixel_2'
get_google_device_art 'pixel_2_xl'
if [ -z "$1" ]; then
shot_path=$(date +%Y-%m-%d-%H-%M-%S).mp4
else
shot_path="$*"
fi
ffmpeg="ffmpeg"
# Get the device id lower case it.
device=$(adb shell getprop ro.product.model | sed 's/ /_/g' | tr '[:upper:]' '[:lower:]')
background_frame="./backgrounds/${device}_port_back.png"
foreground_frame="./backgrounds/${device}_port_fore.png"
shadow_frame="./backgrounds/${device}_port_shadow.png"
trap ctrl_c INT
function ctrl_c() {
echo "** Trapped CTRL-C"
echo "** Downloading screencast"
sleep 2
adb shell am broadcast -a com.android.systemui.demo -e command exit
adb pull /sdcard/Movies/$shot_path .
echo "FFMPEG Location: ${ffmpeg}"
echo "Device frame location: ${frame}"
echo "Device foreground location: ${forground_frame}"
echo "Device background location: ${background_frame}"
echo "Device shadow location: ${shadow_frame}"
if [ -x $(which "$ffmpeg") ] && [ -e "$background_frame" ]
then
echo "Rendering images with background"
$ffmpeg -y \
-f lavfi \
-i color=c=white:s=3840x1800 \
-i $shadow_frame \
-i $background_frame \
-i $shot_path \
-i $foreground_frame \
-filter_complex "[1:v]scale=920:-1[shadow]; \
[2:v]scale=920:-1[frame]; \
[3:v]scale=720:-1[video]; \
[4:v]scale=920:-1[sheen]; \
[0:v][shadow]overlay=shortest=1:x=(main_w-overlay_w)/2:y=(main_h-overlay_h)/2[background]; \
[background][frame]overlay=x=(main_w-overlay_w)/2:y=(main_h-overlay_h)/2[phone]; \
[phone][video]overlay=x=(main_w-overlay_w)/2:y=(main_h-overlay_h)/2[merged]; \
[merged][sheen]overlay=x=(main_w-overlay_w)/2:y=(main_h-overlay_h)/2" $shot_path-frame.mp4
$ffmpeg -y -i $shot_path-frame.mp4 -vf fps=10,scale=320:-1:flags=lanczos,palettegen $shot_path-palette.png
$ffmpeg -i $shot_path-frame.mp4 -i $shot_path-palette.png -filter_complex "fps=30,scale=720:-1:flags=lanczos[x];[x][1:v]paletteuse" $shot_path.gif
fi
alldone
}
function setup() {
adb shell settings put global sysui_demo_allowed 1
adb shell am broadcast -a com.android.systemui.demo -e command network -e mobile show -e datatype lte -e level 4
adb shell am broadcast -a com.android.systemui.demo -e command battery -e level 100 -e plugged false
adb shell am broadcast -a com.android.systemui.demo -e command network -e wifi show -e level 4
# Tweak this if you want the clock to changed
adb shell am broadcast -a com.android.systemui.demo -e command clock -e hhmm 0440
# Remove this if you want notifications to be availalbe
adb shell am broadcast -a com.android.systemui.demo -e command notifications -e visible false
echo When finished, run: adb shell am broadcast -a com.android.systemui.demo -e command exit
}
function alldone() {
adb shell am broadcast -a com.android.systemui.demo -e command exit
}
setup
adb shell screenrecord --bit-rate 16000000 /sdcard/Movies/$shot_path
echo "Finished"
@ebidel
Copy link

ebidel commented Nov 7, 2017

What's the purpose of all the adb shell am broadcast -a com.android.systemui.demo -e command exit? There are 2 calls and an echo that says to run that command after you're done.

AFAICT, you do that cleanup when alldone gets called.

@PaulKinlan
Copy link
Author

The ADB shell commands set up demo mode so you can take the battery status etc.

The final one in the echo i think is just me being lazy not cleaning up the code as in the first version you had to run it manually.

@varundtsfi
Copy link

varundtsfi commented May 24, 2019

@PaulKinlan
Hi Guys I have study your script which is very useful for my android screen recording but I have few doubts If you can clear please
1- If I am using this for a long like half an hrs, I feels device very leggy so I think its using system resources heavily.
2- I have noticed one more thing. when I ran it, Battery charging getting stopped (only charging symbol) but actually charging is happening, Why Its showing battery charging disable.
3- Here we are using android adb commands will these command works on all the android versions.
4- Why its not recording for a long time like 2 hrs or 5 hrs.
One Question
Is there any way by which we can stop android device charging in real so that device battery will not bulged.

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