admin管理员组

文章数量:1292094

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.

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.

Share Improve this question edited Dec 19, 2018 at 16:15 Guilherme David da Costa 2,3684 gold badges32 silver badges47 bronze badges asked Mar 8, 2017 at 14:44 Marc RasmussenMarc Rasmussen 20.6k83 gold badges223 silver badges383 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12

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./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./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'}
      ]
    }
  }
};

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);
        }
    });
});

}

本文标签: javascriptNodejs using amazon transcoder to format videoaudio filesStack Overflow