exif_settime.pl
author Peter Gervai <grin@grin.hu>
Wed, 07 May 2014 16:25:00 +0200
changeset 0 624a9ab34425
permissions -rwxr-xr-x
Add code lying around to the repo

#!/usr/bin/perl -w
#
# set date from exif info
# $Id: exif_settime.pl,v 1ab5ee663fb1 2011/05/24 06:19:06 grin $
#
# (c)2003, Peter Gervai
#


use POSIX qw( strftime );

my $exifprog="exiftags";
my $filepattern = qr/^(.+)\.jpg/i;
my $FNAME = "pic";
my $FEXTENSION = "jpg";

my $into_current_dir = 1;

my $DEBUG=0;

my $dir = $ARGV[0];
if( !defined($dir) || !-d $dir ) {
    print "hasznalat: $0 <start directory>\n\n";
    exit;
}


my %files;
&walk_files( $dir, '', \%files );

for my $key (sort keys %files) {
    
    print "($key) ", $files{$key}[0], '->', $files{$key}[1], "<<\n" if $DEBUG;
    print "mv \"$files{$key}[0]\" \"$files{$key}[1]\"\n";
    
}


sub walk_files {
    my ($base, $sdir, $rfiles) = @_;
    print "#!/bin/sh\n#Walking dir $base; $sdir...\n";
    
    if( !-r "$base/$sdir" ) {
        print "#Nem olvashato $base/$sdir!\n";
        return undef;
    }
    
    opendir( D, "$base/$sdir" );
    my @entries = grep { !/^\.\.?$/ } readdir( D );
    closedir( D );
    foreach my $entry (@entries) {
        if( -f "$base/$sdir/$entry" && $entry =~ /$filepattern/ ) {
            # process file
            my $time = &get_newtime( "$base/$sdir", $entry, $1 );
            
            print "touch -d '$time' $base/$sdir/$entry\n";
        }
    }
}



sub get_newtime {
    my ($base, $entry, $basename) = @_;
    my $time = &get_exif_time( "$base/$entry" );
    
    return $time;
}


sub get_exif_time {
    my $file = shift;
    
    if( -f $file ) {
        my $res = qx( $exifprog "$file" 2> /dev/null );
        if( $res =~ m/Image Created: (\S+ \S+)/ ) {
            #print "res=$_>>$1<<\n";
            # s m h md m y
            my @d = split /[: ]/, $1;
            #print "mon:", join(",", @d) , "<<\n";
            $d[1] -= 1;
            $d[0] -= 1900;
            return strftime("%Y-%m-%d %H:%M:%S",  @d[5,4,3,2,1,0] );
        }
    }
    return "today";
}