Copyright 2022 - Custom text here

extract_imgs.pl

#!perl

#extract all images and movies files from tree

$directory = $ARGV[0];

print "dir=$directory\n";
$extract_imgs = "$directory/extracted_imgs";
$extract_mov = "$directory/extracted_mov";

mkdir($extract_imgs);
mkdir($extract_mov);


&change($directory,".");

sub change($) {
        my $file = shift;
        my $dir = shift;
    
        if (-d $file && $file !~ /\/extracted_/ ) {
                # Local variables.
                local *D;
                my    $f;
                # Open the directory, and check its result value.
                opendir D, $file || die "Cannot open $file:";
                # Loop over the files in the directory.
                foreach $f (readdir D) {
                        # We leave the special directories alone, or we'll never finish.
                        next if $f =~ /^\.{1,2}$/;
                        # Go in recursion.
                        &change("$file/$f",$file);
                }
                # Close dir and exit call.
                closedir D || die "Cannot close $file: $!";
        }
        else
        {
                if( $file =~ /\.jpe*g$/i || $file =~ /\.gif$/i || $file =~ /\.bmp$/i || $file =~ /\.mov$/i || $file =~ /\.vob$/i || $file =~ /\.mpg$/i || $file =~ /\.mpeg$/i || $file =~ /\.mp4$/i || $file =~ /\.avi$/i || $file =~ /\.rm$/i || $file =~ /\.wmv$/i  || $file =~ /\.m1v$/i  || $file =~ /\.rm$/i || $file =~ /\.mp2$/i || $file =~ /\.rm$/i || $file =~ /\.mpa$/i){
                    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)= stat($file);
                    if( $size > 20480 ){
                        $i = rindex($file,"/");
                        $base = substr($file,$i+1);
                        $i = rindex($dir,"/");
                        $basedir = substr($dir,$i+1);
                                                
                        $base = $basedir . "_" . $base;
                                $ie = rindex($base,".");
                                $ext = substr($base,$ie);
                        $base_wo_ext = substr($base,0,$ie);
                        
                        if( $file =~ /\.mov$/i || $file =~ /\.mpg$/i || $file =~ /\.mpeg$/i || $file =~ /\.mp4$/i || $file =~ /\.avi$/i || $file =~ /\.rm$/i || $file =~ /\.wmv$/i  || $file =~ /\.m1v$/i  || $file =~ /\.rm$/i || $file =~ /\.mp2$/i || $file =~ /\.rm$/i || $file =~ /\.mpa$/i || $file =~ /\.vob$/i){
                            $extract_dir = $extract_mov;
                        }else{
                            $extract_dir = $extract_imgs;
                        }
                
                        if( -f "$extract_dir/$base" ){
                            
                            $idx = 0;
                            while ( -f "$extract_dir/$base_wo_ext$idx$ext" ){
                                $idx++;
                            }
                            $dstfile = "$extract_dir/$base_wo_ext$idx$ext";
                        }else{
                            $dstfile = "$extract_dir/$base";
                        }
                        print "rename $file => $dstfile\n";
                        rename($file,$dstfile);                        
            }
        }
    }
}
f t g m