Motion Capture Sorting

When using Motion for a security camera setup, it can generate a lot of files… One installation I have setup has “Encode movies in real-time” enabled, so not only are there many .jpgs in the capture directory, but also .avis.

This can become quite tedious… copying and pasting them from a GUI every day. Consequently, I would let the files simply pile up for a few days before moving them around. Then it became a big job.

Scripting to the rescue. The following below is a PHP-CLI script, that when run from the motion capture directory will sort the various files into a nice little structure that allows for dropping the .avis into a play list for viewing, and keeps the .jpgs around for backup.

#!/usr/bin/php
<?php

$fileExtOne = ".jpg";
$fileExtTwo = ".avi";
$fileNameArray = array();

//dump filenames (ending in .jpg or .avi)
//in current directory into array
if ($handle = opendir(".")) {
    while (false !== ($file = readdir($handle))) {
        if (substr($file, -4) == $fileExtOne ||
            substr($file, -4) == $fileExtTwo) {
            array_push($fileNameArray, $file);
        }
    }
    closedir($handle);
}

//make multi-dimensional array with substrs of filename
for ($i=0; $i<count($fileNameArray); $i++) {
    $fileNameSplit = split("-",$fileNameArray[$i]);
    $dirArray[$i]["group"] = $fileNameSplit[0];
    $dirArray[$i]["year"] = substr($fileNameSplit[1], 0, 4);
    $dirArray[$i]["month"] = substr($fileNameSplit[1], 4, 2);
    $dirArray[$i]["day"] = substr($fileNameSplit[1], 6, 2);
    $dirArray[$i]["name"] = $fileNameArray[$i];
}

//create unique array with new directory names and mkdir them
for ($i=0; $i<count($dirArray); $i++) {
    $dirNameArray[$i] = $dirArray[$i]["year"]
            ."-". $dirArray[$i]["month"]
            ."-". $dirArray[$i]["day"]
            ."/". $dirArray[$i]["group"];
}
$uniqueNewDirNames = array_unique($dirNameArray);
foreach ($uniqueNewDirNames as $value) {
    $splitUniqueDirNames = split("/", $value);
    mkdir($splitUniqueDirNames[0], 0755);
    mkdir($splitUniqueDirNames[0]
        ."/". $splitUniqueDirNames[1], 0755);
}

//move files into the appropriate directories
for ($i=0; $i<count($dirArray); $i++) {
    if (substr($dirArray[$i]["name"], -4) == $fileExtOne) {
        rename($dirArray[$i]["name"], $dirArray[$i]["year"]
            ."-". $dirArray[$i]["month"]
            ."-". $dirArray[$i]["day"]
            ."/". $dirArray[$i]["group"]
            ."/". $dirArray[$i]["name"]);
    }
    if (substr($dirArray[$i]["name"], -4) == $fileExtTwo) {
        rename($dirArray[$i]["name"], $dirArray[$i]["year"]
            ."-". $dirArray[$i]["month"]
            ."-". $dirArray[$i]["day"]
            ."/". $dirArray[$i]["name"]);
    }
}

?>

Here’s what find returns after running the script on the capture directory full of sample .avis and .jpgs.

username@workstation:~/captures> find .
.
./2008-01-06
./2008-01-06/01
./2008-01-06/01/01-20080106145755-02.jpg
./2008-01-06/01/01-20080106145755-04.jpg
./2008-01-06/01/01-20080106145756-00.jpg
./2008-01-06/01/01-20080106145757-00.jpg
./2008-01-06/01/01-20080106145757-01.jpg
./2008-01-06/01/01-20080106145758-00.jpg
./2008-01-06/01/01-20080106145758-01.jpg
./2008-01-06/01/01-20080106145758-02.jpg
./2008-01-06/01/01-20080106145758-04.jpg
./2008-01-06/01/01-20080106145758-05.jpg
./2008-01-06/01/01-20080106145758-06.jpg
./2008-01-06/01/01-20080106145759-00.jpg
./2008-01-06/01/01-20080106145808-01.jpg
./2008-01-06/01-20080106145755.avi
This entry was posted on Sunday, January 6th, 2008 at 4:45 pm. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply