Upload videos within your cms
Last updated on Feb. 17, 2010
We recently updated our API to support video uploads with a regular HTML form. Using this method, it now is extremely easy to upload content to your BOTR account. This method also ensures the video goes directly from the client to our server, not passing through yours. On top of that, we added progress polling to our upload server. This allows you to display a progress bar in your application by polling our server with a little bit of AJAX.
Introduction
A video upload to Bits on the Run consists of a few steps. These are described in detail in the API documentation. Here's a high-level overview:
- First, the client does a /videos/create API call. This call sets up our server to receive the video. The server returns an object with upload data.
- Second, the client uses the returned object to construct an upload URL. The video upload form should be posted to this URL.
- Third, the client posts the video to the upload URL. Using javascript, the upload progress can be displayed.
- Fourth, after the upload has completed the server redirects to a page on the client's site again. The client can display an Upload completed message on this redirected page and show more info on the uploaded video.
Demo
Since actual code always work better than words, here's a demo of the upload process. It uses PHP for the serverside and jQuery for the clientside scripting. The full code of this demo is included in the PHP API Examples download.
In the following section, we'll re-build this upload demo.
Upload page setup
Let's start with an upload page (e.g. "index.php"). As always, we first include and initialize the PHP API Kit. The kit takes care of serializing and sending all API calls. Replace the xxxx and yyyy part with your account key and secret, which can be copy/pasted from the account page of the CMS.
require_once('botr/api.php');
$botr_api = new BotrAPI('xxxx','yyyyyyyy');
Next, we'll do a /videos/create API call. This call returns a link object we can use to construct the actual upload URL. Note that the token is attached as last parameter to the upload URL. This is needed to do any javascript progress polls. Also note that the redirect-address is the page the server will redirect to after the upload has finished. In this case, it's a show.php file in the same directory.
$response = $botr_api->call('/videos/create');
if ($response['status'] == 'error') { die(print_r($response)); }
$url = 'http://'.$response['link']['address'].$response['link']['path'];
$url .= '?key='.$response['link']['query']['key'];
$url .= '&api_format=xml';
$url .= '&redirect_address=http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"].'show.php';
$url .= '&token='.$response['link']['query']['token'];
With the upload URL set up, we can construct the actual HTML upload form. The upload URL is printed in the action attribute of the form. Note the file input element must be named file.
<form id="uploadForm" action="<?=$url?>" method="POST" enctype="multipart/form-data">
<fieldset>
<label>Select video</label>
<input id="uploadFile" type="file" name="file" />
<button type="submit" id="uploadButton">Upload</button>
</fieldset>
</form>
Redirect page setup
With the upload page done, we can take a look at the redirect page. Again, this page should start by initializing the BOTR API.
require_once('botr/api.php');
$botr_api = new BotrAPI('xxxx','yyyy');
Next, this page can grab the video_key from the HTTP GET array and use it to call for info on the uploaded file. Our upload server always sends the video_key to a redirect page. Subsequently, the PHP simply pushes all properties of the video in a fieldset.
$video_key = $_GET['video_key'];
$response = $botr_api->call('/videos/show', array('video_key'=>$video_key));
if ($response['status'] == 'error') { die(print_r($response)); }
$properties = '';
foreach($response['video'] as $key =>$val) {
$properties .= "<label>".$key."</label><p>".$val."</p>";
}
The only thing left is to print all data in HTML. We kinda abusing the form tag here, but any HTML is fine.
<form>
<fieldset>
<label>thumb</label>
<p><img src="img/processing.gif" alt="<?=$video_key?>" id="pollThumb" /></p>
</fieldset>
<fieldset id="pollProps">
<?=$properties?>
</fieldset>
</form>
Adding javascript
With all the PHP code in place, we have a working video upload application. This is fine by itself, but some additional javascripting will greatly improve the user experience. These javascripts are included in the PHP API Examples download and will take care of:
- Displaying upload progress on the upload page. Javascript will inject a progress DIV into the upload form and start an AJAX polling process. The progress div is resized from 0% to 100% during the upload, giving the user visual feedback. This script is built on top of jQuery.
- Updating the video status on the redirect page. Javascript will poll for the video status and display a thumbnail of the video when available (this usually takes ~10 seconds). If the first conversion is done, javascript will also display a preview page link. This script is built on top of jQuery and a separate PHP file that does the actual API call. Make sure to insert your API key/secret into that PHP file too!
We'll not walk through the javascripting in this tutorial, but note you can simply include the script references in the head of your page to get things working. Both the upload and redirect page continue to work fine without any javascripting.
As always, if you have questions or additional code you want to share, please post to our support forum. Our entire team is active there.
