exif_settime.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_settime.pl,v 1ab5ee663fb1 2011/05/24 06:19:06 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 "#!/bin/sh\n#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 $time = &get_newtime( "$base/$sdir", $entry, $1 );
       
    55             
       
    56             print "touch -d '$time' $base/$sdir/$entry\n";
       
    57         }
       
    58     }
       
    59 }
       
    60 
       
    61 
       
    62 
       
    63 sub get_newtime {
       
    64     my ($base, $entry, $basename) = @_;
       
    65     my $time = &get_exif_time( "$base/$entry" );
       
    66     
       
    67     return $time;
       
    68 }
       
    69 
       
    70 
       
    71 sub get_exif_time {
       
    72     my $file = shift;
       
    73     
       
    74     if( -f $file ) {
       
    75         my $res = qx( $exifprog "$file" 2> /dev/null );
       
    76         if( $res =~ m/Image Created: (\S+ \S+)/ ) {
       
    77             #print "res=$_>>$1<<\n";
       
    78             # s m h md m y
       
    79             my @d = split /[: ]/, $1;
       
    80             #print "mon:", join(",", @d) , "<<\n";
       
    81             $d[1] -= 1;
       
    82             $d[0] -= 1900;
       
    83             return strftime("%Y-%m-%d %H:%M:%S",  @d[5,4,3,2,1,0] );
       
    84         }
       
    85     }
       
    86     return "today";
       
    87 }