Make Your Videos Downloadable

Want to allow your viewers to download a copy of the current video?

Here's a way to automate the generation of a download link, provided that you have a URLAccess Read Token for the Media API. The account delivery method must also be progressive download, or have Universal Delivery Service enabled. I'm going to use the find_video_by_id method (borrowing the JavaScript Media API wrapper from opensource.brightcove.com to simplify the code). This is just the scripting part, but you can view the complete sample page at http://files.brightcove.com/BCLAutomatedVideoDownload.html.

		<script language="JavaScript" type="text/javascript" src="http://admin.brightcove.com/js/BrightcoveExperiences.js"></script>
<script type="text/javascript" src="http://admin.brightcove.com/js/APIModules_all.js"> </script>
<!-- add MAPI wrapper from http://opensource.brightcove.com/ -->
<script type="text/javascript" src="http://files.brightcove.com/bc-mapi.js"></script>
<!-- add logging script for debugging -->
<script type="text/javascript" src="http://files.brightcove.com/BCLlogging.js"></script>
<!-- custom script -->
<script type="text/javascript">
/* vars */
var BCLplayer;
var BCLexperienceModule;
var BCLvideoPlayer;
var BCLcurrentVideo;
/* initial event listeners */
function onPlayerError(event) {
log("info: " + event.info);
}
function onPlayerLoaded(id) {
newLog();
log("EVENT: onPlayerLoaded");
BCLplayer = brightcove.getExperience(id);
BCLexperienceModule = BCLplayer.getModule(APIModules.EXPERIENCE);
}
function onPlayerReady(event) {
log("EVENT: onPlayerReady");
// get a reference to the video player module
BCLvideoPlayer = BCLplayer.getModule(APIModules.VIDEO_PLAYER);
// add a listener for media change events
BCLvideoPlayer.addEventListener(BCMediaEvent.BEGIN, onMediaBegin);
}
// listener for media change events
function onMediaBegin(event) {
log("EVENT: MediaBegin");
// get the ID of the current video as it starts playing
BCLcurrentVideoID = BCLvideoPlayer.getCurrentVideo().id;
// pass the video ID to getDownloadLink()
getDownloadLink(BCLcurrentVideoID);
}
function getDownloadLink(id) {
// object for search parameters
var searchParams = new Object();
// set Media API token - MUST BE URL ACCESS READ token!!
BCMAPI.token = "jskS1rEtQHy9exQKoc14IcMq8v5x2gCP6yaB7d0hraRtO__6HUuxMg..";
// set callback for Media API call
BCMAPI.callback = "onSearchResult";
// set command
BCMAPI.command = "find_video_by_id";
// set search params
searchParams.video_id = id;
// need this if the delivery for the account is streaming, and Universal Delivery Service must be enabled!
searchParams.media_delivery = "HTTP";
// just get the field we need for better performance
searchParams.video_fields = "FLVURL";
// execute the search
BCMAPI.find(BCMAPI.command,searchParams);
}
// handler for the media API search
function onSearchResult(jsondata) {
log(jsondata.FLVURL);
// create the link and write it into the HTML
var str = "";
str += "<a href='"+jsondata.FLVURL+"'>Download the Video</a>";
document.getElementById("downloadLink").innerHTML = str;
}
</script>