Convert VirtualDub joblist script to XML - using PHP

Step-by-Step descriptions of how to do things.
Post Reply
User avatar
^rooker
Site Admin
Posts: 1481
Joined: Fri Aug 29, 2003 8:39 pm

Convert VirtualDub joblist script to XML - using PHP

Post by ^rooker »

I had to parse VirtualDubs joblist (Sylia script) to extract some metadata like input video filename, frame-range, etc.

My idea was to use regular expressions to transform the linear script into an XML structure, for more convenient parsing.

Here's my conversion function:

Code: Select all

/**
* Converts a VirtualDub joblist file (Sylia Script) into an XML-like structure
* and returns a "SimpleXML" object, containing the data.
* More information about syntax and content of a VirtualDub.jobs file at:
*          http://www.virtualdub.org/docs/vdscript.txt
*/
function convert_virtualdub_joblist_to_xml($filename)
{
$joblist = file_get_contents($filename);

if ($joblist == false)
	printf("ERROR: Cannot read file: '%s'\n", $filename);
	return false;
}

$pattern = array(
			'/\/\/--*/',                            // Remove comment lines consisting only of hyphens
			'/\/\/ \$job "(.*)"/',                  // Begin job XML node
			'/\/\/ \$endjob/',                      // End job XML node
			'/\/\/ \$(\w*) "?(.*?)"?\r/',           // Transform comment data into nodes (e.g. "$start_time")
			'/VirtualDub\.Open\(.*\);/',            // Open settings XML node
			'/VirtualDub\.Close\(\);/',             // Close settings XML node
			'/VirtualDub\./',                       // Indent sub-nodes
			'/VirtualDub\.(\w*)\.(.*)\((.*?)\);/',  // Transform sub-object function calls to nodes
			'/VirtualDub\.(\w*)(.*);/',             // Transform function calls to nodes
			'/-- (.*?) --/',                        // Double hyphen (--) breaks XML comments. Get rid of it
			'/ -- /',                               //   - '' -
			'/\/\/(.*?)\r/',                        // Transform all other comment strings "//" to XML comments

		);
$replacement = array(
			' ',
			'<job name="\1">',
			'</job>',
			'<\1>\2</\1>',
			'<settings>',
			'</settings>',
			'    VirtualDub.',
			'<\1 action="\2">\3</\1>',
			'<\1>\2</\1>',
			' \1 ',
			' / ',
			'<!--\1 -->',
		);

// The xml root name is hardcoded to "virtualdub_joblist" - because it's not used anywhere else:
$xml_string = sprintf("<virtualdub_joblist>\n%s\n</virtualdub_joblist>\n", preg_replace($pattern, $replacement, $joblist));

$simple_xml = simplexml_load_string($xml_string);           // Has the nice side-effect of tidying up the XML
return $simple_xml;
}
Jumping out of an airplane is not a basic instinct. Neither is breathing underwater. But put the two together and you're traveling through space!
Post Reply