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}}