Integrating AWS Elastic Transcoder With NodeJS Application

Posted By : Vishal Yadav | 13-May-2020

Introduction

Elastic transcoding is a media transcoding service provided by AWS. It is highly scalable, cost-effective, and easy to use. It is used to convert media into a suitable format which is playable on different devices like smartphones, tablet, PC, smart tv, etc.

Elastic transcoder converts/transcode video into other formats based on PresetId. Before starting video transcoding, you have to set up an Elastic Transcoder on AWS and get PipelineID for it.

PresetId: AWS provides a list of transcoding formats likes HLS streaming, MPEG-DASH, Webm or you can simply transcode it to different resolutions. All these formats have a unique PresetId which can be used to initiate a transcoding job.


Prerequisites

  • AWS IAM account with access to AWS Elastic transcoder
  • NodeJS
  • NPM

 

1. Install the AWS SDK for NodeJs and configure it.

//Install AWS SDK
npm I aws-sdk

//Create a S3 object and configure it as per credentials

const AWS = require('aws-sdk');

//Create a object that contains access keys
var credentials = {
       accessKeyId: process.env.AWS_ACCESS_KEY,
       secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
};

//Create an Object to start the transcoding Job.
var Etrans = new AWS.ElasticTranscoder({
      apiVersion: '2012–09–25', //Different API versions are provided by s3
      region: 'us-east-1', //Bucket Region
      credentials: credentials,
      videoBucket: 'Bucket_Name',
});

 

2. Create a new transcoding job.

//Create a JSON object to be passed as parameter
var params = {
      PipelineId: pipelineId, //PipelineId of Elastic transcoder
      OutputKeyPrefix: newKey + '/',
      Input: {
      Key: srcKey,  //Source path of video 
      FrameRate: 'auto',
      Resolution: 'auto',
      AspectRatio: 'auto',
      Interlaced: 'auto',
      Container: 'auto'
      },
      Outputs: [{
      Key: 'transcoded/video',
      PresetId: 'Preset_ID',
      "SegmentDuration":'3', //Duration in segment on which transcoding is done as we chose HLS streaming
      ThumbnailPattern: 'poster-{count}', //It is used to create snapshot of Video
      }]
      }

//To create a new Job

Etrans.createJob(params, function(err, data){
      if (err)
      {
        console.log('error is :', err); 
      } 
      else {
        console.log('data is',data);
      }
})

 

3. Read the transcoding Job-status.

// Transcoding Job can take some time so you need to add ReadJob to check status.

 Etrans.readJob({ Id: data.transcodeId }, (err, data) => {
        if (err){
        console.log(err);
        reject(err);
        } 
        else{
         console.log(data)
        }
})

 

Conclusion

The above code can be used to transcode any video stored on the s3 bucket into the required format. Basically, transcoding service is used to convert media into streaming formats.

About Author

Author Image
Vishal Yadav

Vishal has good knowledge of MEAN stack, JavaScript,HTML,CSS. He has worked on both front-end and back-end. He likes to learn new technologies.

Request for Proposal

Name is required

Comment is required

Sending message..