Background:
So, let's say there's a webcam out there, with pictures taken at regular intervals. No, not that type of webcam, I'm talking about one of outdoors, so you can see what's happening outside your little cube. Now staring and refreshing at that little image 24/7/365 isn't really your cup of tea. I mean, shouldn't you use wget to grab the images for you?

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.


==
Kwyjibo. A big, dumb, balding North American ape. With no chin.

In reply to Time for Some Movie Magick by abitkin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.