Skip to content

Instantly share code, notes, and snippets.

@jandubois
Created April 18, 2017 17:07
Show Gist options
  • Save jandubois/0aa9397bf33e9b6ae48493c8f6d268d6 to your computer and use it in GitHub Desktop.
Save jandubois/0aa9397bf33e9b6ae48493c8f6d268d6 to your computer and use it in GitHub Desktop.
Post-process asciinema recordings to reduce replay time
#!/usr/bin/env perl
use strict;
use warnings;
# Shorten video by limiting the max wait time
my $max_pause = 1;
# To make interactive commands more noticable, insert a longer delay
# after lines with a command prompt.
my $prompt = qr/jan\@zuze/;
my $prompt_pause = 0;
my($pause,$str) = (0, "");
my $duration = 0;
while (<>) {
next if /^\s+"duration":/;
print;
last if /^\s+"stdout": \[/;
}
# Combine multiple strings into a single entry without pauses as long
# as the strings don't include control characters.
while (<>) {
next if /^\s+\[$/;
if (/^\s+(\d.*),$/) {
$pause ||= eval $1 > $max_pause ? $max_pause : eval $1;
}
elsif (/\s+"(.*)"$/) {
$str .= $1;
}
elsif (/^\s+\](,?)/) {
if ($1 eq "" || index($str, "\\") >= 0) {
print qq( \[\n $pause,\n "$str"\n \]$1\n);
$duration += $pause;
$pause = $str =~ $prompt ? $prompt_pause : 0;
$str = "";
}
}
else {
die;
}
last if /^\s+\]$/;
}
while (<>) {
$_ = qq( \],\n "duration": $duration\n) if /^ \]$/;
print;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment