| 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # Test resizing on all image/audio files found within a directory tree |
| 4 | # Also checks for large memory use |
| 5 | |
| 6 | use strict; |
| 7 | use lib qw(blib/lib blib/arch); |
| 8 | |
| 9 | $|++; |
| 10 | |
| 11 | use Audio::Scan 0.84; |
| 12 | use Image::Scale; |
| 13 | use File::Find; |
| 14 | use Proc::ProcessTable; |
| 15 | |
| 16 | use constant MEMORY_LIMIT => 20 * 1024 * 1024; |
| 17 | use constant LEAK_THRESHOLD => 5; |
| 18 | |
| 19 | my $check_leaks = 0; |
| 20 | |
| 21 | my $dir = shift || die "Usage: $0 directory\n"; |
| 22 | |
| 23 | my $t = Proc::ProcessTable->new( cache_ttys => 1 ); |
| 24 | my $before; |
| 25 | $check_leaks && ( $before = size_of() ); |
| 26 | |
| 27 | find( |
| 28 | { |
| 29 | wanted => sub { |
| 30 | |
| 31 | # Skip ._* files OSX leaves around |
| 32 | return if /\._/; |
| 33 | |
| 34 | my $ok; |
| 35 | |
| 36 | if ( Audio::Scan->is_supported($File::Find::name) ) { |
| 37 | $ok = scan_audio($File::Find::name); |
| 38 | } |
| 39 | elsif (/\.(jpg|gif|png|bmp)$/i) { |
| 40 | |
| 41 | # Image file |
| 42 | $ok = scan_image($File::Find::name); |
| 43 | } |
| 44 | else { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | if ( $check_leaks && $ok ) { |
| 49 | my $after = size_of(); |
| 50 | if ( ( $after - $before ) > LEAK_THRESHOLD ) { |
| 51 | print " *** grew " . ( $after - $before ) . " KB\n"; |
| 52 | } |
| 53 | $before = $after; |
| 54 | } |
| 55 | }, |
| 56 | no_chdir => 1, |
| 57 | }, |
| 58 | $dir |
| 59 | ); |
| 60 | |
| 61 | sub scan_audio { |
| 62 | my $file = shift; |
| 63 | |
| 64 | my $offset; |
| 65 | my $length; |
| 66 | |
| 67 | local $ENV{AUDIO_SCAN_NO_ARTWORK} = 1; |
| 68 | |
| 69 | my $s = Audio::Scan->scan_tags($file); |
| 70 | my $tags = $s->{tags}; |
| 71 | |
| 72 | # MP3, other files with ID3v2 |
| 73 | if ( my $pic = $tags->{APIC} ) { |
| 74 | if ( ref $pic->[0] eq 'ARRAY' ) { |
| 75 | |
| 76 | # multiple images, return image with lowest image_type value |
| 77 | $length = ( sort { $a->[1] <=> $b->[1] } @{$pic} )[0]->[3]; |
| 78 | $offset = ( sort { $a->[1] <=> $b->[1] } @{$pic} )[0]->[4]; |
| 79 | } |
| 80 | else { |
| 81 | $length = $pic->[3]; |
| 82 | $offset = $pic->[4]; |
| 83 | } |
| 84 | |
| 85 | if ( $length && !$offset ) { |
| 86 | print "$file ($length) no offset, using SV mode... "; |
| 87 | |
| 88 | return scan_audio_embedded($file); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | # FLAC/Ogg picture block |
| 93 | elsif ( $tags->{ALLPICTURES} ) { |
| 94 | $length = ( sort { $a->{picture_type} <=> $b->{picture_type} } @{ $tags->{ALLPICTURES} } )[0]->{image_data}; |
| 95 | $offset = ( sort { $a->{picture_type} <=> $b->{picture_type} } @{ $tags->{ALLPICTURES} } )[0]->{offset}; |
| 96 | } |
| 97 | |
| 98 | # ALAC/M4A |
| 99 | elsif ( $tags->{COVR} ) { |
| 100 | $length = $tags->{COVR}; |
| 101 | $offset = $tags->{COVR_offset}; |
| 102 | } |
| 103 | |
| 104 | # WMA |
| 105 | elsif ( my $pic = $tags->{'WM/Picture'} ) { |
| 106 | if ( ref $pic eq 'ARRAY' ) { |
| 107 | |
| 108 | # return image with lowest image_type value |
| 109 | $length = ( sort { $a->{image_type} <=> $b->{image_type} } @{$pic} )[0]->{image}; |
| 110 | $offset = ( sort { $a->{image_type} <=> $b->{image_type} } @{$pic} )[0]->{offset}; |
| 111 | } |
| 112 | else { |
| 113 | $length = $pic->{image}; |
| 114 | $offset = $pic->{offset}; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | # APE |
| 119 | elsif ( $tags->{'COVER ART (FRONT)'} ) { |
| 120 | $length = $tags->{'COVER ART (FRONT)'}; |
| 121 | $offset = $tags->{'COVER ART (FRONT)_offset'}; |
| 122 | } |
| 123 | |
| 124 | return if !$length && !$offset; |
| 125 | |
| 126 | print "$file ($length @ $offset)... "; |
| 127 | |
| 128 | # resize |
| 129 | my $im = Image::Scale->new( $file, { offset => $offset, length => $length } ); |
| 130 | if ($im) { |
| 131 | my $ok; |
| 132 | print $im->width . 'x' . $im->height . ' '; |
| 133 | $ok = eval { $im->resize_gd_fixed_point( { width => 100, memory_limit => MEMORY_LIMIT } ) }; |
| 134 | if ($@) { |
| 135 | print "resize failed ($@)"; |
| 136 | sleep 5; |
| 137 | } |
| 138 | elsif ( !$ok ) { |
| 139 | print "resize failed"; |
| 140 | } |
| 141 | else { |
| 142 | print "ok"; |
| 143 | } |
| 144 | } |
| 145 | else { |
| 146 | print "read failed"; |
| 147 | sleep 5; |
| 148 | } |
| 149 | |
| 150 | print "\n"; |
| 151 | |
| 152 | return 1; |
| 153 | } |
| 154 | |
| 155 | # Let Audio::Scan extract the artwork, needed for some APIC tags |
| 156 | sub scan_audio_embedded { |
| 157 | my $file = shift; |
| 158 | |
| 159 | my $image_data = ''; |
| 160 | my $length = 0; |
| 161 | |
| 162 | local $ENV{AUDIO_SCAN_NO_ARTWORK} = 0; |
| 163 | |
| 164 | my $s = Audio::Scan->scan_tags($file); |
| 165 | my $tags = $s->{tags}; |
| 166 | |
| 167 | # MP3, other files with ID3v2 |
| 168 | if ( my $pic = $tags->{APIC} ) { |
| 169 | if ( ref $pic->[0] eq 'ARRAY' ) { |
| 170 | |
| 171 | # multiple images, return image with lowest image_type value |
| 172 | $image_data = ( sort { $a->[1] <=> $b->[1] } @{$pic} )[0]->[3]; |
| 173 | } |
| 174 | else { |
| 175 | $image_data = $pic->[3]; |
| 176 | } |
| 177 | |
| 178 | $length = length $image_data; |
| 179 | } |
| 180 | |
| 181 | if ( !$length ) { |
| 182 | warn " !!! No image data found, possible Audio::Scan bug?\n"; |
| 183 | sleep 5; |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | # resize |
| 188 | my $im = Image::Scale->new( \$image_data ); |
| 189 | if ($im) { |
| 190 | my $ok; |
| 191 | print $im->width . 'x' . $im->height . ' '; |
| 192 | $ok = eval { $im->resize_gd_fixed_point( { width => 100, memory_limit => MEMORY_LIMIT } ) }; |
| 193 | if ($@) { |
| 194 | print "resize failed ($@)"; |
| 195 | sleep 5; |
| 196 | } |
| 197 | elsif ( !$ok ) { |
| 198 | print "resize failed"; |
| 199 | } |
| 200 | else { |
| 201 | print "ok"; |
| 202 | } |
| 203 | } |
| 204 | else { |
| 205 | print "read failed"; |
| 206 | sleep 5; |
| 207 | } |
| 208 | |
| 209 | print "\n"; |
| 210 | |
| 211 | return 1; |
| 212 | } |
| 213 | |
| 214 | sub scan_image { |
| 215 | my $file = shift; |
| 216 | |
| 217 | print "$file... "; |
| 218 | |
| 219 | my $im = Image::Scale->new($file); |
| 220 | if ($im) { |
| 221 | my $ok; |
| 222 | print $im->width . 'x' . $im->height . ' '; |
| 223 | $ok = eval { $im->resize_gd_fixed_point( { width => 100, memory_limit => MEMORY_LIMIT } ); }; |
| 224 | if ($@) { |
| 225 | print "resize failed ($@)"; |
| 226 | sleep 5; |
| 227 | } |
| 228 | elsif ( !$ok ) { |
| 229 | print "resize failed"; |
| 230 | } |
| 231 | else { |
| 232 | print "ok"; |
| 233 | } |
| 234 | } |
| 235 | else { |
| 236 | print "read failed"; |
| 237 | sleep 5; |
| 238 | } |
| 239 | |
| 240 | print "\n"; |
| 241 | |
| 242 | return 1; |
| 243 | } |
| 244 | |
| 245 | sub size_of { |
| 246 | my $pid = shift || $$; |
| 247 | |
| 248 | foreach my $p ( @{ $t->table } ) { |
| 249 | if ( $p->pid == $pid ) { |
| 250 | return $p->rss; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | die "Pid $pid not found?"; |
| 255 | } |