Generate thumbnails from video with AWS Elastic Transcoder
Are you someone looking to generate thumbnails from a video but not sure how to go about it?
Users have consistently found traditional transcoding to be complicated. Customers must first purchase and operate transcoding software, which may be costly and difficult to maintain and set up. Second, creating transcoded output for various devices frequently requires trial and error to discover the ideal transcoding settings that play well and work well for the end-user. This iterative procedure wastes computational resources. Third, standard encoding solutions do not scale up and down to meet the demands of clients’ businesses. Instead, with traditional solutions, clients may need to predict the capacity they require ahead of time, resulting in either wasted revenue or business delay.
Hence, a simple and easy solution is to use Amazon Elastic Transcoder.
So, what is Amazon Elastic Transcoder?
Amazon Elastic Transcoder is media transcoding done in the cloud. It is designed to be a highly scalable, easy-to-use, and cost-effective way for developers and businesses to convert/transcode media files from their source format into versions that can be played on consumer devices. We can convert the media files to play back on smartphones, tablets, and PCs.
In this post, I have explained how you can use Amazon S3, AWS Lambda, and Amazon Elastic Transcoder to detect the frames in a video and generate thumbnails.
- Source S3 bucket(jvelliah-aws-s3-videos) is used to upload video files.
- An IAM role(Elastic_Transcoder_Lambda_Role) grants Elastic Transcoder the required permissions to access S3 buckets.
- The Elastic Transcoder Pipeline(blog-transcoder-pipeline) is a queue that will manage the transcoding jobs.
- Source S3 bucket calls a Lambda function(Transcode_Videos) to create a new job in Elastic Transcoder.
- Elastic Transcoder job creates video thumbnails in .png format and uploads them into the destination(jvelliah-aws-s3-thumbnails) S3 bucket.
S3 Buckets
An Amazon S3 bucket is a public cloud storage resource provided by Amazon Web Services (AWS) Simple Storage Service (S3). Amazon S3 buckets, analogous to file folders, hold objects that include data and associated descriptive information.
If I want to upload my video and save transcoded files, I must create two Amazon S3 buckets in one AWS region.
Here, I have created the following buckets:
Elastic Transcoder Pipeline
Elastic Transcoder Pipelines are queues used to handle transcoding jobs. Whenever you create a job, you select which Pipeline to add the work. Elastic Transcoder begins processing tasks in a pipeline within the order they were added. If you design a task to transcode into several formats, Elastic Transcoder produces files for each format in the sequence you specify in the job.
I have created my new Pipeline by clicking on the Create New Pipeline button in the Elastic Transcoder section.
IAM Role
An IAM role is an IAM entity that refers to a set of permissions for requesting AWS services. Then I create a new IAM role and attach the following permissions to the new role.
AWS Lambda
AWS Lambda is a serverless computing system that runs your code in response to events and maintains the underlying resources for you automatically. These events might involve status changes or updates, such as a user adding an item to a shopping basket on an ecommerce store. Here, I use an AWS Lambda function with the Runtime option is set to use Node.js to create transcoder jobs using the Pipeline I have just configured whenever a new video uploaded to the source bucket. To give Lambda function permissions, I assigned the role created in the previous step. Also, I have attached the S3 ObjectedCreated trigger with the lambda function to call to create a transcoding job.
Code
I clicked on the name of the Lambda function in the Designer panel and replaced the code with:
"use strict";
var AWS = require("aws-sdk");
var ElasticTranscoder = new AWS.ElasticTranscoder({
apiVersion: "2012–09–25",
region: "us-east-1", // TODO: Replace with your region
});
exports.handler = function (event, context) {
var pipelineId = "1642284311566-c7dpw8"; // TODO: Replace with your pipeline ID
var presetId = "1351620000001-000010"; // TODO: Replace with your preset ID (https://docs.aws.amazon.com/elastictranscoder/latest/developerguide/system-presets.html)
var sourceBucket = event.Records[0].s3.bucket.name;
var sourceFilename = event.Records[0].s3.object.key;
var srcKey = decodeURIComponent(sourceFilename.replace(/\+/g, " "));
var destinationFolder = sourceFilename.replace(/\./, "-");
var params = {
PipelineId: pipelineId,
Input: {
Key: srcKey,
FrameRate: "auto",
Resolution: "auto",
AspectRatio: "auto",
Interlaced: "auto",
Container: "auto",
},
Outputs: [
{
Key: destinationFolder + "/preview.mp4",
PresetId: presetId,
Composition: [
{
TimeSpan: {
StartTime: "0",
Duration: "30",
},
},
],
},
{
Key: destinationFolder + "/web.mp4",
ThumbnailPattern: destinationFolder + "/thumbs-{count}",
PresetId: presetId,
},
],
};
ElasticTranscoder.createJob(params, function (err, data) {
if (err) {
context.fail();
return;
}
});
};
This code will create two videos in the destination S3 bucket and a set of thumbnails. The web.mp4 video will be a full-length version and preview.mp4 will be a 30-second preview clip. I am using the generic 720p pre-set for the videos.
I saved my changes to the Lambda function code.
Notification
I configured a new SNS topic, subscribed to another Lambda function, and attached it with the Pipeline. The SNS will notify me when the final files are ready.
Test and Output
It’s now time to test the overall flow. I need to upload a video file to the source bucket. The upload activity should trigger the Transcode-Videos Lambda function to create a transcoding job. If the transcoding job goes well, the transcoded output files should appear in the destination bucket and the notifications sent out as displayed below:
Logs
AWS Lambda automatically monitors the functions on my behalf and writes the logs in Amazon CloudWatch. The log records will help me to troubleshoot failures in a Lambda function.
To wrap it up
With this AWS Elastic Transcoder, we can design similar architectural solutions to meet different requirements. For example, we could use the thumbnails to find distinct people in a video with Amazon Rekognition. Rekognition Video extracts and analyses motion-based context from saved or live stream footage. This will be the focus of our next article. So, don’t forget to check it out.
Published on:
Learn more