Skip to content

Instantly share code, notes, and snippets.

@TerrorBite
Last active June 16, 2017 10:40
Show Gist options
  • Save TerrorBite/7fb60dce936cfd9fffe52cd721384607 to your computer and use it in GitHub Desktop.
Save TerrorBite/7fb60dce936cfd9fffe52cd721384607 to your computer and use it in GitHub Desktop.
Asciinema record-to-disk and playback-from-disk Python programs. Useful with asciinema v0.9.8 that doesn't have these functions natively.
#!/usr/bin/env python3
from sys import stdout, argv
import pickle
from time import sleep
with open(argv[1], 'rb') as f:
asciicast = pickle.load(f)
pos = 0
timing = asciicast.stdout.timing.split(b'\n')
for t in timing:
wait, length = t.split() # Decode frame data
wait, length = float(wait), int(length)
data = asciicast.stdout.data[pos:pos+length] # Get frame data
sleep(wait) # Wait for length of the frame
stdout.buffer.write(data) # Print the data
stdout.buffer.flush() # Don't forget to flush stdout
pos+=length # Update position
#!/usr/bin/env python3
from asciinema.commands.record import RecordCommand
from asciinema.config import Config
import pickle, time, sys
class ModifiedRecordCommand(RecordCommand):
def _record_asciicast(self):
asciicast = RecordCommand._record_asciicast(self)
if self.confirmator.confirm("~ Do you want to save it to disk? [Y/n] "):
filename = time.strftime("%Y%m%d-%H%M%S.asciicast")
with open(filename, 'wb') as fileobj:
pickle.dump(asciicast, fileobj)
print("~ Saved asciicast as {}".format(filename))
return asciicast
if __name__ == '__main__':
cmd = sys.argv[1] if len(sys.argv) > 1 else None
config = Config()
ModifiedRecordCommand(config.api_url, config.api_token, cmd, None, False).execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment