Integrating Video With Emfield And Media: Brightcove

Photo of Greg Harvey
Fri, 2009-06-19 12:51By greg

Firstly, hello Planet Drupal (or Drupal Planet... or Drupal folks, generally). *waves*

This is my first post since http://www.drupaler.co.uk was added to the list over there, so hopefully at least some of you will find it useful/interesting/both. Thanks to Robert Douglass for the addition.

Onwards. I'm working on a video project at the moment, displaying video content from Cantos, via Brightcove, using the powerful CCK, Emfield, Media: Brightcove combination of modules. I had rather hoped things would go smoothly, but they seldom do, as you'll all testify to, I'm sure. And it's when things don't work right out of the box that we Drupal chaps earn our money.

So here's the whole story. I downloaded and installed CCK, Emfield and Media: Brightcove in the usual way, contacted Cantos who sent me their Brightcove publisher ID, player ID and a sample video ID. Great! Off I went to the Embedded Media Field settings (admin/content/emfield) where I plugged in the publisher and player IDs under the Brightcove settings.

Next I created a video content type called, predictably, "video". I added my new CCK field using the Emfield widget, told it to use Brightcove as its source and off I went. Create a new video and the first thing I'm asked for is a video URL.

Oh. Wait. I don't have a URL. I just have a player ID, publisher ID and a video ID. I tried just throwing in the video ID (you never know!) but it bounced back expecting a valid URL. (I raised that feature request here.)

So what constitutes valid? A quick glance at the code in providers/brightcove3.inc will tell you. Line 155 in the emvideo_brightcove3_extract() function, to be precise. It looks like this - albeit with a whopping great comment in there about what exactly is being parsed and why:

return array(
'@c\.brightcove\.com/services/viewer/.*videoId=(\d+)@i',
'@id="myExperience([^"]+)" class="BrightcoveExperience"@i',
);
?>

Those of you familiar with regular expressions will already see the first expression looks the most straightforward to work with. It will pretty much accept something like this:

http://c.brightcove.com/services/viewer/anyoldrubbish?videoID=0000000

And it only really cares about the last bit straight after videoID= ... the ID itself. All the other information it might need you already set in the Embedded Media Settings earlier. Cool! Knowing that we can fake up a "valid" URL, even if we weren't given one. =)

I actually just did this and it worked fine (ultimately, but read on because we're not done yet):

http://c.brightcove.com/services/viewer/videoId=0000000

However, for some unknown reason this continued to not work for me. We thought it was maybe a bad ID somewhere, so we checked, double and triple. Then we did a lot of head-scratching and settings checking. We also had no small amount of assistance from the good people at Brightcove, most notably (and big thanks to) Luke Gaydon and Doug Castoldi.

Eventually, when all else had failed, I forwarded this link from Doug to Cantos and it gave them the clue they needed:
http://support.brightcove.com/docs/assigning-content-players-programmati...

They were using the wrong sort of Brightcove player! I'm not sure why it took so long to come to this conclusion, but Cantos sent me a link to a new player that *would* accept videoId parameters and all worked straight away. (I was working blind and never saw the Brightcove player publishing steps in action, so I can't tell you exactly what they did - just that if things aren't working it's probably simply how you created your embed code.)

And that's all there was to it! I now have lovely Brightcove/Cantos embedded video content in my website. We won't be launching for a few weeks and I don't want to give anything away just yet, but I will follow up with a URL when appropriate.

I will, however, leave you with a bit of code I used to present the Brightcove player as a node template variable so you can switch it off in the CCK display options of your content type and expose it where you need it in your node.tpl.php file, should your theming require it (mine did). You'll need a module first, and you can add this to your code:

/**
* Implementation of hook_preprocess_node().
*
* Adding variables to node template.
*/
function mymodule_preprocess_node(&$vars) {

// I usually have a switch here because I often want to
// add different vars to different content types
switch ($vars['node']->type) {

case 'video':
// add Brightcove video to node template
if ($vars['node']->field_video_brightcove[0]) {
$vars['brightcove_video'] = emvideo_brightcove3_video($vars['node']->field_video_brightcove[0]['embed'], 480, 317, $vars['node']->field_video_brightcove, $vars['node']->field_video_brightcove[0], $vars['node'], FALSE);
}
break;
}

}
?>

And once that's in place you can add this to your template wherever you need it (in my case, node-video.tpl.php):