Last active
August 29, 2015 14:05
-
-
Save oglops/f1c52e9185f04699b0e3 to your computer and use it in GitHub Desktop.
latest version i'm using at home ffcast screen casting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# the convert call depends on whether you have h264 support in | |
# convert -list format | |
# if you don't have it , then record to -vcodec huffyuv | |
# if you see convert: Not enough pixel data `/tmp/x.avi' @ avi.c/ReadAVIImage/1243. | |
# maybe try removing -vcodec completely and use uncompressed avi | |
# usage: | |
# gif-batch.sh -r # just record | |
# gif-batch.sh -w # just record window | |
# gif-batch.sh -rc # record and upload | |
# gif-batch.sh -wc # record window and upload | |
# if-batch.sh -wc xxx.gif # record window and upload , save as xxx.gif | |
if [ $# -eq 0 ]; then | |
echo -e "Usage: \n\t git-batch.sh -wrc\n\n" | |
exit 0 | |
fi | |
CUR_DIR=$(pwd) | |
cleanup() | |
{ | |
cd $CUR_DIR | |
} | |
control_c() | |
# run if user hits control-c | |
{ | |
echo -en "\n*** Ouch! Exiting ***\n" | |
cleanup | |
exit $? | |
} | |
# trap keyboard interrupt (control-c) | |
trap control_c SIGINT | |
for last; do true; done | |
if [[ "$last" == *.gif ]] | |
then | |
GIF_FILE=$last | |
echo 'file:' $last | |
fi | |
random_key() | |
{ | |
echo $(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w $1 | head -n 1) | |
} | |
rand_name=`random_key 10` | |
echo rand file name : ${rand_name} | |
export TMPDIR=/var/tmp | |
rec_dir_root=/var/tmp/ffcast | |
rec_dir=${rec_dir_root}/${rand_name} | |
mkdir -p $rec_dir | |
while getopts "wrc" OPTION; do | |
case $OPTION in | |
w) echo recording window | |
RECORD_WINDOW=true | |
FFCAST_PARAM="-w" | |
# ;& # this means continue to execute the next block without testing its pattern | |
;& | |
r) echo recording | |
# TMP_AVI=$(mktemp /tmp/outXXXXXXXXXX.avi) | |
# TMP_GIF=$(mktemp /tmp/outXXXXXXXXXX.gif) | |
TMP_GIF=${rec_dir}/rec_${rand_name}.gif | |
TMP_AVI=${rec_dir}/rec_${rand_name}.avi | |
TMP_GIF_OPTIMIZE=${rec_dir}/rec_${rand_name}_optimize.gif | |
# TMP_MOV=$(mktemp /tmp/outXXXXXXXXXX.mov) | |
TMP_MOV=${rec_dir}/rec_${randname}.mov | |
if [ "$RECORD_WINDOW" = true ] ; then | |
echo "recording window is true" | |
fi | |
cd $rec_dir_root | |
# RECORD_CMD=$(echo ffcast ${FFCAST_PARAM:--s} ffmpeg -y -r 20 $TMP_AVI \&\& convert -set delay 10 -layers Optimize $TMP_AVI $TMP_GIF) | |
# RECORD_CMD=$(echo ffcast ${FFCAST_PARAM:--s} ffmpeg -y -r 20 $TMP_AVI \&\& convert -set delay 10 -dither none -depth 8 -deconstruct -layers optimizePlus -colors 32 $TMP_AVI $TMP_GIF) | |
# RECORD_CMD=$(echo ffcast ${FFCAST_PARAM:--s} ffmpeg -y -r 20 -vcodec libx264 $TMP_AVI \&\& convert -quality 100% -layers Optimize $TMP_AVI $TMP_GIF) | |
# try setting these to lower cpu usage | |
# export MAGICK_THREAD_LIMIT=1 | |
export MAGICK_AREA_LIMIT=100 | |
# export MAGICK_THROTTLE=50 | |
# maybe try adding -limit memory 256MiB -limit map 512MiB | |
# or just -limit area 512MiB | |
RECORD_CMD=$(echo ffcast ${FFCAST_PARAM:--s} ffmpeg -y -r 20 -b:v 16384k -vcodec libx264 $TMP_AVI \&\& convert -quality 100% -layers Optimize $TMP_AVI $TMP_GIF) | |
# rm -rf /tmp/gif | |
# mkdir -p /tmp/gif | |
# RECORD_CMD=$(echo ffcast ${FFCAST_PARAM:--s} ffmpeg -y -r 20 $TMP_GIF) | |
cd $CUR_DIR | |
echo Record command : $RECORD_CMD | |
eval $RECORD_CMD | |
# gifsicle –delay=10 –loop -O2 /tmp/gif/*.gif > animation.gif | |
gifsicle -d10 $TMP_GIF -O3 -o $TMP_GIF_OPTIMIZE | |
if [ -n "$GIF_FILE" ]; then | |
echo 'copying to specified gif file', $GIF_FILE | |
/bin/cp -f $TMP_GIF $GIF_FILE | |
TMP_GIF=$GIF_FILE | |
fi | |
export TMP_GIF=$TMP_GIF_OPTIMIZE | |
# export TMP_MOV=$TMP_MOV | |
gwenview $TMP_GIF >/dev/null 2>&1 | |
# gthumb --viewer $TMP_GIF >/dev/null 2>&1 | |
echo recorded video : $TMP_GIF | |
# exit 0 | |
# u maxsize=2048 | |
# if [ -n "$GIF_FILE" ]; then | |
# TMP_GIF=$GIF_FILE | |
# else | |
# TMP_GIF=$(ls -t /tmp/out*.gif | head -1) | |
# fi | |
# actualsize=$(du -k $TMP_GIF | cut -f 1) | |
# echo actialsize: $actualsize | |
# if [ $actualsize -ge $maxsize ] | |
# then | |
# TMP_GIF2=$(mktemp /tmp/outXXXXXXXXXX.gif) | |
# gifsicle -O2 --colors 64 $TMP_GIF > $TMP_GIF2 | |
# TMP_GIF = $TMP_GIF2 | |
# fi | |
# echo 'uploading gif : '$TMP_GIF | |
# # http://imgur.com/tools | |
# ~/scripts/imgurbash.sh $TMP_GIF | |
# exit 0 | |
# ;;& | |
;;& | |
c) # upload to gyfcat | |
echo Uploading to gyfcat | |
if [ -n "$GIF_FILE" ]; then | |
TMP_GIF=$GIF_FILE | |
else | |
TMP_GIF=$(ls -t ${rec_dir_root}/*/*.gif | head -1) | |
fi | |
echo Uploading file: $TMP_GIF | |
GYFCAT_KEY=`random_key 10` | |
# exit 0 | |
#curl -i -F "key=$GYFCAT_KEY" -F "acl=private" -F "AWSAccessKeyId=AKIAIT4VU4B7G2LQYKZQ" -F "policy=eyAiZXhwaXJhdGlvbiI6ICIyMDIwLTEyLTAxVDEyOjAwOjAwLjAwMFoiLAogICAgICAgICAgICAiY29uZGl0aW9ucyI6IFsKICAgICAgICAgICAgeyJidWNrZXQiOiAiZ2lmYWZmZSJ9LAogICAgICAgICAgICBbInN0YXJ0cy13aXRoIiwgIiRrZXkiLCAiIl0sCiAgICAgICAgICAgIHsiYWNsIjogInByaXZhdGUifSwKCSAgICB7InN1Y2Nlc3NfYWN0aW9uX3N0YXR1cyI6ICIyMDAifSwKICAgICAgICAgICAgWyJzdGFydHMtd2l0aCIsICIkQ29udGVudC1UeXBlIiwgIiJdLAogICAgICAgICAgICBbImNvbnRlbnQtbGVuZ3RoLXJhbmdlIiwgMCwgNTI0Mjg4MDAwXQogICAgICAgICAgICBdCiAgICAgICAgICB9" -F "success_action_status=200" -F "signature=mk9t/U/wRN4/uU01mXfeTe2Kcoc=" -F "Content-Type=image/gif" -F "file=@$TMP_GIF" https://gifaffe.s3.amazonaws.com/ | |
curl -i -F "key=$GYFCAT_KEY" -F "acl=private" -F "AWSAccessKeyId=AKIAIT4VU4B7G2LQYKZQ" -F "policy=eyAiZXhwaXJhdGlvbiI6ICIyMDIwLTEyLTAxVDEyOjAwOjAwLjAwMFoiLAogICAgICAgICAgICAiY29uZGl0aW9ucyI6IFsKICAgICAgICAgICAgeyJidWNrZXQiOiAiZ2lmYWZmZSJ9LAogICAgICAgICAgICBbInN0YXJ0cy13aXRoIiwgIiRrZXkiLCAiIl0sCiAgICAgICAgICAgIHsiYWNsIjogInByaXZhdGUifSwKCSAgICB7InN1Y2Nlc3NfYWN0aW9uX3N0YXR1cyI6ICIyMDAifSwKICAgICAgICAgICAgWyJzdGFydHMtd2l0aCIsICIkQ29udGVudC1UeXBlIiwgIiJdLAogICAgICAgICAgICBbImNvbnRlbnQtbGVuZ3RoLXJhbmdlIiwgMCwgNTI0Mjg4MDAwXQogICAgICAgICAgICBdCiAgICAgICAgICB9" -F "success_action_status=200" -F "signature=mk9t/U/wRN4/uU01mXfeTe2Kcoc=" -F "Content-Type=image/gif" -F "file=@$TMP_GIF" https://gifaffe.s3.amazonaws.com/ | |
echo GYFCAT_KEY: $GYFCAT_KEY | |
echo transcoding ... | |
# maybe i shoudl use jq to parse json file | |
# http://stedolan.github.io/jq/ | |
function jsonval { | |
temp=`echo $json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $prop` | |
echo ${temp##*|} | |
} | |
json=$(curl http://upload.gfycat.com/transcode/$GYFCAT_KEY) | |
# is response is API Response: {"error":"Connection timeout. Please check upload.gfycat.com\/status\/key for your finished conversion (details at gfycat.com\/api)"} | |
# we need to wait and poll | |
prop='error' | |
error=`jsonval` | |
ready=0 | |
echo API Response: $json | |
sleepCount=1 | |
if [[ $error == Connection* ]]; then | |
echo probably your image is encoding | |
while [ $ready -eq 0 ]; do | |
echo sleep 60s then check status - $sleepCount | |
sleep 60 | |
json=$(curl http://upload.gfycat.com/status/$GYFCAT_KEY --silent) | |
prop='task' | |
task=`jsonval` | |
echo API Response: $json | |
# sleepCount=$sleepCount+1 | |
sleepCount=$((sleepCount+1)) | |
if [ $task == complete ]; then | |
ready=1 | |
fi | |
done | |
fi | |
# after loop exits | |
# {"task":"complete","gfyname":"InformalOrderlyElkhound"} | |
prop="gfyname" | |
gfyName=`jsonval` | |
json=$(curl http://gfycat.com/cajax/get/$gfyName) | |
# {"gfyItem":{"gfyId":"unsungzigzagbream","gfyName":"UnsungZigzagBream","gfyNumber":878900342,"userName":"anonymous","width":856,"height":552,"frameRate":10,"numFrames":983,"mp4Url":"http:\/\/fat.gfycat.com\/UnsungZigzagBream.mp4","webmUrl":"http:\/\/fat.gfycat.com\/UnsungZigzagBream.webm","gifUrl":"http:\/\/zippy.gfycat.com\/UnsungZigzagBream.gif","gifSize":872965,"mp4Size":3015134,"webmSize":1458303,"createDate":1408965314,"views":1,"title":null,"extraLemmas":null,"md5":"4e00b2c81ae2c79e0b5950bfdb9f177a","tags":null,"nsfw":null,"sar":null,"url":false,"source":4,"dynamo":null,"subreddit":null,"redditId":null,"redditIdText":null,"uploadGifName":null}} | |
# prop='gfyItem' | |
# gfyItem=`jsonval` | |
# echo gfyItem : $gfyItem | |
# json=$gfyItem | |
echo transcoding "done" | |
echo new json: $json | |
prop='gfyName' | |
gyfid=`jsonval` | |
prop='mp4Url' | |
mp4Url=`jsonval` | |
prop='webmUrl' | |
webmUrl=`jsonval` | |
prop='gifUrl' | |
gifUrl=`jsonval` | |
# echo API Response: $json | |
echo | |
echo gyfid: $gyfid | |
echo mp4Url: $mp4Url | |
echo webmUrl: $webmUrl | |
echo gifUrl: $gifUrl | |
echo --------------- | |
echo gyfurl: http://gfycat.com/$gyfid | |
exit 0 | |
;; | |
*) # echo "Incorrect options provided" exit 1 ;; | |
echo "done" | |
;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment