exif_rename.pl
changeset 0 624a9ab34425
equal deleted inserted replaced
-1:000000000000 0:624a9ab34425
       
     1 #!/usr/bin/perl -w
       
     2 #
       
     3 # set date from exif info
       
     4 # $Id: exif_rename.pl 208 2003-09-15 12:24:09Z grin $
       
     5 #
       
     6 # (c)2003, Peter Gervai
       
     7 #
       
     8 
       
     9 
       
    10 use POSIX qw( strftime );
       
    11 
       
    12 my $exifprog="exiftags";
       
    13 my $filepattern = qr/^(.+)\.jpg/i;
       
    14 my $FNAME = "pic";
       
    15 my $FEXTENSION = "jpg";
       
    16 
       
    17 my $into_current_dir = 1;
       
    18 
       
    19 my $DEBUG=0;
       
    20 
       
    21 my $dir = $ARGV[0];
       
    22 if( !defined($dir) || !-d $dir ) {
       
    23     print "hasznalat: $0 <start directory>\n\n";
       
    24     exit;
       
    25 }
       
    26 
       
    27 
       
    28 my %files;
       
    29 &walk_files( $dir, '', \%files );
       
    30 
       
    31 for my $key (sort keys %files) {
       
    32     
       
    33     print "($key) ", $files{$key}[0], '->', $files{$key}[1], "<<\n" if $DEBUG;
       
    34     print "mv \"$files{$key}[0]\" \"$files{$key}[1]\"\n";
       
    35     
       
    36 }
       
    37 
       
    38 
       
    39 sub walk_files {
       
    40     my ($base, $sdir, $rfiles) = @_;
       
    41     print "Walking dir $base; $sdir...\n";
       
    42     
       
    43     if( !-r "$base/$sdir" ) {
       
    44         print "Nem olvashato $base/$sdir!\n";
       
    45         return undef;
       
    46     }
       
    47     
       
    48     opendir( D, "$base/$sdir" );
       
    49     my @entries = grep { !/^\.\.?$/ } readdir( D );
       
    50     closedir( D );
       
    51     foreach my $entry (@entries) {
       
    52         if( -f "$base/$sdir/$entry" && $entry =~ /$filepattern/ ) {
       
    53             # process file
       
    54             my $newname = &get_newname( "$base/$sdir", $entry, $1 );
       
    55             
       
    56             my $cnt = 1;
       
    57             my $oname = $newname;
       
    58             $newname .= ".$FEXTENSION";
       
    59             while( defined( $rfiles->{$newname} ) ) {
       
    60                 print "CLASH $cnt\n" if $DEBUG;
       
    61                 $newname = "$oname-$cnt.$FEXTENSION";
       
    62                 $cnt++;
       
    63             }
       
    64             
       
    65             $rfiles->{$newname} = ["$base/$sdir/$entry", $newname];
       
    66             
       
    67             print "Got file $base; $sdir; $entry.\n" if $DEBUG;
       
    68             
       
    69         } elsif( -d "$base/$sdir/$entry" ) {
       
    70             # walk subdir
       
    71             &walk_files( $base, "$sdir/$entry", $rfiles );
       
    72         }
       
    73     }
       
    74 }
       
    75 
       
    76 
       
    77 
       
    78 sub get_newname {
       
    79     my ($base, $entry, $basename) = @_;
       
    80     my $time = &get_exif_time( "$base/$entry" );
       
    81     
       
    82     my $newname;
       
    83     if( $into_current_dir ) {
       
    84         $newname = "./$FNAME-$time";
       
    85     } else {
       
    86         $newname = "$base/$FNAME-$time";
       
    87     }
       
    88     
       
    89     return $newname;
       
    90 }
       
    91 
       
    92 
       
    93 sub get_exif_time {
       
    94     my $file = shift;
       
    95     
       
    96     if( -f $file ) {
       
    97         my $res = qx( $exifprog "$file" 2> /dev/null );
       
    98         if( $res =~ m/Image Created: (\S+ \S+)/ ) {
       
    99             #print "res=$_>>$1<<\n";
       
   100             # s m h md m y
       
   101             my @d = split /[: ]/, $1;
       
   102             #print "mon:", join(",", @d) , "<<\n";
       
   103             $d[1] -= 1;
       
   104             $d[0] -= 1900;
       
   105             return strftime("%Y.%m.%d-%H.%M.%S",  @d[5,4,3,2,1,0] );
       
   106         }
       
   107     }
       
   108     return "none";
       
   109 }