Sunday, April 11, 2010

Using bash to manually double the length of a (time lapse) image sequence

This small program came to be after not being able to render videos at 15fps in Adobe Premiere Pro CS4 with an MPEG2 codec. I reckoned that if I simply doubled the time the files would be displayed, in this case, using each frame twice in the sequence, I could achieve this effect. After deciding that a windows batch script would take too much time to code, I fell back to the shell language, Bash.

Input files: (30fps)
images000001.jpg (Frame 1)
images000002.jpg (Frame 2)
images000003.jpg (Frame 3)
...
images001913.jpg (Frame 4)

Resulting files: (15fps)
new_images000001.jpg (Frame 1)
new_images000002.jpg (Frame 1)
new_images000003.jpg (Frame 2)
new_images000004.jpg (Frame 2)
...
new_images003825.jpg (Frame 1913)
new_images003826.jpg (Frame 1913)


#!/bin/bash

for filename in ./images*
do
num1=${filename:8:5}
prefix=new_images
suffix=.jpg
num2="$(echo $num1 | sed 's/0*//')"

finnum=0
cpynum=0

finnum=$[ ($num2 * 2) - 1 ]
cpynum=$[ $finnum + 1 ]

#echo $filename "=>" $prefix$finnum$suffix
#echo " =>" $prefix$cpynum$suffix

mv $filename $prefix$finnum$suffix
cp $prefix$finnum$suffix $prefix$cpynum$suffix
done;

Saturday, March 6, 2010

PHP and WMI: System stats

First a little background:

So I just migrated a web staging server from Ubuntu 9.04 to Windows XP Pro because of problems I was having when transferring large and a lot of files to and from other computers.

I had read about a software called WAMP which is a software bundle containing Apache, PHP and mySQL (w/ phpmyadmin). So I reckoned that I save myself some time and give it a try instead of having to install everything manually and configure everything so that it plays nicely. It payed off except for the part where I had to give the root user for phpmyadmin a password and tell it not to log on automatically, and I couldn't get the whole pmad DB to work even after setting all the variables correctly and defining the control user (root).

Anyhow, so I have a database with 16 tables containing 1,029 rows. While it's not really a big database, it's the biggest one I've ever had control over. The computer all of this is running is on is a $300 rig with an Intel Atom 230 1.6GHz and 1GB of RAM, real sucky, I know.

The next thing I did is look for PHP snippets that would give me info about the server, stuff like CPU Load, Memory, and if I'm lucky, network utilization. Stuff you can find in the more expensive software cPanel. After a couple minutes on Google I find out, to my dismay, that a newly implemented PHP function, sys_getloadavg() only works for *nix systems because it depends on /proc/loadavg which isn't present on Windows Operating Systems. A half hour later I've been through at least 4 different PHP scripts that attempt at getting a CPU load for Windows systems, only one of which works:
<?php
define ( 'CPU_NAME', '.' );
$obj = new COM ( 'winmgmts:{impersonationLevel=impersonate}//' . CPU_NAME . '/root/cimv2' );
$pc = 0;

foreach ( $obj->instancesof ( 'Win32_Processor' ) as $mp )
{
echo "<pre>";
echo " CPU Load: " . $mp->LoadPercentage . "%\r\n";
echo "</pre>";
}
?>

It loads the Windows Management Instrumentation which is needed to get the cpu load. It creates a COM class and goes through all instances of the Win32_Processor WMI class which contain a bunch of properties, the one we want is the LoadPercentage class.

In the same fashion I displayed the available memory:

<?php
foreach ( $obj->instancesof ( 'Win32_OperatingSystem' ) as $mp )
{
$AvailMem = $mp->FreePhysicalMemory;
$AvailMem = round($AvailMem/1024);
$TotalMem = $mp->TotalVisibleMemorySize;
$TotalMem= round($TotalMem/1024);
$Processes = $mp->NumberOfProcesses;
$Status = $mp->Status;
echo "<pre>";
echo " Free Physical Memory: " . $AvailMem . " MB \r\n";
echo "Total Physical Memory: " . $TotalMem . " MB \r\n\r\n";
echo " Processes Running: " . $Processes . " \r\n</pre>";
}
}
?>

Now you could either check the Microsoft Software Developer Network Library (MSDN Library) for all the properties so you can fetch its value or you could just loop through all the properties with this convenient snippet:
foreach ( $obj->instancesof ( 'Win32_NetworkAdapter' ) as $np )
{
foreach($np->Properties_ as $propertyVal)
{
$arrProperties[$propertyVal->Name] = $propertyVal->Value;
}
print_r($arrProperties);
}

Here I used Win32_NetworkAdapter as the class that I'd like to see all the properties and their values. Pretty sweet stuff. If I were to print_r($np) then I'll only get 'variant Object' as the output.


If you have the time and use for the WMI then it's definitely a cool thing to mess around with for Windows Systems, just make sure you protect the php page because could be juicy info for hackers.