Skip to content

Instantly share code, notes, and snippets.

@mbeacom
Last active June 11, 2016 20:17
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 mbeacom/f528eea2c0b4a597c6d30facdfee47b3 to your computer and use it in GitHub Desktop.
Save mbeacom/f528eea2c0b4a597c6d30facdfee47b3 to your computer and use it in GitHub Desktop.
Records your Android device screen and converts it from MP4 to GIF
#!/bin/sh
# Saves screen grab to mp4 on device until you escape script -> pull mp4 from device and delete remote copy -> convert mp4 to gif and delete mp4
# Setup (OSX):
# This script requires ffmpeg and adb in your $PATH, installation steps are up to you, but these are pretty simple:
# Install Brew if you haven't already via: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# brew install android-platform-tools; brew install ffmpeg
# wget https://gist.github.com/mbeacom/afcee5b866945a14a41e321569c2242b/raw/d1328b57efba71d2b6c8cc89810622a1cefa185c/androidgif.sh
# chmod a+x androidgif.sh
# USAGE: ./androidgif.sh output
# Set output file, I set it to the current directory you're in term
output_file="$PWD/$1.gif"
# Usage func
function usage() {
cat << EOF
Usage: androidgif output
Captures a video of the screen from your Android device via ADB (mp4),
pulls the mp4 from the device, and converts it to a GIF.
EOF
}
palette="/tmp/palette.png"
filters="fps=15,scale=320:-1:flags=lanczos"
# Record and pull video
echo "Recording... \nCTRL+C to finalize the recording."
trap "echo 'Recording stopped, downloading output...'" INT
adb shell screenrecord --verbose "/sdcard/$1.mp4"
trap - INT
sleep 5
adb pull "/sdcard/$1.mp4" "/tmp/$1.mp4"
sleep 1
adb shell rm "/sdcard/$1.mp4"
# Convert the MP4 to a GIF
echo "Converting to GIF..."
ffmpeg -v warning -i "/tmp/$1.mp4" -vf "$filters,palettegen" -y $palette
ffmpeg -v warning -i "/tmp/$1.mp4" -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y $output_file
# Cleanup
rm -f "/tmp/$1.mp4"
rm -f "/tmp/palette.png"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment