9 # Configuration parameters
10 my $srcpathname = "/naspool/pictures/New Photos"; # Path to look for photos to move from
11 my $dstpathname = "/naspool/videos/HomeVideos"; # Path to move the photos to
12 my $merge_videos_by_day = "/naspool/videos/bin/merge_videos_by_day";
13 my $minage = "+10"; # Files must be older than X minutes to move
14 my $owner = "ajp"; # The owner of the files after they are moved
15 my $group = "pip"; # The group of the files after they are moved
16 my $mode = "664"; # The mode to set on each file after they are moved
17 my $find_cmd = "find \"$srcpathname/\" -cmin $minage -a \\( -iregex \".*\.mov\" -o -iregex \".*\.3gp\" -o -iregex \".*\.mp4\" -o -iregex \".*\.mts\" -o -iregex \".*\.mkv\" \\)";
20 if(! -d $srcpathname) { print "-E- Can't find srcpath: $srcpathname\n"; exit 1; }
21 if(! -d $dstpathname) { print "-E- Can't find dstpath: $dstpathname\n"; exit 1; }
22 if(! -x $merge_videos_by_day) { print "-E- Can't find required script: $merge_videos_by_day\n"; exit 1; }
24 my %monthname2month = (
39 my %month2monthname = (
57 print "usage: $0 [-v] [-t]\n";
58 print " -v = verbose; print file being moved (to)\n";
59 print " -t = test; print what will happen, but don't do anything\n";
65 opendir(my $dh, $dirname) or die "Not a directory";
66 return scalar(grep { $_ ne "." && $_ ne ".." } readdir($dh)) == 0;
69 if(defined $opt_h) { usage(); exit 1; }
71 # Only proceed if there are video files to organize
72 $video_files_found=`$find_cmd`;
73 if(!$video_files_found) { exit 0; }
75 # Only one instance of this script running at a time
77 my $pidfile = File::Pid->new({file => "/tmp/organize_videos.pid"});
78 exit if $pidfile->running();
84 # Merge videos prior to copying them over to the destination path
87 $errno=system("$merge_videos_by_day -s \"$srcpathname\" -t");
89 $errno=system("$merge_videos_by_day -s \"$srcpathname\"");
91 if($errno > 0) { $errno = $errno - 255; }
92 if($errno) { die "-E- $merge_videos_by_day encountered some errors with exit code $errno\n"; }
94 # Copy the videos over to the destination path
96 print "$find_cmd\n" if($opt_v);
97 foreach $file (`$find_cmd`) {
100 $srcdir = dirname($file);
101 $file = basename($file);
103 $ext = $file; $ext =~ s/.*\.(\S+)$/$1/; $ext = lc($ext);
105 print "Found movie: srcdir: $srcdir srcfile: $srcfile ext: $ext\n" if($opt_v);
107 # Throw out files not in the current srcpath
108 if((! -f "$srcfile") && (! -f "$srcdir/$srcfile")) { next; }
110 # Change all spaces to underscores in the filename
111 #$srcfile =~ s/ /_/g;
113 # Make a note of the month, year, and day this video was taken (from the modification time of the file)
114 $date_taken = ctime(stat("$srcdir/$srcfile")->mtime);
116 # Get the date taken from the filename
117 if($srcfile =~ /^(\d+)-(\d+)-(\d+)/) {
120 $day = sprintf("%02d",$3);
122 $monthname = lc($month2monthname{$month});
124 # Get the date taken from the modification time
125 elsif($date_taken =~ /\S+\s+(\S+)\s+(\d+)\s+\S+\s+(\d+)/) {
128 $day = sprintf("%02d",$2);
129 $monthnum = $monthname2month{$month};
130 $monthname = lc($month2monthname{$month});
132 print "-E- Unable to parse year and month from this file: $srcdir/$srcfile\n";
136 # We are ready to pick a destination folder to put the picture in
137 $dstdir = $dstpathname . "/" . $year;
138 $dstfile = $dstdir . "/" . $year . "-" . $monthnum . "-" . $day;
140 # Check for duplicate filenames at the destination
141 $newfile = $dstfile . "." . "000";
142 if(-e "$newfile.$ext") {
143 foreach $i ('001' .. '999') {
144 $newfile = $dstfile . "." . $i;
145 if(! -e "$newfile.$ext") { last; }
149 $dstfile = "$newfile.$ext";
152 print "-> Moving \"$srcdir/$srcfile\" to \"$dstfile\"\n";
154 # Make sure the destination directories exist
155 $errno=system("mkdir -p \"$dstdir\"");
156 if($errno) { print "-E- Error creating dstdir: $dstdir\n"; next; }
157 # Perform the move operation from $srcdir/$srcfile -> $dstfile
158 if($opt_v) { print "-> Moving \"$srcdir/$srcfile\" to \"$dstfile\"\n"; }
159 # Make sure the dstfile doesn't exist, if it does, don't do the move
160 if(! -f "$dstfile") {
161 $errno=system("mv \"$srcdir/$srcfile\" \"$dstfile\" 2>/dev/null");
162 if($errno) { print "-E- Error moving srcfile to dstfile: $srcdir/$srcfile -> $dstfile\n"; next; }
164 if($opt_v) { print "-> Skipping \"$srcdir/$srcfile\". Destfile \"$dstfile\" already exists.\n"; }
166 # Fix the permissions
167 system("chown $owner \"$dstfile\"");
168 system("chgrp $group \"$dstfile\"");
169 system("chmod $mode \"$dstfile\"");
172 # Check to see if there is an empty sub directory to remove
173 if(($srcdir ne $srcpathname) && ($srcpathname ne ".")) {
174 if(is_folder_empty($srcdir)) {
175 print "-> Subdir detected for videos ($srcdir != $srcpathname)\n" if($opt_v);
176 if(! defined $opt_t) {
178 system("rm $tmpdir");
179 system("mv \"$srcdir\" $tmpdir > /dev/null 2>/dev/null");
180 print "-> Moved empty subdir $srcdir to $tmpdir\n" if($opt_v);