Skip to content

Instantly share code, notes, and snippets.

@eznix86
Forked from lorenzos/android-screen-to-gif.sh
Last active April 2, 2019 14:15
Show Gist options
  • Save eznix86/d012fdb3e46166cb1f92b81a1d956535 to your computer and use it in GitHub Desktop.
Save eznix86/d012fdb3e46166cb1f92b81a1d956535 to your computer and use it in GitHub Desktop.
Captures screen from Android device via ADB and makes a high quality GIF
#!/bin/bash
# based on http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html
#
# How to install:
# Install adb
# sudo apt-get install ffmpeg
# wget https://gist.github.com/theArtechnology/d012fdb3e46166cb1f92b81a1d956535/raw/android-screen-to-gif.sh
# chmod a+x android-screen-to-gif.sh
# Help message
function usage() {
cat << EOF
Captures screen from Android device via ADB and makes a high quality GIF
Usage: ./android-screen-to-gif.sh output.gif
EOF
}
# Output file
if [[ ! "$1" ]]; then
usage
exit 1
fi
if ! command -v adb > /dev/null; then
echo "Missing adb";
exit 1
fi
if ! command -v ffmpeg > /dev/null; then
echo "Missing ffmpeg";
exit 1
fi
TMP_MP4="/tmp/android-screen-tmp.mp4"
TMP_FRAMES_DIR="/tmp/android-screen-tmp-frames"
TMP_GIF="/tmp/android-screen-tmp.gif"
# Cleanup
rm -f "$TMP_MP4"
rm -rf "$TMP_FRAMES_DIR"
rm -f "$TMP_GIF"
# Record and pull video
echo "Starting recording, press CTRL+C when you're done..."
trap "echo 'Recording stopped, downloading output...'" INT
adb shell screenrecord --bit-rate 10000000 --verbose "/sdcard/tmp-android-screen.mp4"
trap - INT
sleep 5
adb pull "/sdcard/tmp-android-screen.mp4" "$TMP_MP4"
sleep 1
adb shell rm "/sdcard/tmp-android-screen.mp4"
# Convert to gif
echo "Convert to gif..."
palette="/tmp/palette.png"
filters="fps=50,scale=320:-1:flags=lanczos"
echo "Getting frames..."
ffmpeg -v warning -i "$TMP_MP4" -vf "$filters,palettegen" -y $palette
echo "Almost done..."
ffmpeg -v warning -i "$TMP_MP4" -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y "$1"
echo "Completed: $HOME/$1";
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment