Posts Tagged ‘timeline’

Read groups in a Mistika timeline with PHP

Thursday, April 1st, 2010

Here’s some code to read top-level groups from a Mistika timeline (.env-file). If all films are stored in separate groups with names, this snippet will list all films and their length (in both 25 and 24 frames TC).

Will produce something like this:

  • Test.env
    • Film one 00:00:20:0025 00:00:20:2024
    • Film two 00:00:20:0025 00:00:20:2024
function framesToTC($frames, $framerate) {
	
	
	$hours = floor( $frames / ( $framerate * 60 * 60 ) );
	$framesleft = $frames - ($hours * $framerate * 60 * 60);
	
	$minutes = floor( $framesleft / ( $framerate * 60 ) );
	$framesleft -= ( $minutes * $framerate * 60 );
	
	$seconds = floor( $framesleft / ( $framerate ) );
	$framesleft -= ( $seconds * $framerate );
	
	$tc = sprintf("%02d:%02d:%02d:%02d", $hours, $minutes, $seconds, $framesleft );
	
	return $tc;
}

function groupsInEnv( $envAbsPath ) {
	$groups = array();
	$balance = 0;
	$envH = fopen($envAbsPath, 'r');
	while (!feof($envH)) {
		$line = fgets($envH);
		if ( strpos($line, "Group(") === 0 && $balance === 1 ) {
			$waitForName = 1;
		}
		elseif ( strpos(ltrim($line), "n(") === 0 && $waitForName == 1 ) {
			$groupName = substr(trim($line), 2, -1);
			$waitForName = 0;
			$waitForLength = 1;
		}
		elseif ( $waitForLength == 1 ) {
//                                $groups[$groupName] = substr(trim($line), strpos($line, "W("), strpos($line, ")", strpos($line, "W(")) - 1);
			$expLine = explode(" ", $line);
			$groups[$groupName] = substr($expLine[2], 2, -1);
			$waitForLength = 0;
		}
		$balance += substr_count($line, "(");
		$balance -= substr_count($line, ")");
	}
	fclose($envH);
	if ( count($groups) === 0 ) {
		$groups["Timeline has no groups"] = "N/A";
	}
	return $groups;
}
$project = $_GET['mistika'];
$dataDir = "/PROJECTS/$project/DATA";
$timelines = scandir( $dataDir );
$out = "<ul>";
foreach( $timelines as $timeline){
	if ( substr($timeline, -4) == ".env" ) {
		$out .= "<li>$timeline<ul>\n";
		$films = groupsInEnv("$dataDir/$timeline");
		foreach( $films as $film => $filmFrames ) {
				$out .= "<li><b>$film</b>     ".framesToTC($filmFrames, 25).'<sup>25</sup>     '.framesToTC($filmFrames, 24).'<sup>24</sup></li>';
		}
		$out .= '</ul></li>'."\n";
	}
}