Marc Rasmussen
Marc Rasmussen

Reputation: 20555

Node.js using amazon transcoder to format video / audio files

My goal is to make sure that all videos that are being uploaded to my application is the right format and that they are formatted to fit minimum size.

I did this before using ffmpeg however i have recently moved my application to an amazon server.

This gives me the option to use Amazon Elastic Transcoder

However by the looks of it from the interface i am unable to set up automatic jobs that look for video or audio files and converts them.

For this i have been looking at their SDK / api references but i am not quite sure how to use that in my application.

My question is has anyone successfully started transcoding jobs in node.js and know how to convert videos from one format to another and / or down set the bitrate? I would really appreciate it if someone could point me in the right direction with some examples of how this might work.

Upvotes: 1

Views: 4647

Answers (2)

Gökhan Ayhan
Gökhan Ayhan

Reputation: 1299

If you want to generate master playlist you can do it like this. ".ts" files can not playable via hls players. Generate ".m3u8" file

async function transcodeVideo(mp4Location, outputLocation) {
let params = {
    PipelineId: elasticTranscoderPipelineId,
    Input: {
        Key: mp4Location,
        AspectRatio: 'auto',
        FrameRate: 'auto',
        Resolution: 'auto',
        Container: 'auto',
        Interlaced: 'auto'
    },
    OutputKeyPrefix: outputLocation + "/",
    Outputs: [
        {
            Key: "hls2000",
            PresetId: "1351620000001-200010",
            SegmentDuration: "10"
        },
        {
            Key: "hls1500",
            PresetId: "1351620000001-200020",
            SegmentDuration: "10"
        }
        ],
    Playlists: [
        {
            Format: 'HLSv3',
            Name: 'hls',
            OutputKeys: [
                "hls2000",
                "hls1500"
            ]
        },
    ],
};

let jobData = await createJob(params);
return jobData.Job.Id;

}

async function createJob(params) {
return new Promise((resolve, reject) => {
    transcoder.createJob(params, function (err, data) {
        if(err) return reject("err: " + err);
        if(data) {
            return resolve(data);
        }
    });
});

}

Upvotes: 0

Gergo
Gergo

Reputation: 2290

However by the looks of it from the interface i am unable to set up automatic jobs that look for video or audio files and converts them.

The Node.js SDK doesn't support it but you can do the followings: if you store the videos in S3 (if not move them to S3 because elastic transcoder uses S3) you can run a Lambda function on S3 putObject triggered by AWS.

http://docs.aws.amazon.com/lambda/latest/dg/with-s3.html

My question is has anyone successfully started transcoding jobs in node.js and know how to convert videos from one format to another and / or down set the bitrate? I would really appreciate it if someone could point me in the right direction with some examples of how this might work.

We used AWS for video transcoding with node without any problem. It was time consuming to find out every parameter, but I hope these few line could help you:

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

aws.config.update({
  accessKeyId: config.AWS.accessKeyId,
  secretAccessKey: config.AWS.secretAccessKey,
  region: config.AWS.region
});

var transcoder = new aws.ElasticTranscoder();

let transcodeVideo = function (key, callback) {
    // presets: http://docs.aws.amazon.com/elastictranscoder/latest/developerguide/system-presets.html
    let params = {
      PipelineId: config.AWS.transcode.video.pipelineId, // specifies output/input buckets in S3 
      Input: {
        Key: key,
      },
      OutputKeyPrefix: config.AWS.transcode.video.outputKeyPrefix, 
      Outputs: config.AWS.transcode.video.presets.map(p => {
        return {Key: `${key}${p.suffix}`, PresetId: p.presetId};
      })
    };

    params.Outputs[0].ThumbnailPattern = `${key}-{count}`;
    transcoder.createJob(params, function (err, data) {
      if (!!err) {
        logger.err(err);
        return;
      }
      let jobId = data.Job.Id;
      logger.info('AWS transcoder job created (' + jobId + ')');
      transcoder.waitFor('jobComplete', {Id: jobId}, callback);
    });
  };

An example configuration file:

let config = {
  accessKeyId: '',
  secretAccessKey: '',
  region: '',
  videoBucket: 'blabla-media',
  transcode: {
    video: {
      pipelineId: '1450364128039-xcv57g',
      outputKeyPrefix: 'transcoded/', // put the video into the transcoded folder
      presets: [ // Comes from AWS console
        {presetId: '1351620000001-000040', suffix: '_360'},
        {presetId: '1351620000001-000020', suffix: '_480'}
      ]
    }
  }
};

Upvotes: 12

Related Questions