Motion Capture Sorting
Sunday, 06 January 2008
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/env php <?php // v. 1.0 $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); } if (count($fileNameArray) <= 0) { exit("No files to process.\n"); } //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 make 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); $newDirName = $splitUniqueDirNames[0] . '/' . $splitUniqueDirNames[1]; if (!file_exists($newDirName)) { if (mkdir($newDirName, 0755, true)) { echo 'Created directory: ' . $newDirName . "\n"; } } } //move files into the appropriate directories for ($i=0; $i<count($dirArray); $i++) { if (substr($dirArray[$i]['name'], -4) == $fileExtOne) { $newFileName = $dirArray[$i]['year'] .'-'. $dirArray[$i]['month'] .'-'. $dirArray[$i]['day'] .'/'. $dirArray[$i]['group'] .'/'. $dirArray[$i]['name']; if (rename($dirArray[$i]['name'], $newFileName)) { echo 'Moved file: ' . $newFileName . "\n"; } } if (substr($dirArray[$i]['name'], -4) == $fileExtTwo) { $newFileName = $dirArray[$i]['year'] .'-'. $dirArray[$i]['month'] .'-'. $dirArray[$i]['day'] .'/'. $dirArray[$i]['name']; if (rename($dirArray[$i]['name'], $newFileName)) { echo 'Moved file: ' . $newFileName . "\n"; } } }
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