1 |
#!/usr/bin/perl -w |
2 |
|
3 |
# frames.pl |
4 |
# |
5 |
# 07/19/07 18:06:13 CEST Dobrica Pavlinusic <dpavlin@rot13.org> |
6 |
|
7 |
use strict; |
8 |
|
9 |
use GD; |
10 |
use File::Path; |
11 |
|
12 |
my $tmp = '/tmp/frames'; |
13 |
my $out = '/tmp/frames.avi'; |
14 |
my $jpeg_q = 100; |
15 |
my $repeat = 16; |
16 |
$repeat = 1; # off |
17 |
|
18 |
rmtree $tmp if -e $tmp; |
19 |
mkpath $tmp || die "can't create $tmp: $!"; |
20 |
|
21 |
my $im = new GD::Image( 160, 120 ); |
22 |
|
23 |
my $im_col; |
24 |
|
25 |
sub col { |
26 |
my $hex = shift; |
27 |
$hex = lc($hex); |
28 |
die "not hex: $hex" unless ( $hex =~ m/^([0-9a-f])([0-9a-f])([0-9a-f])$/i ); |
29 |
my ( $r,$g,$b ) = ( hex($1 . $1),hex($2 . $2),hex($3 . $3) ); |
30 |
if ( ! defined( $im_col->{$hex} ) ) { |
31 |
my $col = $im->colorAllocate( $r, $g, $b ); |
32 |
warn "creating color $col $hex\n"; |
33 |
$im_col->{$hex} = $col; |
34 |
} |
35 |
return $im_col->{$hex}; |
36 |
} |
37 |
|
38 |
my @cols = ( |
39 |
'000', |
40 |
'f00', |
41 |
'0f0', |
42 |
'00f', |
43 |
'ff0', |
44 |
'0ff', |
45 |
'fff', |
46 |
); |
47 |
|
48 |
my $frame = 1; |
49 |
|
50 |
foreach my $c ( @cols ) { |
51 |
foreach ( 1 .. $repeat ) { |
52 |
$im->fill(50,50, col($c) ); |
53 |
warn "frame $frame\n"; |
54 |
my $path = sprintf('%s/%03d.jpg', $tmp, $frame++); |
55 |
open(my $fh, '>', $path) || die "can't open $path: $!"; |
56 |
print $fh $im->jpeg( $jpeg_q ) || die "can't save picture $path: $!"; |
57 |
close($fh) || die "can't close picture $path: $!"; |
58 |
} |
59 |
} |
60 |
|
61 |
system("ffmpeg -i $tmp/%03d.jpg -r 16 -vcodec copy -y $out") == 0 or |
62 |
die "encoding failed: $?"; |
63 |
|
64 |
rmdir $tmp || die "can't remove $tmp: $!"; |
65 |
|
66 |
warn "created $out ", -s $out, " bytes\n"; |