Fork me on GitHub

Videojs-markers

The easiest way to show ad breaks, breakpoints, and bookmarks on your videojs player.

Basic

Add breakpoints on the video progress bar, hover-over tooltips and break overlays.

<video id="demo" controls class="video-js vjs-default-skin">
   <source src="http://vjs.zencdn.net/v/oceans.mp4" type="video/mp4">
   <source src="http://vjs.zencdn.net/v/oceans.webm" type="video/webm">
</video>

<script>
var player = videojs('demo');
player.markers({
   markers: [
      {time: 9.5, text: "this"},
      {time: 16,  text: "is"},
      {time: 23.6,text: "so"},
      {time: 28,  text: "cool"}
   ]
});
</script>

Customize markers

Fully customize the styles and text for your markers, tooltip and overlays.

<video id="demo" controls class="video-js vjs-default-skin">
   <source src="http://vjs.zencdn.net/v/oceans.mp4" type="video/mp4">
   <source src="http://vjs.zencdn.net/v/oceans.webm" type="video/webm">
</video>

<style>
.special-blue{
   background-color: #4949d8 !important;
}
</style>

<script>
var player = videojs('demo');

player.markers({
   markerStyle: {
      'width':'9px',
      'border-radius': '40%',
      'background-color': 'orange'
   },
   markerTip:{
      display: true,
      text: function(marker) {
         return "I am a marker tip: "+ marker.text;
      }
   },
   breakOverlay:{
      display: true,
      displayTime: 4,
      style:{
         'width':'100%',
         'height': '30%',
         'background-color': 'rgba(10,10,10,0.6)',
         'color': 'white',
         'font-size': '16px'
      },
      text: function(marker) {
         return "This is a break overlay: " + marker.overlayText;
      },
   },
   markers: [
      {time: 9.5, text: "this", overlayText: "1", class: "special-blue"},
      {time: 16,  text: "is", overlayText: "2"},
      {time: 23.6,text: "so", overlayText: "3"},
      {time: 28,  text: "cool", overlayText: "4"}
   ]
});
</script>

Dynamic video

Dynamically add or remove markers on the fly.



  • Marker events:
<video id="demo" controls class="video-js vjs-default-skin">
   <source src="http://vjs.zencdn.net/v/oceans.mp4" type="video/mp4">
   <source src="http://vjs.zencdn.net/v/oceans.webm" type="video/webm">
</video>

<script>
var player = videojs('demo');
player.markers({
   breakOverlay:{
      display: true
   },
   onMarkerClick: function(marker){
      $('.dynamic-demo-events').append('
  • Marker click: '+marker.time+'
  • '); }, onMarkerReached: function(marker){ $('.dynamic-demo-events').append('
  • Marker reached: '+marker.time+'
  • '); }, markers: [ {time: 9.5, text: "this", overlayText: "1"}, {time: 16, text: "is", overlayText: "2"}, {time: 23.6,text: "so", overlayText: "3"}, {time: 28, text: "cool", overlayText: "4"} ] }); // setup control handlers $(".dynamic-demo-prev").click(function(){ player.markers.prev(); }); $(".dynamic-demo-next").click(function(){ player.markers.next(); }); $(".dynamic-demo-add-random").click(function(){ var randomTime = Math.floor((Math.random() * parseInt(player.duration())) + 1); // come up with a random time player.markers.add([{ time: randomTime, text: "I'm new", overlayText: "I'm new" }]); }); $(".dynamic-demo-shift"). click(function(){ var markers = player.markers.getMarkers(); for(var i = 0; i < markers.length; i++) { markers[i].time += 1; } player.markers.updateTime(); }); $(".dynamic-demo-remove-first").click(function(){ player.markers.remove([0]); }); $(".dynamic-demo-remove-all").click(function(){ player.markers.removeAll(); }); $(".dynamic-demo-destroy").click(function(){ player.markers.destroy(); }); </script>

    Add the dependencies

    Include jquery.min.js, video-js.css and video.js:

    <link href="http://vjs.zencdn.net/4.3/video-js.css" rel="stylesheet">
    <link href="src/to/videojs.markers.css" rel="stylesheet">
    ...
    <script src="http://code.jquery.com/jquery-2.0.3.min.js"> </script>
    <script src="http://vjs.zencdn.net/4.3/video.js"></script>
    <script src='src/to/videojs.markers.js'></script>
          

    Setup your markers:

    // initialize video.js
    var player = videojs('video-id');
    
    //load the marker plugin
    player.markers({
       markers: [
          {
             time: 9.5,
             text: "put"
          },
          {
             time: 16,
             text: "any"
          },
          {
             time: 23.6,
             text: "text"
          },
          {
             time: 28,
             text: "here"
          }
       ]
    });

    Default Global Options

    // setup plugin
    player.markers({
       markerStyle: {
          'width':'7px',
          'border-radius': '30%',
          'background-color': 'red'
       },
       markerTip:{
          display: true,
          text: function(marker) {
             return "Break: "+ marker.text;
          },
          time: function(marker) {
             return marker.time;
          }
       },
       breakOverlay:{
          display: false,
          displayTime: 3,
          style:{
             'width':'100%',
             'height': '20%',
             'background-color': 'rgba(0,0,0,0.7)',
             'color': 'white',
             'font-size': '17px'
          },
          text: function(marker) {
             return "Break overlay: " + marker.overlayText;
          }
       },
       onMarkerClick: function(marker) {},
       onMarkerReached: function(marker) {},
       markers: [{
          time: 9.5,
          text: "marker",
          overlayText: 'overlay',
          class: 'custom-marker'
       }]
    });
    
    // plugin api
    player.markers.next();
    player.markers.prev();
    player.markers.remove([1,2]);
    player.markers.removeAll();
    player.markers.add([{ time: 40, text: "I'm added"}]);
    player.markers.reset([{ time: 23, text: "I'm reset"}]);
    player.markers.destroy();

    Documentation

    Name Type Description
    markerStyle Object

    The general css styling that would be applied to all the markers created by the plugin. The default is only concerned with width, border-radius, and background-color, but any other css rules would be applied too (think of it as jQuery's css function).

    Set up square, orange markers:

    {
       'width':'7px',
       'border-radius': '0%',
       'background-color': 'orange'
    }
    markerTip Object Options for hover-over marker tips.
    display Boolean

    Whether markerTips are displayed or not. Default to true.

    text Function

    Callback function for dynamically constructing your marker tip text. Simply return a string. The argument marker is the marker object passed to the plugin.

    Show time of the marker (hh::mm::ss format):

    function(marker) {
       var seconds = marker.time % 60;
       var minutes = parseInt(marker.time / 60) % 60;
       var hours = parseInt(marker.time / (60 * 60)) ;
       return hours + ":" + minutes + ":" + seconds;
    }
    time Function

    Callback function for dynamically getting the time for the marker. The plugin makes no assumption of where the time attribute is located in the marker object. This allows you to use existing formats to represent time (in seconds). The default function uses marker.time.

    breakOverlay Object Options for the break overlays at each marker.
    display Boolean

    Whether overlays are displayed or not. Default to false.

    displayTime Integer

    The number of seconds a overlay is shown from the beginning of its marker.. Default to 3 seconds.

    style Object

    Setup css styles for the overlays (play around with color, font-size, and height). Similar to markerStyle, any other css styles not in the default options would still be applied.

    text Function

    Same idea as markerTip's text callback mentioned above. This callback function is used to dynamically construct your overlay text. Simply return a string.

    function(marker) {
       return "You can setup anything here" + marker.overlay;
    }
    onMarkerReached Function

    This callback function is triggered whenever playback reaches the time interval from a marker. The interval is specified in the breakOverlay.displayTime . This can be used to trigger certain actions at each marker, depending on your use case. Again, this function is given a marker instance. In 0.6.0, the function also takes in a second parameter index.

    function(marker, index) {
       alert ("I just reached a marker!");
    }
    onMarkerClick Function

    This callback function is triggered when clicking on the markers. The default behavior for clicking on the marker is seeking to that point in the video. However, if onMarkerClick returns false, the default behavior will be prevented.

    function(marker) {
       alert ("I just clicked on a marker!");
    }
    markers Array

    An array of markers to display by the plugin. For each marker, you can specify the following:

    {
       time : 10,
       text: "marker text 1",
       overlayText: 'overlay text 1',
       class: 'custom-marker-class'
    }

    The time field is required, while the rest are optional. text and overlay are handy fields for setting up the markerTip and overlay text. class should be used when you want to have different colors for different markers which wouldn't be possible with the universal markerStyle.

    [{
       time: 10,
       text: "red marker",
       class: "red-marker-class"
    },
    {
       time: 20,
       text: "blue marker",
       class: "blue-marker-class"
    }]

    API Methods

    Name Description
    getMarkers

    Returns the array of current markers in the plugin, sorted by ascending time.

    next

    Go to the next marker from the current time in the video. If there's isn't a next marker, then does nothing.

    prev

    Go to the pervious marker from the current time in the video. If there's isn't one, then does nothing.

    updateTime

    Starting in v0.5.0, we allow modifying the marker's time dynamically (from the original marker object passed in). Once marker times have been changed, updateTime should be called to immediately reflect the changes in the UI.

    Add a second to all the markers

    var markers = player.markers.getMarkers();
    for (var i = 0; i < markers.length; i++) {
       markers[i].time += 1
    }
    player.markers.updateTime();
    remove

    Remove the markers in the given array of indices. Note the index is 0-based (e.g. first marker has index 0).

    Remove the 2nd and 3rd marker:

    player.markers.remove([1,2]);
    removeAll

    Remove all the markers in the player.

    add

    Dynamically add new markers.

    player.markers.add([{
       time: 40,
       text: "I'm added dynamically"
    }]);
    reset Reset all the markers in the video. This is equivalent to removeAll then add new markers.
    player.markers.reset([{
       time: 40,
       text: "I'm new"
    },
    {
       time:20,
       text: "I'm new too"
    }]);
    destroy Destroy the plugin. After calling this function, player.markers should be undefined.