On This Page

Brightcove Player Sample: Download Video Plugin

Product(s)

Video Cloud

Role(s)

Player Developer

Topic(s)

Code Samples

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

The content in this document applies to Video Cloud only. Also, since iOS devices do not have a user-accessible file system, the plugin will not work on iOS devices.

Player example

You will 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. This plugin does work with videos in a playlist.

BROWSER DOWNLOAD BEHAVIOR: Browsers have changed how they handle file downloads. Safari, Firefox and Chrome all open the downloaded video in a new window or tab, which generally is not desired behavior. For this reason this sample uses a client-side JavaScript tool to force browsers to do a proper download of the video. For detailed information of the tool see https://www.npmjs.com/package/downloadjs.

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

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 image that downloads the video inside an HTML div element.

Place the download video image element in the DOM.

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.

Assign the largest video to a variable

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 to dynamically build an HTML element that functions as a download button. An example of how the dynamically created element might appear follows:

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.

Application styling

The CSS sizes and positions the download button for placement in the controlbar.

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:

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.

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.

Note: This implementation will NOT work as a plugin when using the iframe player implementation. This is because the <p> tag set as the insertion point is external to the player code, and thus not reachable by the plugin, whose context is the player which is in the iframe.

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.