#!/usr/bin/perl

use warnings;
use strict;

my $out = shift @ARGV || 'screencast.avi';

warn "Select Window to record!\n";
my $xwininfo = `xwininfo`;

my $rate = 10;
my $w = $1 if $xwininfo =~ m{Width:\s*(\d+)}s;
my $h = $1 if $xwininfo =~ m{Height:\s*(\d+)}s;
my $offset = $1 if $xwininfo =~ m{Corners:\s*(\S+)}s;

$w = $w + 1 & 0xfffe;
$h = $h + 1 & 0xfffe;

$offset =~ s{\+}{};
$offset =~ s{\+}{,};

my $ffmpeg = "ffmpeg -f x11grab -s ${w}x${h} -r $rate -sameq -i $ENV{DISPLAY}+$offset $out";
warn "# $ffmpeg",$/;

system($ffmpeg) == 0 or die "$ffmpeg: $?";
#system "mplayer -osdlevel 3 $out";
system "ffplay $out";

warn "# created $out ", -s $out, $/;
