Transcode your favourite videos using AWS Lambda and Elastic Transcoder

Published by Vignesh M on

Video transcoding has been made easier and effective using the Elastic Transcoder service from Amazon. In this post, we will see how to make use of Elastic Transcoder to transcode the videos we upload to Amazon S3 bucket. Also, we will make use of AWS Lambda which will invoke the Elastic Transcoder whenever a new Object is added to S3 bucket.

Create S3 Buckets

In this example, we will use two S3 buckets say input and output bucket. The object (video file to be transcoded) will be uploaded into input bucket. The output bucket will have the transcoded videos. For this example, the input bucket is created with the name input.videos-to-transcode and the output bucket is output.transcoded-videos. Both the buckets are created in the US-EAST region(N. Virginia).

For detailed steps on working with S3 buckets, refer the post Getting started with S3.

Note: Since the Bucket name is unique through out, you have to choose a different bucket name other than the one mentioned in this example.

Elastic Transcoder

In Elastic Transcoder, pipelines are the queues to which the transcoding jobs can be added. Elastic Transcoder process the jobs in the order in which it is placed in the pipeline. Let’s configure the pipeline using the Elastic Transcoder console.

Create a pipeline

  • In the Elastic Transcoder console, choose Create a new Pipeline.
  • Configure the pipeline by providing the input and output bucket details as below.

  • For IAM role, choose Create console default role. AWS will automatically create the IAM role Elastic_Transcoder_Default_Role.
  • You can review the IAM policy attached to the default role.

  • For Notifications and Encryption sections, leave the default values.
  • Choose Create Pipeline.
  • You will now have the pipeline created with status as Active. Make a note of the pipeline id.

AWS Lambda Function

To summarize the things we have done so far, we have created input and output S3 buckets for placing the video files. Then we have configured the Elastic Transcoder pipeline. Now we need to connect these two pieces i.e S3 buckets and Elastic Transcoder so that when we upload some objects(video file) in S3 bucket the transcoding needs to be done for the video files. So how can we do that?

Here comes the AWS Lambda to our rescue. Yes. Lambda acts as a glue between the S3 bucket and the Elastic Transcoder. This is done by configuring the events for new object creation in S3 bucket which will trigger the Lambda function. Then the Lambda function will create the Elastic Transcoder Job and add that to the Transcoder pipeline we have created. Elastic Transcoder then process the Job and produces the transcoded videos in the output S3 bucket.

Creating IAM role(Execution role) for Lambda

Before creating the Lambda function, let’s create an IAM role which will be associated with Lambda when it executes.

  • Access the IAM console.
  • Select RolesĀ  in the left navigation pane and choose Create role.
  • For role type, choose AWS service and select Lambda. Then select Next: Permissions.
  • For Attach permissions policy, search for AWSLambdaBasicExecutionRole and select it.
  • Then search for AmazonElasticTranscoderJobsSubmitter and select it.
  • Choose Next: Review.
  • Then provide the name for the role. For this example, it’s lambda-exec-role_video-transcoder.
  • Choose Create role.

Author the Lambda Function

Now it’s time to write the Lambda function itself. In this example, we will use Java language to write the function. For this, you need Java 8, Eclipse IDE, AWS Toolkit for Eclipse, Maven.

You can get the source code for this example from here. Also the download link is available at the end of this post.

Step 1 :

Add the below dependencies and plugins in your pom.xml

Step 2: Then create a class TranscodeJobCreator.java which will handle the requests by implementing the RequestHandler interface.

Step 3: Compile and build the project using Maven goal as package. After successful build then run the Maven build again using the goal as package shade:shade. In the target folder of the project, you should have the jar videotranscoder-4.0.0.jar.

Deploy the Lambda Function

Now that we have written the Lambda function, we need to deploy it so that the function can be triggered when certain events occur.

  • Access the Lambda console. Choose region as N. Virginia.
  • Select Create function. Choose Author from scratch.
  • We will configure the Trigger in the following steps. For now leave it and choose Next.
  • Give a name for the function. For this example, it’s func_transcode-videos.
  • Select the Runtime as Java 8.

  • Now for Code entry type, upload the videotranscoder-4.0.0.jar available in the project’s target folder.
  • Select Upload and choose the jar file.
  • For Environment variables, we need two entries to be provided as we have used it in the handler code we have written.
  • For ELASTIC_TRANSCODER_PIPELINE_ID, give the value of pipeline id.
  • For OUTPUT_VIDEO_FORMAT, we choose mp4 for this example.

  • For function handler give the value as com.rayfocus.aws.lambda.videotranscoder.TranscodeJobCreator.
  • For Role, choose the existing role lambda-exec-role_video-transcoder.

  • In Advanced settings, change the Timeout value to 30 seconds so that Lambda function will have enough time to complete the execution. Leave the other settings unchanged.
  • Then choose Next.

  • Review the Lambda function details and select Create function.
  • Now you should see the message for successful creation of Lambda function.

Configure the Triggers for AWS Lambda

  • To configure the triggers for Lambda, select the function created in previous step and choose Triggers section.
  • Then select Add trigger.

  • Select the trigger type as S3.
  • Then select the input S3 bucket as event source. For Event type choose Object created(All).
  • Enable the trigger and choose Submit.
  • Now the trigger should be added successfully to the Lambda function.

Test the Lambda Function

Now finally it’s time to test the video transcoding. Upload the sample video file into the input S3 bucket. You should get the transcoded videos in the output S3 bucket.

Input Video File

 

Transcoded Output Video Files

 

Conclusion and What’s Next?

Congratulations! You have now done the video transcoding using Elastic Transcoder and AWS Lambda. We have seen how the Lambda function can be leveraged to achieve the transcoding functionality without configuring any servers by ourselves. This gives us the way to go ahead and explore more about the serverless architectures.

We can have a thick client now talking to NoSQL database directly or by calling the API Gateway endpoint which will invoke Lambda function. The Lambda function will then do the database operations. There are plenty of options available to design the applications using serverless way in the Cloud.

Let’s go serverless!

That’s it for this post. If you face any errors in the example explained in this post, check the Cloud watch logs which will have the error details captured during Lambda invocations.

If you have any questions or issues, please share it in the comments section.

Thank you!


Download source code : example_videos-transcoding.zip



Vignesh M

Java developer , AWS Certified Solutions Architect Associate and Cloud technology enthusiast. Currently working for Hexaware technologies. He believes that knowledge increases by sharing not by saving.

4 Comments

Sri · September 29, 2017 at 11:20 am

Awesome da machi

    Vignesh M · October 13, 2017 at 6:21 am

    Thank you Sri!

Michael · October 24, 2018 at 9:26 am

Any idea how to edit the lambda function of AWS config to generate a thumbnail of the uploaded video?

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.