AWS ElasticTranscoding With Laravel Or PHP

Priyadarshini
5 min readOct 2, 2020

Hello Everyone!

I am going to share how to use the AWS Elastic Transcoding service in Laravel

What Is AWS Elastic Transcoder?

As per official documentation,

Amazon Elastic Transcoder lets you convert media files that you have stored in Amazon Simple Storage Service (Amazon S3) into media files in the formats required by consumer playback devices. For example, you can convert large, high-quality digital media files into formats that users can play back on mobile devices, tablets, web browsers, and connected televisions.

Transcoding is the process of converting an audio or video file from one encoding format to another to make the media source compatible to be played on with many target devices.

For example: If a video source is in mp4 format, it may not be played on multiple devices like IOS, Android, etc., devices

Setup needed to get started with AWS Transcoding

AWS Account

AWS S3 bucket

S3 bucket is a cloud storage resource available in AWS. The media files to be transcoded should be available in the S3 bucket. An input bucket for the files to be transcoded and an output bucket for the transcoded files are required. The same bucket can also be used as an input bucket and an output bucket.

Pipeline

The official documentation says that, a pipeline manages the jobs that transcode your files

Presets

The official documentation says that, a preset is a template that contains the settings that you want Elastic Transcoder to apply during the transcoding process, for example, the codec and the resolution that you want in the transcoded file. When you create a job, you specify which preset you want to use.

Jobs

The official documentation says that, a job does the work of transcoding. You specify the name of the file that you want to transcode (the input file), the name that you want Elastic Transcoder to give the transcoded file, the preset that you want Elastic Transcoder to use, and a few other settings. Elastic Transcoder gets the input file from the Amazon S3 input bucket that you specified in your pipeline, transcodes the file, and saves the transcoded file or files in the Amazon S3 output bucket that you specified in the pipeline.

Refer the following link: Getting Started to set up Amazon Elastic Transcoder

In the above setup Creating an AWS account, creating AWS S3 bucket, pipeline, and preset should be managed in the AWS Console.

Proceeding further we will look into creating Jobs to transcode the media source in Laravel dynamically.

Hope, already Laravel installation set up is done and you have previous working experience in this framework, if not refer to the following link, Laravel Installation

Amazon offers an official SDK package supporting AWS services for PHP.

Add AWS SDK for Laravel as a dependency via Composer, ensure that the PHP version should be 5.5 or above.

composer require aws/aws-sdk-php

As mentioned earlier we need an S3 bucket, pipeline, and presets to start transcoding. Hope you have configured the same in the AWS console. Need to add the configurations in .env the file

AWS_BUCKET=<BUCKET NAME>// Enter the bucket name what has been specified in Pipeline settings.
// If you have configured two buckets for input and output, you can have the configuration has,
// AWS_INPUT_BUCKET
// AWS_OUTPUT_BUCKET
AWS_REGION=ap-southeast-1// Specify AWS region, where you have configured your pipeline settingsAWS_VERSION=latest// AWS keys for authentication
// This is required to establish the connection on AWS
// Refer the link How do I create an AWS access key?
AWS_SECRET=<AWS_SECRET_KEY>
AWS_KEY=<AWS_ACCESS_KEY>
//Specify the pipeline ID configured in AWS Console
// In this post we are going to use only single Pipeline, you can also use multiple pipeline
// For a region you can create upto 4 pipelines
AWS_PIPELINE_ID=<AWS_PIPELINE_ID>

Next, we need presets, you can maintain the presets in a table and create a Model for the same. Or add them as the configuration in .env the file

In AWS SDK there is a method to fetch presets from AWS Console, you use it and store it in the database. Each preset has a unique ID.

Fetch Presets & store in Database

First, include the below file in a controller,

use Aws\ElasticTranscoder\ElasticTranscoderClient;

Next, establish the connection as below,

//The below credentials we have already configured in .env file
$credentials = array (
'region' => env('AWS_REGION'),
'version' => 'latest',
'credentials' => [
'key' => env('AWS_KEY'),
'secret' => env('AWS_SECRET')
]);
$AWSClient = ElasticTranscoderClient::factory ($credentials);//Method to get presets from AWS cloud
$AWSClient->listPresets ( array ('PageToken' => <value for accessing the next page of results>) );

We have done all the required setup to proceed with transcoding.

To start transcoding a job similar to presets establish the connection and call the method to create the job, Here since I have already established the connection am using the same going forward.

AWS SDK package has a built-in function to create the job createJob()

// To create Job
$jobResult = $AWSClient->createJob (array(
'PipelineId' => env(AWS_PIPELINE_ID),
'Input' => array ('Key' => <PATH_OF_INPUT_FILE_TO_BE_TRANSCODED_FROM S3_BUCKET>),
'Outputs' => $outputs, //Explained below
'OutputKeyPrefix' => <PATH_OF_OUTPUT_FILE_AFTER_TRANSCODING_INTO_S3_BUCKET>,
'Playlists' => $playlist )); //Explained below
//This is output we need to set for transcoding.
// I have given a sample for ready to start
// Details expansion can be referred here, createJob
$outputs = "Outputs":[{
"Key":"name of the transcoded file should be unique",
"ThumbnailPattern":""|"pattern", //The thumbnail file name
"Rotate":"auto|0|90|180|270", //Set to `auto`
"PresetId":"preset to use for the job", //Pass the preset ID configured in .env or stored in Database.
"SegmentDuration":"[1,60]", Set to 5
}],

ThumbnailPattern:

Presets has a setting to generate thumbnail image from the video source, so you can specify the filename pattern how you wanted the image to be saved as output.

E.g., ‘thumbnail/’.<PRESET_ID>.’/thumb-’ . <PRESET_ID> . ‘-{count}’,

$playlist = "Playlists":[{
"Format":"HLSv3|HLSv4|MPEG-DASH|Smooth",
"Name":"name",
"OutputKeys":[
"Outputs:Key to include in this playlist" //should be unique
]
}]

That’s it the job is created, and it will be triggered in AWS Console under the ‘Jobs’ section

The createJob will give the response in JSON format, it has job ID, which should be stored to trace the status of the Job

Need to use the below method to track the status of the job using the job ID

$jobStatus = $AWSClient->readJob(array('Id' => <JOB_ID>));// The status would be Submitted, Progressing, Completed or Error.

That’s it you have completed transcoding!!!

--

--

Priyadarshini

Application Developer | Fullstack | Newbie in to Microservices Architecture | Tech enthusiastic