c606da3fb266a971f5fa0aedd51d0794ccc0b2a5
[videoscripts/.git] / organize_videos
1 #!/usr/bin/perl
2
3 use File::Copy;
4 use File::Basename;
5 use Getopt::Std;
6 use File::stat;
7 use Time::localtime;
8
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\" \\)";
18
19 # Sanity check
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; }
23
24 my %monthname2month = (
25                        "Jan" => "01",
26                        "Feb" => "02",
27                        "Mar" => "03",
28                        "Apr" => "04",
29                        "May" => "05",
30                        "Jun" => "06",
31                        "Jul" => "07",
32                        "Aug" => "08",
33                        "Sep" => "09",
34                        "Oct" => "10",
35                        "Nov" => "11",
36                        "Dec" => "12"
37                        );
38
39 my %month2monthname = (
40                        "01" => "Jan",
41                        "02" => "Feb",
42                        "03" => "Mar",
43                        "04" => "Apr",
44                        "05" => "May",
45                        "06" => "Jun",
46                        "07" => "Jul",
47                        "08" => "Aug",
48                        "09" => "Sep",
49                        "10" => "Oct",
50                        "11" => "Nov",
51                        "12" => "Dec"
52                        );
53
54 getopts("htv");
55
56 sub usage {
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";
60     return 1;
61 }
62
63 sub is_folder_empty {
64     my $dirname = shift;
65     opendir(my $dh, $dirname) or die "Not a directory";
66     return scalar(grep { $_ ne "." && $_ ne ".." } readdir($dh)) == 0;
67 }
68
69 if(defined $opt_h) { usage(); exit 1; }
70
71 # Only proceed if there are video files to organize
72 $video_files_found=`$find_cmd`;
73 if(!$video_files_found) { exit 0; }
74
75 # Only one instance of this script running at a time
76 use File::Pid;
77 my $pidfile = File::Pid->new({file => "/tmp/organize_videos.pid"});
78 exit if $pidfile->running();
79 $pidfile->write();
80
81 # Print the date
82 system("date");
83
84 # Merge videos prior to copying them over to the destination path
85 my $errno = 0;
86 if(defined $opt_t) { 
87     $errno=system("$merge_videos_by_day -s \"$srcpathname\" -t");
88 } else {
89     $errno=system("$merge_videos_by_day -s \"$srcpathname\"");
90 }
91 if($errno > 0) { $errno = $errno - 255; }
92 if($errno) { die "-E- $merge_videos_by_day encountered some errors with exit code $errno\n"; }
93
94 # Copy the videos over to the destination path
95 chdir "$srcpathname";
96 print "$find_cmd\n" if($opt_v);
97 foreach $file (`$find_cmd`) {
98
99     chomp($file);
100     $srcdir = dirname($file);
101     $file = basename($file);
102     $srcfile = $file;
103     $ext = $file; $ext =~ s/.*\.(\S+)$/$1/; $ext = lc($ext);
104     
105     print "Found movie: srcdir: $srcdir srcfile: $srcfile ext: $ext\n" if($opt_v);
106
107     # Throw out files not in the current srcpath
108     if((! -f "$srcfile") && (! -f "$srcdir/$srcfile")) { next; }
109     
110     # Change all spaces to underscores in the filename
111     #$srcfile =~ s/ /_/g;
112         
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);
115
116     # Get the date taken from the filename
117     if($srcfile =~ /^(\d+)-(\d+)-(\d+)/) {
118         $year = $1;
119         $month = $2;
120         $day = sprintf("%02d",$3);
121         $monthnum = $month;
122         $monthname = lc($month2monthname{$month});
123     }
124     # Get the date taken from the modification time
125     elsif($date_taken =~ /\S+\s+(\S+)\s+(\d+)\s+\S+\s+(\d+)/) {
126         $year = $3;
127         $month = $1;
128         $day = sprintf("%02d",$2);
129         $monthnum = $monthname2month{$month};
130         $monthname = lc($month2monthname{$month});
131     } else {
132         print "-E- Unable to parse year and month from this file: $srcdir/$srcfile\n";
133         next;
134     }
135
136     # We are ready to pick a destination folder to put the picture in
137     $dstdir = $dstpathname . "/" . $year;
138     $dstfile = $dstdir . "/" . $year . "-" . $monthnum . "-" . $day;
139
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; }
146         }
147         $dstfile = $newfile;
148     }
149     $dstfile = "$newfile.$ext";
150
151     if(defined $opt_t) {
152         print "-> Moving \"$srcdir/$srcfile\" to \"$dstfile\"\n";
153     } else {
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; }
163         } else {
164             if($opt_v) { print "-> Skipping \"$srcdir/$srcfile\". Destfile \"$dstfile\" already exists.\n"; }
165         }
166         # Fix the permissions
167         system("chown $owner \"$dstfile\"");
168         system("chgrp $group \"$dstfile\"");
169         system("chmod $mode \"$dstfile\"");
170     }
171
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) { 
177               $tmpdir=`tempfile`;
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);
181           }
182       }
183     }
184 }
185
186 $pidfile->remove();
187
188 print "\n\n";