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",
      {"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:


18 responses to “Encoding video files with Zencoder API

  1. Looks great! Thanks savitas.

  2. savitas

    Hey!!!! thanks Steve, nice to see your comment :)

  3. Thanks steve, *super* useful!

  4. Oh sorry, “savitas” i meant

  5. savitas

    thanks! chichilatte

  6. @chichilatte
    Then we should say thankyou to both (Steve wrote the api…)

  7. Hello everybody,

    I think after reading you got the php part.
    If you would like to go one step further now, check this:


    It may give you some hints how to do communication the ajax way
    (with progressbars and player)…
    Code is downloadable and commented. Have fun.

  8. Buzzy

    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?

  9. savitas

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

  10. Buzzy

    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 :)

  11. savitas

    Yeah I think that is enough.
    If still its not working then you can first try here https://app.zencoder.com/request_builder with all your input and output parameters, also check your job status https://app.zencoder.com/jobs after login. Also example here was quite useful for me https://github.com/zencoder/zencoder-php

  12. Buzzy

    Thank you soooooooo much!

    It works,

    Many Blessings. :)

  13. savitas

    :) great!!!

  14. akhila

    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 :)

  15. sushant

    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

  16. sushant

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

  17. 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

  18. savitas

    Great, implement it in CI and release some useful library for this :)

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>