Skip to content

Instantly share code, notes, and snippets.

@rxi
Created March 6, 2017 19:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rxi/b80e325857dbebe69d729450f2f9c08a to your computer and use it in GitHub Desktop.
Save rxi/b80e325857dbebe69d729450f2f9c08a to your computer and use it in GitHub Desktop.
#!/usr/bin/python2.7
import getopt, sys, os
def fmt(fmt, dic):
for k in dic:
fmt = fmt.replace("{" + k + "}", str(dic[k]))
return fmt
INFILE = "*.png"
OUTFILE = "timelapse.avi"
AUDIO = None
FPS = 18
BITRATE = 7000
opts, args = getopt.getopt(sys.argv[1:], "hf:b:a:o:i:", ["help"])
for o, a in opts:
if o == "-i": INFILE = a
if o == "-o": OUTFILE = a
if o == "-a": AUDIO = a
if o == "-f": FPS = float(a)
if o == "-b": BITRATE = int(a)
if o in ("-h", "--help"):
print "usage:"
print " encode_timelapse.py [OPTIONS]"
print
print "options:"
print " -i infile :%s" % INFILE
print " -o outfile :%s" % OUTFILE
print " -a audiofile :%s" % AUDIO
print " -f fps :%s" % FPS
print " -b bitrate :%s" % BITRATE
exit()
cmdstr = fmt(
'mencoder "mf://{infile}" -mf fps={fps} -o {outfile} -ovc lavc ' +
'-lavcopts vcodec=mpeg4:vbitrate={bitrate} {audio}',
{
"infile" : INFILE,
"outfile" : OUTFILE,
"fps" : FPS,
"bitrate" : BITRATE,
"audio" : ("-oac copy -audiofile %s" % AUDIO) if AUDIO else "",
})
print(cmdstr)
os.system(cmdstr)
#!/usr/bin/python2.7
import time, sys, os, subprocess
def get_current_workspace():
""" Returns the integer ID of the currently active workspace """
lines = subprocess.check_output(["wmctrl", "-d"]).split("\n")
for i in range(len(lines)):
if "*" in lines[i]:
return i
return 0
def get_arg(key, default=None):
""" Tries to return the given program argument. Returns default if no value
for this argument exists """
args = sys.argv[1:]
try:
return args[args.index(key) + 1]
except:
return default
def print_help():
print "Usage: timelapse.py [OPTION]"
print "Saves a screenshot at a given interval"
print ""
print " --help Display this help and exit"
print " -i Interval in seconds"
print " -o Output directory"
print " -x Excluded workspaces, comma separated"
print ""
print "Example:"
print " ./timelapse.py -i 5 -o abc -x 0,1"
print ""
def main():
if "--help" in sys.argv:
print_help()
exit()
interval = int(get_arg("-i", "10"))
output_dir = get_arg("-o", "out")
excluded = [] if not get_arg("-x") else map(int, get_arg("-x").split(","))
if not os.path.exists(output_dir):
os.makedirs(output_dir)
os.chdir(output_dir)
print "Taking a screenshot every %d seconds and saving to '%s'..."\
% (interval, output_dir)
while 1:
if get_current_workspace() in excluded:
print "%s On excluded workspace, skipping capture..." % time.asctime()
else:
print "%s Capturing screen..." % time.asctime()
os.system("scrot &")
time.sleep(interval)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment