Skip to content

Instantly share code, notes, and snippets.

@leedo
Created August 28, 2013 01:53
Show Gist options
  • Save leedo/6361276 to your computer and use it in GitHub Desktop.
Save leedo/6361276 to your computer and use it in GitHub Desktop.
shrink and speed up video from Sony action cam
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw{max};
use List::MoreUtils qw{none};
use POSIX qw{ceil};
@ARGV == 2 or die "need video file";
my ($dir) = $ARGV[0] =~ m{([^/]+)\.MP4$};
mkdir $_ for $dir, "$dir/discard";
my @args = (qw{-r 1 -f image2}, "$dir/frame_%05d.jpg");
my $output = system "ffmpeg", "-i", $ARGV[0], @args;
my $max = max map {/frame[_0]+(\d+)/} glob "$dir/frame_*.jpg";
my $height = int($max / 30); # random guess, seems to work
my $center = int($max / 2);
my $width = int($max / 3);
my $e = 2.71828;
my @frames = ();
my $next = 1;
while ($next <= $max) {
push @frames, sprintf "frame_%05d.jpg", $next;
my $step = ($height * $e) ** -( (($next - $center) ** 2) / ((2 * $width) ** 2) );
$next += ceil($step * 10);
}
opendir my($dh), $dir;
while (my $file = readdir $dh) {
if (none {$file eq $_} @frames) {
rename "$dir/$file", "$dir/discard/$file";
}
}
system "ffmpeg", qw{-r 15/1 -f image2 -pattern_type glob -i}, "$dir/frame_*.jpg",
qw{-c:v libx264 -r 30 -pix_fmt yuv420p -vf vflip,hflip,scale=640:-1}, $ARGV[1];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment