Encoding video files with Zencoder API

Here is a small tutorial on how to use zencoder API for encoding your video files into different formats, this API really works great and provide the best result in very less time. You can download the PHP library which is really easy to use.

This request response works in 2 steps, 1st step where a job is created on zencoder server after your request with encoding formats, and in 2nd step you get the notification when video is encoded successfully, which ensures you that job finished successfully.

So  lets start with step 1 where you need to create a request / job for video encoding.

Creating a job/request with zencoder API:

Simply do it in this way,

$encoding_job = new ZencoderJob('
 {
   "api_key": "93h630j1dsyshjef620qlkavnmzui3",
   "input": "s3://bucket-name/file-name.avi",
   "outputs": [
    {
      "label": "web"
    }
  ]
 }
');

Where api_key is your api_key which you can get after register on zencoder.com, “input” is the file location with filename which you want  to get encoded in different formats. And most important is “Outputs” section where you can simply give many options for your output file. All the options are mentioned here http://zencoder.com/docs/api/ once your job gets created you can check it with following line of codes:

if ($encoding_job->created) {
      // Success
      echo "w00t! \n\n";
      echo "Job ID: ".$encoding_job->id."\n";
      echo "Output '".$encoding_job->outputs["web"]->label."' ID: ".$encoding_job->outputs["web"]->id."\n";
      // Store Job/Output IDs to update their status when notified or to check their progress.
 } else {
       // Failed
       echo "Fail :(\n\n";
       echo "Errors:\n";
       foreach($encoding_job->errors as $error) {
        echo $error."\n";
     }
}

If $encoding_job->created then job is created successfully else it fails and most common reason is json request was not created properly. So check your json request again if it fails.

So above is first step where you have made your request now the second step:

When zencoder completes the encoding job it will send the notification, for getting notification some parameters with the output needs to be set, following is the example for sending notification parameter with the request:

$encoding_job = new ZencoderJob('
{
   "api_key": "93h630j1dsyshjef620qlkavnmzui3",
   "input": "s3://bucket-name/file-name.avi",
   "outputs": [
   {
      "label": "web",
       "notifications":[
      {"format": "json", "url": "http://example.com/notification.php"}
     ]
   }
 ]
}
');

Once encoding completes on zencoder server it will hit the notification url for the response. For more details please check http://zencoder.com/docs/api/#notifications

Now for getting the response data, PHP library of zencoder provide a class ZencoderOutputNotification simply call in this way

$notification = new ZencoderOutputNotification();

$response_data = $notification ->catch_and_parse();

now $response_data will have the object array containing information of the job and state of the job, you can simply find the job is finished or not by this:

if( $response_data->job->state == “finished”)
{
   //do whatever processing you want.
}

Other parameters comes in the response data is:

{"job":{"state":"finished","test":true,"id":2281},
"output":{"state":"finished",
"url":"http://s3.amazonaws.com/some-example/file.webm",
"label":"WEBM","id":332222}}

18 thoughts on “Encoding video files with Zencoder API”

  1. Thank you so much for this tut, just a quick question, how can I implement the PHP library on a shared host environment for drupal please?

  2. I don’t have specific experience with drupal, but i think you can create a drupal module which can include this library.

  3. Hi Savitas,

    Please forgive me if I am being a complete potato head, but if I create a simple trial.php file and enter the information you have provided (editing the api-key and input + output details) then visit that file within my browser to trigger the function call, would that be enough to get the ball rolling?

    I am baffled by all this api php but would really like to work it out?

    Sincere thanks for your time.

    Buzzy :)

  4. Hey Savita i just loved the post. It is very helpful for a beginner like me.
    But i have a query. How can we do the same with XML request.
    I mean here the request is sent through JSON data format, how can we do with XML data.

    Thanks in advance :)

  5. hey i got this error.. what should i do???

    Fatal error: Class ‘ZencoderOutputNotification’ not found in /home/content/9/html/kulkarni/zencoder/Services/notifications.php on line 3

  6. actully i stuck with notifications… i am done with other part..only i want to know how to receive notifications..

  7. thak, i was looking forward to something like this, though its old but i am gona tray it. My motive is to integrate it wit codeigniter

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>