This article gives an example that shows you how to generate a Google Video Sitemap for your video content hosted in Brightcove, using Java and the Brightcove Media API. For more information about sitemaps, see Making your Video Content Indexable. This article provides complete source code to be able to set up and run this sample in a Java-based environment. All of the source code is built using Java (or JSPs), but you can just as easily use .Net or PHP if you prefer. You can download a zip file that includes all of the sample code described in this article, including the style sheets we use for processing.
This example uses the Brightcove Media API to gather metadata about videos in your Brightcove account. To learn more about the Media API, read Getting Started with the Media API. You will need a Media API read token for your account in order to use the Media API.
After you generate the Google Video Sitemap XML document, you need to host it on a public facing web server. We used the Tomcat application server available at tomcat.apache.org.
Once you have a Media API token, you need to construct a HTTP GET request that use the find_all_videos function in the Media API to learn how many videos you have in your account. Here's an example of how to do this:
http://api.brightcove.com/services/library?token=J4JaPG1bBous-yrLR9ApnhzeedEZA.&page_size=1&command=find_all_videos&output=JSON&get_item_count=true
This request does several things; the table below describes what each part of the request does:
| http://api.brightcove.com/services/library? | this is the URL for a Media API request |
| token=J4JaPG1bBous-yrLR9ApnhzeedEZA. | this is your Media API read token |
| page_size=1 | page_size indicates the number of videos to return per page. Setting this to 1 means you will only have 1 video per page. |
| command=find_all_videos | This is the Brightcove Media API function to call. |
| get_item_count=true | This argument indicates that the find_all_videos function will return the number of pages of data (at 100 videos per page). |
| output=JSON | This indicates the result of the function will be in JSON format. |
Once you make this request and receive the results, you need to cast the results into a JSON format and get the total number of videos you have in your account. In this sample, we use the following JSP code to make a request to the Brightcove service and handle the JSON that's returned:
//define the URL you will make the call to
jsonURL = newURL("http://api.brightcove.com/services/library?
token=J4JaPG1bBous-yrLR9ApnhzeedEZA.&page_size=1
&command=find_all_videos&output=JSON&get_item_count=true");
// define the Java connection object you will use to the make the connection
HTTPURLConnection connection = (HttpURLConnection) jsonURL.openConnection();
connection.setRequestMethod("GET");
// connect to the service
connection.connect();
// set up a reader to read back the results and put them in a String object
BufferedReader reader = new BufferedReader(newInputStreamReader(connection.getInputStream()));
while (reader.ready()) {
lineRead = lineRead + reader.readLine();
}
// parse the results and put them into a JSONObject. We used a free open source
// JSON object implementation available at http://www.json.org/java
JSONObject jo = newJSONObject(lineRead.trim());
//get the number of videos in the account from the JSON object
int tmpVar = jo.getInt("total_count");
Once you know the number of videos in your account, you can now go ahead and generate the site map. We do this with a simple XSL transform that takes the MRSS results from the Brightcove Media API and transforms them into a Google Video Sitemap. Note, we have left out all of the XSL code details as this is mostly generic XSL and has nothing to do with Brightcove. You can examine xsl_transform.jsp and googleVideoSiteMap.xsl in the source code zip if you want to see the details.
Here is the code we implemented to do this:
//set up a simple for loop that will page through the results
// of the Media API call when there are more than videos in an account
// (Note that the Media APIs return a maximum of 100 videos per request)
for (int i = 0; i < tmpVar / 100; i++) {
// create a XML Transform utilities to process the XSL stylesheet and also
// the MRSS data from the Brightove service
TransformerFactory tFactory = TransformerFactory.newInstance();
// load the XLS stylesheet that defines the Google Video Site Map transform
Transformer transformer = tFactory.newTransformer(newStreamSource(googleVideoSiteMapSheetLocation));
// set a couple of variables in the XLS sheet that will be used to generate
// the sitemap
transformer.setParameter("playerURL", playerURL);
transformer.setParameter("siteURL", siteURL);
// make the media API calls (note we are paging through the results) and
// pass the MRSS data into the transform. The results of the transform are written to
// the out stream and will show up in a browser (or a Google-bot indexer).
transformer.transform(new StreamSource
("http://api.brightcove.com/services/library?token="+accountToken+"
&command=find_all_videos&output=MRSS""
+ "&page_number="+i),new StreamResult(out));
The result of this transformation is a Google Video Sitemap that looks like this:
<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns:dcterms="http://purl.org/dc/terms/" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns:bc="http://www.brightcove.com/tv/" xmlns:media="http://search.yahoo.com/mrss/" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>bar.org</loc> </url> <video:video> <video:title>FP</video:title> <video:description>Sci Fi</video:description> <video:player_loc>foo.com?bctid=275941287 </video:player_loc> <video:tag>quick,testing,test,hello,kamino,3$,tester </video:tag> <video:thumbnail_loc> http://brightcove.vo.llnwd.net/d2/unsecured/media/263096/2096_432539ee2fc768.jpg?pubId=263096 </video:thumbnail_loc> <video:publication_date>Thu, 09 Aug 2007 00:29:00 -0700 </video:publication_date> </video:video> <video:video xmlns:bc="http://www.brightcove.com/link"> <video:title>Forbidden Planet (F)</video:title> <video:description>An expedition is sent from Earth to Altair<video:description> <video:player_loc>foo.com?bctid=270912259 </video:player_loc> <video:tag>testing,scifi,test,hello,tester,quick,Publisher</video:tag> <video:thumbnail_loc> http://brightcove.vo.llnwd.net/d2/unsecured/media/263096/26096_431918_a3c592539ee2fc768.jpg?pubId=263096 </video:thumbnail_loc> <video:publication_date>Mon, 07 Jul 2008 09:06:49 -0700 </video:publication_date> </video:video> </urlset>