Easy Video Uploads with Echove SDK

Technology
Media API
Required
Media API token, Echove PHP SDK
Edition
Pro, Enterprise

In this article we'll go over how to easily upload videos to your Brightcove account through a form. We'll be using Echove, an open-source PHP SDK for interacting with the Brightcove Media API. For an example of updating your videos with Echove, check out this other article.

If you haven't already downloaded Echove, you can get it at the Google Code page; documentation is available on the Echove website.

Note: For a more up-to-date open-source PHP SDK for the Brightcove Media API, check out the PHP MAPI Wrapper, available at Open Source @ Brightcove.

For each section we'll take a look at the code base, then we'll go back over it line-by-line to understand what's happening.

Upload Form

<form method="post" action="upload_video.php" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="8000000" />
    Title: <input type="text" name="title" /><br />
    Short Description: <textarea name="shortDescription"></textarea><br />
    File: <input type="file" name="video" /><br />
    <input type="submit" />
</form>

As you can see we've created a simple upload form that posts our data to another page. We're sending along a title, a short description, and the video file.

Form Handler

<?php

    # Include & Instantiate
    require('../echove.php');
    $bc = new Echove(
        'z9Jp-c3-KhWc4fqNf1JWz6SkLDlbO0m8UAwOjDBUSt0.',
        'z9Jp-c3-KhWdkasdf74kaisaDaIK7239skaoKWUAwOjDBUSt0..'
    );

    # Create new metadata
    $metaData = array(
        'name' => $_POST['title'],
        'shortDescription' => $_POST['shortDescription']
    );

    # Rename the video file
    $file = $_FILES['video'];
    rename($file['tmp_name'], '/tmp/' . $file['name']);
    $file_location = '/tmp/' . $file['name'];

    # Send video to Brightcove
    $id = $bc->createMedia('video', $file_location, $metaData);

?>

In the first few lines, we're simply including the Echove SDK and instantiating it with our Media API Read and Write tokens.

Next we're setting the metadata for our video; in this example we'll just supply a name and short description. You can pass any item from the Video DTO, though.

The next step is a bit more difficult. When a file is uploaded onto the server, it is placed in our temporary directory and given a random string as a name; because Brightcove only accepts specific file types, we need to rename the file to include its file extension. The easiest way is to simply rename the file to its original file name.

Now all we have to do is call our createMedia method from Echove, passing in the file location and our metadata for the new video. This method returns the Video ID of the newly uploaded file, which we can save for use later on.