-
Notifications
You must be signed in to change notification settings - Fork 0
/
shrink
executable file
·116 lines (108 loc) · 4.18 KB
/
shrink
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/perl
# Basic idea: measure the dimensions, then pick good sizes for
# smaller pictures and thumbnails, and then shrink/crop.
# Need to use exif utility, grep for Orientation -> jpegtran in place
# - "right - top" means we need to rotate right 90
# - "top - left" is normal
# - "left - bottom" means we need to rotate left 90
# - ??? bottom - right is 180???
use strict;
use File::Basename qw/dirname basename/;
my $tx = 160; # thumbnail width
my $ty = 120; # thumbnail height
my $mm = 800; # small dimension for medium-sized (UNUSED)
my $mx = 900; # medium width
my $movieOverlay = dirname($0) . "/movie-overlay.png";
my $tprefix = "t/t_";
my $mprefix = "m/m_";
my @files = ();
while ($_ = shift) {
if (/^-tx$/) {
$tx = shift;
} elsif (/^-ty$/) {
$ty = shift;
} elsif (/^-mx$/) {
$mx = shift;
} elsif (/^-tp$/) {
$tprefix = shift;
} elsif (/^-mp$/) {
$mprefix = shift;
} elsif (/^-/) {
die "unrecognized option $_";
} else {
push @files, $_;
}
}
system "mkdir -p ".dirname($tprefix) unless -d dirname($tprefix);
system "mkdir -p ".dirname($mprefix) unless -d dirname($mprefix);
foreach (@files) {
if (/\.mp4$/) {
my $thm = $_;
# deal with video files too! (medium = flv, thumbnail = jpg)
my $tf = $tprefix . $_;
$tf =~ s/mp4$/jpg/;
!-e $tf or die "$tf: already exists";
my $mf = $mprefix . $_;
$mf =~ s/mp4$/jpg/;
!-e $mf or die "$mf: already exists";
my $mv = $mprefix . $_;
$mv =~ s/mp4$/flv/;
!-e $mf or die "$mv: already exists";
# First make the thumbnail
my $mpout = "00000001.jpg";
unlink $mpout if -e $mpout;
system "mplayer -frames 1 -vo jpeg -nosound $_";
`identify $mpout` =~ /\s(\d+)x(\d+)\s/ or die "$_: not a picture";
my ($x, $y) = ($1, $2);
# (copied from below)
my ($sx, $sy) = ($tx / $x, $ty / $y);
my $ts = $sx > $sy ? $sx : $sy;
($sx, $sy) = (int($ts * $x), int($ts * $y));
my $cx = $sx > $tx ? int(($sx - $tx) / 2) : 0;
my $cy = $sy > $ty ? int(($sy - $ty) / 2) : 0;
#
my $opts = "$movieOverlay -gravity center -composite -format jpg";
system "convert $mpout -geometry ${sx}x$sy -crop ${tx}x$ty+$cx+$cy $opts $tf";
# Now make the FLV (and still shot)
# See http://forum.doom9.org/archive/index.php/t-140045.html
system "mencoder $_ -o $mv -of lavf -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vcodec=flv:vbitrate=800:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 -vf scale=640:360 -srate 22050";
system "convert $mpout -geometry 640x360 $mf";
unlink $mpout;
} else {
system "jhead -ft -autorot $_ > /dev/null"; # idempotent
my $mf = $mprefix . $_;
!-e $mf or die "$mf: already exists";
my $tf = $tprefix . $_;
!-e $tf or die "$tf: already exists";
`identify $_` =~ /\s(\d+)x(\d+)\s/ or die "$_: not a picture";
### Previous version - constrains largest dim to $mm:
# my ($x, $y, $mx, $my) = ($1, $2, $mm, $mm);
# if ($x < $y) {
# $my = int($y * $mx / $x);
# } else {
# $mx = int($x * $mm / $y);
# }
my ($x, $y) = ($1, $2);
my $my = int($y * $mx / $x);
my ($sx, $sy) = ($tx / $x, $ty / $y);
my $ts = $sx > $sy ? $sx : $sy;
($sx, $sy) = (int($ts * $x), int($ts * $y));
my $cx = $sx > $tx ? int(($sx - $tx) / 2) : 0;
my $cy = $sy > $ty ? int(($sy - $ty) / 2) : 0;
if (-e "$_.crop") { # allow manually adjusting crops
`cat $_.crop` =~ /\+(\d+)\+(\d+)/ or die "bad crop spec: $_.crop";
($cx, $cy) = ($1, $2);
}
print "$_: ${x}x$y -> ${mx}x${my} -> ${sx}x$sy -> ${tx}x$ty+$cx+$cy\n";
my $tmp = "tmp." . basename($mf);
system "convert $_ -geometry ${mx}x$my $tmp";
system "exif -r --output $mf $tmp";
my $badexit = $?;
unlink $tmp;
system "convert $_ -geometry ${sx}x$sy -crop ${tx}x$ty+$cx+$cy $tmp";
system "exif --remove --output $tf $tmp";
$badexit |= $?;
unlink $tmp;
die "error" if $badexit;
}
}