Amazon Elastic Transcoder PHP Example

Don't get me wrong the documentation of the amazon web services are great, but for the elastic transcoder I just wanted a little piece of example code so I don't have to filter an 300 line arguments array for a simple operation.

Aws PHP SDK

Don't forget to install the AWS PHP SDK using composer.

"require":
{
        "aws/aws-sdk-php": "2.7.*@dev"
}

Example

I'm not going to explain how to setup the transcoding pipeline on the aws console. If you haven't done that yet check out this tutorial.

So that you don't have to filter that huge thing here is a simple example:

AWS instance

First of all we need to create a new elastic transcoder client instance.

use Aws\ElasticTranscoder\ElasticTranscoderClient;

$elasticTranscoder = ElasticTranscoderClient::factory(array(
    'credentials' => array(
        'key' => 'your key',
        'secret' => 'your secret',
    ),
    'region' => 'eu-west-1', // dont forget to set the region
));

Creating a new Job

Now we can create a new job with the elastic transcoder client.

$job = $elasticTranscoder->createJob(array(

    'PipelineId' => '<your pipeline id>',

    'OutputKeyPrefix' => 'your/output/prefix/',

    'Input' => array(
        'Key' => 'key/to/your/input/file.mp4',
        'FrameRate' => 'auto',
        'Resolution' => 'auto',
        'AspectRatio' => 'auto',
        'Interlaced' => 'auto',
        'Container' => 'auto',
    ),

    'Outputs' => array(
        array(
            'Key' => 'myOutput.mp4',
            'Rotate' => 'auto',
            'PresetId' => '<your trancoding preset id>',
        ),
    ),
));

// get the job data as array
$jobData = $job->get('Job');

// you can save the job ID somewhere, so you can check 
// the status from time to time.
$jobId = $jobData['Id'];

 Checking a jobs status

You probably want to notify someone when the transcoding is done, you could create an callback, but that would require your application to be accessible at one point from the outside. The other method is polling the status until the jobs status changes to success or error.

$elasticTranscoder->readJob(array('Id' => $jobId));
$jobData = $resposne->get('Job');

if ($jobData['Status'] !== 'progressing'  
    && $jobData['Status'] !== 'submitted') 
{
    echo $jobData['Status'];
}

Thats it, if you need help let me know.

Mario Döring

Mario Döring

http://clancats.ch

Software Engineer, ClanCats Dude, cineman.ch Developer and general Idiot.

View Comments
Navigation