So suddenly, you have hundreds of images, but what good are they? I mean yeah, it's cool to say you have a record of them, but there must be a better use. After scratching your head for a while it dawns on you, what about an animated gif? I mean wouldn't that show you what was happening better than that little jpg you have? So after some digging you find image magick, and you start to conver these jpgs into an animated gif, when it hits you, animated gifs are bad. Too few colors, and compress jpgs to gif sucks.
What's a developer to do? After a little more thought it dawns on you, that perhaps a movie would be a better format. After playing with image magick some more you find that it can convert jpgs into mpgs. Wonderful!
So for a few days you convert everything into the same movie. Each time your cron job runs, it gets progressively longer. Finally, when it takes 72 minutes to run, you say enough. It's time to start searching for a better way. Perhaps if you just did smaller time increments.
So with this new goal you start off on trying to make your scripts understand that an image only belongs to a day, and that you want movies of each day. Enter this cool use for perl.
FYI: in this little trip down my train of thought, all the references to you are actually thoughts I had.
This is the calling script that the cron job kicks off. It should download the images, and rename them to epoch seconds. Fifth, yes I know I shouldn't use system as I do, but in this particular case, I figured the chances of actually having this hole exploited was low, especially since I know the names of all the images before hand.
#!/bin/bash #you need to do a wget here. image_path=some_path image_dest=some_other_path for i in `ls ${image_path}*.jpg` do mv -f $i ${image_dest}/image-`stat -c "%Y" $i`.jpg done touch .rename.config.txt ./convert_day.pl ${image_dest}/*
So now we get to the perl (finally.) Things you should note, first you should have write access to . Second, this uses a persistent data structure. Third, becuase of it's persistent data structure, it would be bad to have more than 1 copy running at the same time in the same path. Fourth, yes I know there can be a lot of time improvements, but those are minimal compared with the time it takes to run convert on 10+ images.
#!/usr/bin/perl use strict; use Time::localtime; use Data::Dumper; my %pics; my %exec_hash; my @elements; my $FH; my $final_path="some_path"; # the user should ensure that this script is only run serially open(FH, "< .rename.config.txt"); my $line; my $lines = ""; while (defined ($line = <FH>)){ $lines .= $line; } %exec_hash=%{eval $lines} unless $lines eq ""; close(FH); # create the movie names and add images to the movies for(@ARGV){ my $temp = $_; my $tm = localtime((stat($temp))[8]); my $nm = sprintf("%02d%02d%04d", $tm->mon+1, $tm->mday, $tm->year+ +1900); push @elements, $nm unless defined $pics{$nm}; $pics{$nm} = [] unless defined $pics{$nm}; push @{$pics{$nm}}, $temp; } # convert the images to movies for(@elements){ my $t = $_; my $line = "convert -delay 10 " . join(" ", @{$pics{$t}}) . " ".$f +inal_path."/" . $t . ".mpg 2>/dev/null 1>/dev/null" ; # As processing a lot of images takes a long time # only update those images which have changed if(!defined($exec_hash{$t}) || $exec_hash{$t} ne $line){ system $line; $exec_hash{$t} = $line; } } my $save = pop @elements; system "cat ".$final_path."/". (pop @elements) .".mpg ".$final_path."/ +". $save . ".mpg > ".$final_path."/current.mpg" ; open($FH, ">.rename.config.txt"); $Data::Dumper::Terse = 1; $Data::Dumper::Indent = 1; print $FH Data::Dumper->Dump([\%exec_hash],['exec_hash']); close($FH);
Feel free to comment away, suggest improvements, ect. One thing to note, mpgs can be catted together, thus reducing the time taken even more; however, I did discover a couple cases where the images were updated as wget was running, thus it missed an image, (which it filled in later using the kickoff script.) Also note, rsync commands can be added to the kickoff script to upload the movies automatically.
The results of all this are here.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
•Re: Time for Some Movie Magick
by merlyn (Sage) on Oct 19, 2004 at 21:28 UTC | |
by abitkin (Monk) on Oct 20, 2004 at 11:58 UTC | |
|
Re: Time for Some Movie Magick
by elwarren (Priest) on Oct 19, 2004 at 20:22 UTC | |
by abitkin (Monk) on Oct 19, 2004 at 20:34 UTC |