Skip to content

Instantly share code, notes, and snippets.

@dmlap
Last active August 29, 2015 14:01
Show Gist options
  • Save dmlap/423d893e2b197d1edce9 to your computer and use it in GitHub Desktop.
Save dmlap/423d893e2b197d1edce9 to your computer and use it in GitHub Desktop.
Query the player for video metadata.
/* Before player load, very basic video metadata is serialized as JSON to a data attribute:
<video
data-account="2549849259001"
data-player="d9f2b281-6199-4f8b-821d-ecea1504071f"
data-embed="default"
data-video-id="7"
class="video-js" controls></video>
You can access any metadata specified this way by querying the DOM:
*/
var account = player.el().getAttribute('data-account');
/* Shortly after a video is loaded into the player, additional metadata will be
downloaded. To access this data once it's available, you should listen to
`loadedmediainfo`. After that event, metadata for the current video is
available on the player itself:
*/
player.on('loadedmediainfo', function() {
console.log('The video title is: ' + player.mediainfo.title);
});
/*
Information about advertisement configuration can be obtained through the video.js ads
plugin (https://github.com/videojs/videojs-contrib-ads). Among many other things, you
can find out when ads start and end:
*/
player.on('adstart', function() { console.log('Started an ad pod'); });
player.on('adend', function() { console.log('Finished an ad pod'); });
// If the video changes, the metadata will be updated as well:
player.on('loadedmediainfo', function() {
sendToAnalytics('new video with metadata:', player.options());
});
// If you'd like to find out at any given moment whether an ad is playing, check the
// className:
(/ vjs-ad-playing /).test(' ' + player.el().className + ' ');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment