#!/usr/bin/env python

import blessings
import subprocess
import signal
import time
import sys
import os
import pyperclip

t = blessings.Terminal()
print "ADB Recorder v0.1"
print t.green("Starting recording on device...")

recorder = subprocess.Popen(["adb", "shell", "screenrecord", "--bit-rate", "8000000", "/sdcard/tmp_record.mp4"])
try:
    while recorder.poll() is None:    
        time.sleep(0.2)
except KeyboardInterrupt:
    pass

recorder.send_signal(signal.SIGTERM)
recorder.wait()

print t.green("Recording done, downloading file....")

# We need to wait for MOOV item to be written
time.sleep(2)

# Download file and cleanup
try:
    subprocess.check_call(["adb", "pull", "/sdcard/tmp_record.mp4", "./tmp_record.mp4"])
    subprocess.check_call(["adb", "shell", "rm", "/sdcard/tmp_record.mp4"])
except subprocess.CalledProcessError:
    print t.red("Could not download recording from the device.");
    sys.exit(-1)

print t.green("Converting video to GIF...")
FFMPEG_FILTERS = "fps=15,scale=480:-1:flags=lanczos"
FFMPEG_PALLETE = ["ffmpeg", "-v", "warning", "-i", "tmp_record.mp4", "-vf", FFMPEG_FILTERS + ",palettegen", "-y", "tmp_palette.png"]
FFMPEG_CONVERT = ["ffmpeg", "-v", "warning", "-i", "tmp_record.mp4", "-i", "tmp_palette.png", "-lavfi", FFMPEG_FILTERS + "[x];[x][1:v]paletteuse=dither=floyd_steinberg", "-y", "video.gif"]

try:
    subprocess.check_call(FFMPEG_PALLETE)
    subprocess.check_call(FFMPEG_CONVERT)
    print t.green("Done!")
except:
    print t.red("Could not convert downloaded recording to GIF!")
    sys.exit(-2)
finally:
    os.remove("tmp_palette.png")
    os.remove("tmp_record.mp4")

pyperclip.copy("file://" + unicode(os.path.abspath("./video.gif")))
print t.yellow("Path to GIF was copied to clipboard.")


@@ -140,16 +140,757 @@
 rminal()
+%0A%0Adef which(program):%0A    import os%0A    def is_exe(fpath):%0A        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)%0A%0A    fpath, fname = os.path.split(program)%0A    if fpath:%0A        if is_exe(program):%0A            return program%0A    else:%0A        for path in os.environ%5B%22PATH%22%5D.split(os.pathsep):%0A            path = path.strip('%22')%0A            exe_file = os.path.join(path, program)%0A            if is_exe(exe_file):%0A                return exe_file%0A%0A    return None%0A%0Adef check_requirements():%0A    if which(%22adb%22) is None:%0A        print t.red(%22This program requires adb executable to be in path.%22)%0A        sys.exit(-3)%0A    if which(%22ffmpeg%22) is None:%0A        print t.red(%22This program requires ffmpeg in path.%22)%0A        sys.exit(-4)%0A%0A
 %0Aprint %22
@@ -908,16 +908,38 @@
 r v0.1%22%0A
+check_requirements()%0A%0A
 print t.
@@ -978,16 +978,66 @@
 ice...%22)
+%0Aprint t.yellow(%22Press Ctrl+C to stop recording.%22)
 %0A%0Arecord

Check for requirements to output proper error messages