687615839dcb47f3773698d1b876ef0205f52a40
[minime.git] / termimg
1 #!/usr/bin/perl
2 use 5.014;
3 my ($file, $size) = @ARGV;
4
5 my @info = eval {
6         require Image::ExifTool;
7         my $exif = Image::ExifTool->new->ImageInfo($file, {
8                 CoordFormat => '%.5f',
9                 DateFormat => '%Y-%m-%d %H:%M:%S',
10                 Exclude => 'System:*',
11         });
12         die "exiftool: $exif->{Error}\n" if $exif->{Error};
13         return (
14                 $exif->{MIMEType},
15                 $exif->{ImageSize} ? "$exif->{ImageSize} ($exif->{Megapixels}MP)" : (),
16                 join(' ',
17                         $exif->{YCbCrSubSampling} // (),
18                         $exif->{Interlace} // (),
19                         $exif->{Compression} // $exif->{FileType},
20                         $exif->{SVGVersion} // $exif->{PDFVersion} // (),
21                 ),
22                 $exif->{ColorType} ? "$exif->{BitDepth}bpp $exif->{ColorType}" : (),
23                 sprintf('exif (%d)', scalar %{$exif}),
24                 $exif->{Thumb} ? "thumb $exif->{ThumbnailLength}B" : (),
25                 (map "@ $_",
26                         $exif->{DateTimeOriginal} //
27                         $exif->{DateCreated} //
28                         $exif->{CircaDateCreated} //
29                         $exif->{ModifyDate} //
30                         $exif->{ZipModifyDate} //
31                         (),
32                 ),
33                 join(', ',
34                         $exif->{GPSPosition} // $exif->{Location} // (),
35                         $exif->{GPSAltitude} // (),
36                 ),
37                 (map "hw $_", join(' ',
38                         $exif->{Make} // (), $exif->{Model} // (),
39                         $exif->{FOV} ? "(FOV $exif->{FOV})" : (),
40                 ) || ()),
41                 (map "sw $_", $exif->{Software} // $exif->{Application} // ()),
42                 (map "> $_", $exif->{'Description-nl'} // ()),
43                 $exif->{Warning} ? "! $exif->{Warning}" : (),
44         );
45 };
46 warn $@ if $@;
47
48 my $filesize = (stat $file)[7];
49 eval {
50         require Digest::MD5;
51         open my $bin, '<', $file;
52         binmode $bin;
53         my $md5 = Digest::MD5->new->addfile($bin)->b64digest;
54         push @info, "# $md5 ($filesize)";
55 } or warn $@;
56
57 my $aspect = eval {
58         require Term::ReadKey;
59         my ($w, $h, $x, $y) = Term::ReadKey::GetTerminalSize();
60         return $x/$w / $y*$h;
61 };
62
63 open my $pgm, '-|', convert => (
64         $file =>
65         -delete => '1--1',
66         $aspect ? ('+distort' => SRT => "0,0 1,$aspect 0") : (),
67         -thumbnail => $size || '40x12',
68         -colorspace => 'gray',
69         '-normalize',
70         -level => '-1%',
71         -channel => 'A', -threshold => '50%',
72         -background => 'black',
73         -layers => 'flatten',
74         -compress => 'none',
75         'pgm:-'
76 ) or die $!;
77
78 if (<$pgm> eq "P2\n") {
79         my ($width, $height) = split ' ', <$pgm>;
80         <$pgm>;  # ignore depth
81         my @ch = split //, " .:oO@";
82         while (<$pgm>) {
83                 print !$_ ? '/' : $ch[ $_ * @ch >> 8 ] for /\d+/g;
84                 print ' ', shift @info if @info;
85                 print $/;
86         }
87         substr $_, 0, 0, ' ' x ($width + 1) for @info;
88 }
89 say for @info;
90
91 =head1 NAME
92
93 termimg - media file fingerprint and metadata
94
95 =head1 SYNOPSIS
96
97 B<termimg> <file> [<size>]
98
99 =head1 DESCRIPTION
100
101 Identify given file by ascii image
102 with a summary of distinctive metadata.
103
104 =head1 AUTHOR
105
106 Mischa POSLAWSKY <perl@shiar.org>
107
108 =head1 LICENSE
109
110 AGPL3+