Brightcove Player Sample: Download Video Plugin

In this topic, you will learn how to create a link to allow the downloading of the best quality MP4 of a video playing in a Brightcove Player. The code will be written and explained, then later in the document converted into a plugin for easy use with different players.

Player example

You see a download icon located on the right side of the control bar to download the highest quality MP4 rendition of the video loaded in the player. Once clicked, the download will begin and you will the percentage downloaded displayed as well as an indicator (three animated dots) that progress is being made, as shown here:

download display

The button can be clicked again once the video is downloaded. Note that this plugin does work with videos in a playlist.

There is a VERY similar sample using audio rather than video assets in the Brightcove Player Sample: Download Audio Plugin document.

See the Pen Download Video Plugin by Brightcove Learning Services (@rcrooks1969) on CodePen.


Source code

View the complete solution on GitHub.

In addition to placing the download button in the controlbar, you can also place it over the player or in the HTML page, as shown here:

  • Overlay placed on the video
    Download overlay
    Download overlay
  • An HTML link you can place anywhere on your HTML page
    Download link
    Download link

Information on how to implement these different locations is shown at the bottom of this document.

Using the CodePen

Here are some tips to effectively use the above CodePen:

  • Toggle the actual display of the player by clicking the Result button.
  • Click the HTML/CSS/JS buttons to display ONE of the code types.
  • Later in this document the logic, flow and styling used in the application will be discussed in the Player/HTML configuration, Application flow and Application styling sections. The best way to follow along with the information in those sections is to:
    1. Click the EDIT ON CODEPEN button in the CodePen and have the code available in one browser/browser tab.
    2. In CodePen, adjust what code you want displayed. You can change the width of different code sections within CodePen.
    3. View the Player/HTML configuration, Application flow and/or Application styling sections in another browser/browser tab. You will now be able to follow the code explanations and at the same time view the code.

Development sequence

Here is the recommended development sequence:

  1. Use the In-Page embed player implementation to test the functionality of your player, plugin and CSS (if CSS is needed)
  2. Put the plugin's JavaScript and CSS into separate files for local testing
  3. Deploy the plugin code and CSS to your server once you have worked out any errors
  4. Use Studio to add the plugin and CSS to your player
  5. Replace the In-Page embed player implementation if you determine that the iframe implementation is a better fit (detailed in next section)

For details about these steps, review the Step-by-Step: Plugin Development guide.

iframe or In-Page embed

When developing enhancements for the Brightcove Player you will need to decide if the code is a best fit for the iframe or In-Page embed implementation. The best practice recommendation is to build a plugin for use with an iframe implementation. The advantages of using the iframe player are:

  • No collisions with existing JavaScript and/or CSS
  • Automatically responsive
  • The iframe eases use in social media apps (or whenever the video will need to "travel" into other apps)

Although integrating the In-Page embed player can be more complex, there are times when you will plan your code around that implementation. To generalize, this approach is best when the containing page needs to communicate to the player. Specifically, here are some examples:

  • Code in the containing page needs to listen for and act on player events
  • The player uses styles from the containing page
  • The iframe will cause app logic to fail, like a redirect from the containing page

Even if your final implementation does not use the iframe code, you can still use the In-Page embed code with a plugin for your JavaScript and a separate file for your CSS. This encapsulates your logic so that you can easily use it in multiple players.

API/Plugin resources used

API Methods API Events API Properties
on() loadstart player.mediainfo
    player.controlBar.customControlSpacer

Player/HTML configuration

This section details any special configuration needed during player creation. In addition, other HTML elements that must be added to the page, beyond the in-page embed player implementation code, are described.

Player configuration

No special configuration is required for the Brightcove Player you create for this sample.

Other HTML

No other HTML changes are needed.

Application flow

The basic logic behind this application is:

  • Get video name and extract the MP4 renditions from the video sources array.
  • Loop over all the video renditions and extract the MP4 versions.
  • Sort the MP4 array on the size attribute, largest to smallest.
  • Assign the largest video to a variable.
  • Create a clickable SVG image that downloads the video inside an HTML div element.
  • Place the download video SVG image element in the DOM.
  • Use an XMLHttpRequest() object to initiate and track progress of download.

Get video name and extract the MP4 renditions from the video sources array

Find the code which is labeled:

// +++ Get video name and the MP4 renditions +++

First the videoName variable is assigned the name of the video (with spaces removed) for use in the HTML anchor tag dynamically built later in the code. Next the renditionsAra variable is assigned all the possible renditions for the video.

Loop over all the video renditions and extract the MP4 versions

Find the code which is labeled:

// +++ Loop over videos and extract only MP4 versions +++

Loop over the array and assign to the mp4Ara variable renditions where the container is MP4, AND the rendition has a src property. The MP4 renditions appear twice in the sources array, once with a src property and once with a stream_name property. The first is for progressive download and the second for streaming.

The following screenshots show the console displaying all renditions and then the filtered renditions showing on MP4 renditions.

All renditions
All renditions
Only MP4 renditions after filtering
Only MP4 renditions after filtering

Sort the MP4 array on the size attribute, largest to smallest

Find the code which is labeled:

// +++ Sort the renditions from highest to lowest on size +++

Sort the mp4Ara using JavaScript's sort() method

Assign the largest video to a variable

Find the code which is labeled:

// +++ Extract the highest rendition +++

Assign the highestQuality variable the zeroth index value from the sorted MP4 renditions array.

Create an element that contains an clickable image inside an HTML div element

Find the code which is labeled:

// +++ Build the download image element +++

At the top of the JavaScript code HTML div and img elements are created and stored in variables. Those elements are then manipulated using the CSS and SVG image to dynamically build an HTML element that functions as a download button. The HTML source for the dynamically created element appears as follows:

<div class="vjs-control downloadStyle">
  <a href="http://f12.cf.brightcove.com/1752604059001/1752604059001_4457269242001_4457254747001.mp4?pubId=1752604059001&amp;videoId=4457254747001" title="Download Sea Marvels Collection"></a>
</div>

Place the download button element in the DOM

Find the code which is labeled:

// +++ Place the download button +++

In this case, the download button is placed in the controlbar's spacer. First, a reference to the spacer element is obtained, then an attribute set to position the new element in the spacer is defined, and lastly the new element placed in the spacer.

Start the download on user click

Find the code which is labeled:

// +++ On image click call the download function +++

Here the XMLHttpRequest() object is instantiated, the download() function is called, which forces download in the same browser window/tab, and progress is monitored and displayed.

Application styling

The CSS sizes and positions the download anchor for placement in the controlbar. It also defines SVG images for the download icon and the animated dots that show progress.

Plugin code

Normally when converting the JavaScript into a Brightcove Player plugin nominal changes are needed. One required change is to replace the standard use of the ready() method with the code that defines a plugin.

Here is the very commonly used start to JavaScript code that will work with the player:

videojs.getPlayer('myPlayerID').ready(function() {
  var myPlayer = this;
  ...
});

You will change the first line to use the standard syntax to start a Brightcove Player plugin:

videojs.registerPlugin('pluginName', function(options) {
  var myPlayer = this;
  ...
});

As mentioned earlier, you can see the plugin's JavaScript code in this document's corresponding GitHub repo: download-video.js.

Using the plugin with a player

Once you have the plugin's CSS and JavaScript files stored in an Internet accessible location, you can use the plugin with a player. In Studio's PLAYERS module you can choose a player, then in the PLUGINS section add the URLs to the CSS and JavaScript files, and also add the Name and Options, if options are needed.

Overlay implementation

To replace the icon in the control bar, and have an overlay on the video, you need to make a few changes.

First, in the JavaScript remove all the code that creates and displays the linked icon. Replace that code with code that creates and appends an overlay.

downloadString = "<a href='" + highestQuality + "' download='" + videoName + "'>Download the Video</a>";
overlay = document.createElement('p');
overlay.innerHTML = downloadString;
overlay.id = 'download-overlay';
myPlayer.el().appendChild(overlay);

You will also need to use CSS to style and place the overlay, which obviously can be changed to suit your implementation.

#download-overlay {
    background-color: #FFFFFF;
    color: #000000;
    font-size: 1em;
    padding: 2px;
    position: absolute;
    bottom: 35px;
    left: 485px;
    width: 135px;
    text-align: center;
}

If you wish to use this approach for your plugin, you will need to place the CSS into a separate file, save in an Internet accessible URL, and enter that URL when associating the plugin with a player in Studio.

HTML page implementation

To replace the icon in the control bar, and define a location in your HTML page for a download link, you need to make a few changes.

First, in your HTML page determine where you wish to insert the link and place the following HTML there.

<p id="insertionPoint"></p>

Next, in the JavaScript remove all the code that creates and displays the linked icon. Replace that code with code that creates a hyperlink and dynamically injects that hyperlink into the HTML page at your insertion point.

downloadString = "<a href='" + highestQuality + "' download='" + videoName + "'>Download the Video</a>";
document.getElementById('insertionPoint').innerHTML = downloadString;