Job-specific properties such as the encoded video file names, marker thumbnail names, metadata, and so on, are evaluated when the job is encoded by using a flexible string replacement engine that shares some similarities with ASP.NET. The engine is not bound to any particular file type; it is run against all files in the template and can be used to generate custom HTML, XML, javascript, XAML, or any other text-based file type based on properties of the encoding job.
Text in the template files is treated as a literal string with C# code blocks inserted by using the <$ and $> token pair. Within a code block, programming logic can exist; strings can be returned to the template and embedded by using the = operator. As with ASP.NET, flow control logic can be split across multiple code blocks. For example, a template that contains a single html file that has the following code:
<html>
<$if (PublishedItems.Length<10) {$>
<p>Your job has <$=PublishedItems.Length $> items<p/>
<$ } else { $>
<p>Your job has more than 10 files in</p>
<$}$>
</html>
would generate an output file such as this for a job with three items:
<html>
<p>Your job has 3 items<p/>
<html>
When Expression Encoder processes the job, it encodes the video files and then evaluates each template file in order to create the job output.
As illustrated here, a simple object model that lets you access and evaluate properties from the encoding job is exposed to the code running in the script blocks. A complete list of objects and methods appears at the end of this section.
Silverlight 1
The following example shows how a JavaScript file generated for Silverlight 1 might be constructed to enumerate and return the encoded files from a job as a JSON list:
//EnumerateItems.js
//Return JSON list of encoded files
function getEncodedFiles() {
return {"items" : [
<$ for(int iItem=0;iItem<PublishedItems.Length;iItem++){$>
{ "mediaUrl": "<$=PublishedItems[iItem].OutputFileName$>",
"clipThumbnail": "<$=PublishedItems[iItem].ThumbnailPath$>",
"chapters": [ <$ for (int iMarker=0;iMarker<PublishedItems[iItem].Markers.Length;iMarker++) {
PublishedMarker marker = PublishedItems[iItem].Markers[iMarker];
string strUrl = (marker.ThumbnailPath!=null)?marker.ThumbnailPath:"";
string strSep = (iMarker<PublishedItems[iItem].Markers.Length-1)?",":""; $>
{ "title": "<$=marker.Value$>",
"time": <$=marker.Time.TotalSeconds.ToString()$>,
"imageUrl": "<$=strUrl$>"} <$=strSep$> <$}$>
] }<$=(iItem<PublishedItems.Length-1?",":"")$>
<$}$>
]
}
}
You can then return the following output for an encoding job with two input clips and several markers:
//JavaScript function
//Return JSON list of encoded files
function getEncodedFiles() {
return {"items" : [
{ "mediaUrl": "NextGen_615k.wmv",
"placeholderImage": "NextGen_Thumb.jpg",
"chapters": [
{ "title": "Chapter 1", "time": 0.595,
"imageUrl": "NextGen_MarkerThumb 00.00.00.5950000.jpg"} ,
{ "title": "Chapter 2", "time": 2.748,
"imageUrl": "NextGen_MarkerThumb 00.00.02.7480000.jpg"} ,
{ "title": "Review comments here", "time": 8.286,
"imageUrl": "NextGen_MarkerThumb 00.00.08.2860000.jpg"} ,
{ "title": "Chapter 3", "time": 11.539,
"imageUrl": "NextGen_MarkerThumb 00.00.11.5390000.jpg"} ,
{ "title": "Chapter 4", "time": 13.173,
"imageUrl": "NextGen_MarkerThumb 00.00.13.1730000.jpg"} ,
] },
{ "mediaUrl": "Powered_by_Silverlight_480p.wmv",
"placeholderImage": "Powered_by_Silverlight_480p_Thumb.png",
"chapters": [
{ "title": "Goto point", "time": 1.5745869,"imageUrl": ""}
] }
]
}
}
Silverlight 2
The following example shows how an HTML file generated for Silverlight 2 might be constructed to enumerate and return the encoded files from a job:
<object data="data:application/x-silverlight," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="MediaPlayerTemplate.xap"/>
<param name="onerror" value="onSilverlightError" />
<param name="initparams" value='fakemode=<$$>,background=<$=TemplateParameter.BackgroundColor$>,autoplay=<$=TemplateParameter.AutoPlay$>,muted=<$=TemplateParameter.Muted$>,scalemode=<$=TemplateParameter.ScaleMode$>,playlist=<$=PlayListParam()$>' />
<a href="http://go.microsoft.com/fwlink/?LinkID=115261" style="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
</a>
</object>
You can then return the following output for an encoding job with three input clips and several markers:
<object data="data:application/x-silverlight," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="MediaPlayerTemplate.xap"/>
<param name="onerror" value="onSilverlightError" />
<param name="initparams" value='fakemode=,background=#FF000000,autoplay=True,muted=False,scalemode=1,playlist=<playList><playListItems><playListItem title="" description="Courtesy%20of%20National%20Geographic.%20%20Used%20by%20Permission." mediaSource="Bear.wmv" thumbSource="Bear_Thumb.jpg" ><chapters><chapter position="2.838" thumbnailSource="Bear_2.838.jpg" title="%5BUnknown%20Value%5D" /><chapter position="4.650" thumbnailSource="Bear_4.650.jpg" title="%5BUnknown%20Value%5D" /><chapter position="5.855" thumbnailSource="Bear_5.855.jpg" title="%5BUnknown%20Value%5D" /></chapters></playListItem><playListItem title="" description="Courtesy%20of%20Robert%20Millis.%20%20Used%20by%20Permission." mediaSource="Butterfly.wmv" thumbSource="Butterfly_Thumb.jpg" ><chapters><chapter position="2.588" thumbnailSource="Butterfly_2.588.jpg" title="%5BUnknown%20Value%5D" /><chapter position="4.503" thumbnailSource="Butterfly_4.503.jpg" title="%5BUnknown%20Value%5D" /><chapter position="6.170" thumbnailSource="Butterfly_6.170.jpg" title="%5BUnknown%20Value%5D" /></chapters></playListItem><playListItem title="" description="Courtesy%20of%20National%20Geographic.%20%20Used%20by%20Permission." mediaSource="Lake.wmv" thumbSource="Lake_Thumb.jpg" ><chapters><chapter position="3.181" thumbnailSource="Lake_3.181.jpg" title="%5BUnknown%20Value%5D" /><chapter position="5.408" thumbnailSource="Lake_5.408.jpg" title="%5BUnknown%20Value%5D" /><chapter position="7.572" thumbnailSource="Lake_7.572.jpg" title="%5BUnknown%20Value%5D" /></chapters></playListItem></playListItems></playList>' />
<a href="http://go.microsoft.com/fwlink/?LinkID=115261" style="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
</a>
</object>
Silverlight player
The preceding output could be used together with additional <xaml> and script to build a Silverlight video player that displays the chapter thumbnails at appropriate times.
In another example, you may want to enumerate the metadata for the first published item in the job and output it as a well-formed XML document for use inside a Silverlight player. The following template code:
<!—metadata.xml -->
<?xml version="1.0" encoding="utf-8"?>
<MetaData>
<$ foreach (KeyValuePair=""
<string,string>
kvp in PublishedItems[0].MetaData) {$>
<metadataitem>
<title>
<$=kvp.Key$></title>
<value>
<$=kvp.Value$></value>
</metadataitem>
<$}$>
</MetaData>
when it is applied to a job item which had the following metadata applied:
would create this output:
<?xml version="1.0" encoding="utf-8" ?>
<MetaData>
<metadataitem>
<title>Title</title>
<value>title is foo</value>
</metadataitem>
<metadataitem>
<title>Author</title>
<value>author is bar</value>
</metadataitem>
<metadataitem>
<title>Copyright</title>
<value>(c) a friend</value>
</metadataitem>
<metadataitem>
<title>Description</title>
<value>A Metadata Example</value>
</metadataitem>
</MetaData>