本文提供了Node.js SDK转码的操作步骤及完整的代码示例。

操作步骤

  1. 创建Client实例。
    var RPCClient = require('@alicloud/pop-core').RPCClient;
    
    var client = initVodClient('<Your AccessKeyId>', '<Your AccessKeySecret>', '<Your RegionId>');
  2. 调节转码参数。
    • Input(作业输入)
      input ={Location: "ossLocation",
              Bucket: "ossBucket",
              Object: encodeURIComponent(ossInputObject)
      };
    • Output(作业输出)
      output={Output: encodeURIComponent(ossOutputObject)};
    • Video(视频)
      video = {Codec:"H.264",
               Bitrate:"1500",
               Width:"1280",
               Fps:"25",
      };
      output.Video=video;
    • Audio(音频)
      audio = {Codec:"AAC",
      	 Bitrate:"128",
               Channels:"2",
               Samplerate:"44100"
      };
      output.Audio=audio;
    • TemplateId(转码模板ID)
      templateId='Your TemplateId';
      output.TemplateId=templateId;
    • PipelineId(管道ID)
      pipelineId='Your PipelineId';
  3. 发起API请求并显示返回值。
    client.request('SubmitJobs', {
      Input: input,
      PipelineId: pipelineId,
      OutputBucket: ossBucket,
      Output: output
      }, {}).then(function (response) {
        console.log('PipelineName is: = ' + response.JobResultList[0].Job.JobId);
        console.log('PipelineId is:' + response.PipelineList[0].Id);
    }).catch(function (response) {
        console.log('ErrorCode = ' + response.Code);
        console.log('ErrorNo = ' + response.Message);
    });
完整代码
var RPCClient = require('@alicloud/pop-core').RPCClient;

function initMtsClient(accessKeyId, accessKeySecret, regionId) {
    var client = new RPCClient({
        accessKeyId: accessKeyId,
        accessKeySecret: accessKeySecret,
        endpoint: 'http://mts.' + regionId + '.aliyuncs.com',
        apiVersion: '2014-06-18'
    });

    return client;
}

var ossBucket = 'xxx';
var ossInputObject = 'yourInputFilename.extension';
var ossLocation = 'xxx';
var pipelineId = 'xxx';
var ossOutputObject = "yourOutputFilename.extension";
var accessKeyId = 'xxx';
var accessKeySecret = 'xxx';
var regionId = 'xxx';
var templateId = 'S00000001-200010';


var client = initMtsClient(accessKeyId, accessKeySecret, regionId);

var input = {
    'Location': ossLocation,
    'Bucket': ossBucket,
    'Object': encodeURIComponent(ossInputObject)
};
var video = {
    'Codec': "H.264",
    'Bitrate': "1500",
    'Width': "1280",
    'Fps': "25",
};

var audio = {
    'Codec': "AAC",
    'Bitrate': "128",
    'Channels': "2",
    'Samplerate': "44100"
};

var output = {OutputObject: encodeURIComponent(ossOutputObject)};

output.Video = video;

output.Audio = audio;

output.TemplateId = templateId;

var outputs = new Array();
outputs.push(output);
client.request('SubmitJobs', {
    Input: JSON.stringify(input),
    Outputs: JSON.stringify(outputs),
    OutputBucket: ossBucket,
    PipelineId: pipelineId,
    OutputLocation: ossLocation
}, {}).then(function (response) {
    console.log('response success: = ' + response.JobResultList.JobResult[0].Success);
    console.log('response jobID: ' + response.JobResultList.JobResult[0].Job.JobId);
}).catch(function (response) {
    console.log(JSON.stringify(response));
});