Skip to content

Instantly share code, notes, and snippets.

@mddub
Created June 24, 2015 05:56
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 mddub/31d84a2ed965fbd16915 to your computer and use it in GitHub Desktop.
Save mddub/31d84a2ed965fbd16915 to your computer and use it in GitHub Desktop.
Make an inbox burndown timelapse
#####################################
# Capture webcam images and log inbox
#####################################
# Install imagesnap: http://iharder.sourceforge.net/current/macosx/imagesnap/
# Install gmail-logger: https://github.com/mddub/gmail-logger
# Make a directory to store the screenshots and webcam images
mkdir images
# Start these two commands in different terminal windows
watch -n 120 'python log_inbox.py'
watch -n 30 'DATE_SUFFIX=`date +"%Y-%m-%d_%H.%M.%S"`; screencapture -x -T 1 images/screen-$DATE_SUFFIX.png; imagesnap -w 1 images/webcam-$DATE_SUFFIX.jpg; echo $DATE_SUFFIX >> times'
#####################################
# Generate frames
#####################################
# Install ImageMagick
python join_into_frames.py
# ...which basically runs these commands:
# once:
convert -size 576x360 canvas:darkgray images/back.png
# for each image:
composite -gravity northwest \( images/screen-2015-01-22_18.39.16.png -resize 288x180 \) images/back.png images/out-2015-01-22_18.39.16.png
composite -gravity southeast \( webcam-2015-01-22_18.39.16.jpg -resize 320x180 \) images/out-2015-01-22_18.39.16.png images/out-2015-01-22_18.39.16.png
convert -fill white -undercolor '#00000080' -gravity northeast -pointsize 28 -annotate +0+148 '2015-01-22_18.39.16' images/out-2015-01-22_18.39.16.png images/out-2015-01-22_18.39.16.png
convert -fill white -undercolor '#00000080' -gravity northeast -pointsize 28 -annotate +0+114 '29 hours 19 minutes' images/out-2015-01-22_18.39.16.png images/out-2015-01-22_18.39.16.png
convert -fill white -undercolor '#00000080' -gravity southwest -pointsize 144 -annotate +0+12 '148' images/out-2015-01-22_18.39.16.png images/out-2015-01-22_18.39.16.png
#####################################
# Convert frames into video
#####################################
# Install ffmpeg
ffmpeg -framerate 10 -pattern_type glob -i 'images/out-*.png' -s:v 576x360 -c:v libx264 -profile:v high -crf 20 -pix_fmt yuv420p inbox_burndown.mp4
import shlex
import subprocess
import time
from datetime import datetime, timedelta
from functools import partial
from operator import itemgetter
import simplejson as json
date_from_counts = lambda d: datetime.strptime(d, '%Y-%m-%d %H:%M:%S')
date_from_times = lambda d: datetime.strptime(d, '%Y-%m-%d_%H.%M.%S')
times = open('times').read().strip().split('\n')
counts = [
(date_from_counts(date).strftime('%Y-%m-%d_%H.%M.%S'), date_from_counts(date), count)
for _, date, count
in [
line.split('\t')
for line
in open('inbox_count.log').read().strip().split('\n')
]
]
cur_count_index = 0
seconds_elapsed = 30
timestamp_elapsed_inbox = []
time_pairs = zip([times[0]] + times[:-1], times)
for first, second in time_pairs:
diff = (date_from_times(second) - date_from_times(first)).seconds
if diff <= 60:
seconds_elapsed += diff
hours_elapsed = seconds_elapsed / 3600
minutes_elapsed = (seconds_elapsed - hours_elapsed * 3600) / 60
pretty_elapsed = '{0} hour{1} {2} minute{3}'.format(
hours_elapsed,
'' if hours_elapsed == 1 else 's',
minutes_elapsed,
'' if minutes_elapsed == 1 else 's',
)
while counts[cur_count_index + 1][1] < date_from_times(second):
cur_count_index += 1
timestamp_elapsed_inbox.append(
[second, pretty_elapsed, counts[cur_count_index][2]]
)
make_canvas = 'convert -size 576x360 canvas:darkgray images/back.png'
subprocess.Popen(shlex.split(make_canvas)).wait()
for timestamp, elapsed, inbox in timestamp_elapsed_inbox:
commands = [
"composite -gravity northwest \\( images/screen-{0}.png -resize 288x180 \\) images/back.png images/out-{0}.png".format(timestamp),
"composite -gravity southeast \\( images/webcam-{0}.jpg -resize 320x180 \\) images/out-{0}.png images/out-{0}.png".format(timestamp),
"convert -fill white -undercolor '#00000080' -gravity northeast -pointsize 28 -annotate +0+148 '{0}' images/out-{0}.png images/out-{0}.png".format(timestamp),
"convert -fill white -undercolor '#00000080' -gravity northeast -pointsize 28 -annotate +0+114 '{1}' images/out-{0}.png images/out-{0}.png".format(timestamp, elapsed),
"convert -fill white -undercolor '#00000080' -gravity southwest -pointsize 144 -annotate +0+12 '{1}' images/out-{0}.png images/out-{0}.png".format(timestamp, inbox),
]
for c in commands:
print c
subprocess.Popen(shlex.split(c)).wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment