本文提供了Node.js SDK拼接开板和尾板的操作步骤及完整的代码示例。

操作步骤

  1. 创建Client实例。
    var RPCClient = require('@alicloud/pop-core').RPCClient;
    
    var client = initVodClient('<Your AccessKeyId>', '<Your AccessKeySecret>', '<YourRegionId>');
  2. 设置转码参数。
    • Input
      var input = {};
      input.Location= ossLocation;
      input.Bucket= ossBucket;
      input.Object=encodeURIComponent(headObject);
    • Output
      var outputOSSObject=encodeURIComponent(ossOutputObject);;
      var output = {};
      output.OutputObject= outputOSSObject;
      output.TemplateId= templateId;
    • Video
      var video = {};
      video.Width= "1280";
      video.Height= "720");
      output.Video=JSON.stringify(video);
    • OpeningList
      var openingVideo = {};
      var httpStri="http://";
      var dot='.';
      var openingVideoURL=httpStri.concat(ossBucket, dot,ossLocation, dot, 'aliyuncs.com/',
                                          encodeURIComponent(headObject));
      openingVideo.OpenUrl= openingVideoURL;
      openingVideo.Width="640";
      openingVideo.Start= "2";
      var openingVideoList = new Array();
      openingVideoList.push(openingVideo);
      output.OpeningList= JSON.stringify(openingVideoList);
    • TailSlateList
      var tailSlateVideo = {};
      var httpStri="http://";
      var dot='.';
      var tailSlateVideoURL=httpStri.concat(ossBucket, dot, ossLocation, dot, 'aliyuncs.com/',
                                          encodeURIComponent(tailObject));
      tailSlateVideo.TailUrl= tailSlateVideoURL;
      tailSlateVideo.Width= "640";
      tailSlateVideo.BlendDuration= "3";
      tailSlateVideo.BgColor= "Black";
      var tailSlateVideoList = new Array();
      tailSlateVideoList.push(tailSlateVideo);
      output.TailSlateList= JSON.stringify(tailSlateVideoList);
  3. 发起API请求并显示返回值。
    var client=client.request('SubmitJobs', {
        Input: JSON.stringify(input),
        Outputs: JSON.stringtify(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));
    });
完整代码
var accessKeyId = "xxx";
var accessKeySecret = "xxx";
var mpsRegionId = "xxx";
var pipelineId = "xxx";
var templateId = "S00000001-200030";
var ossLocation = "xxx";
var ossBucket = "xxx";
var ossInputObject = "xxx.mp4";
var ossOutputObject = "xxx.mp4";
var headObject = "head.mp4";
var tailObject = "tail.mp4";
var httpStri = "http://";
var dot = '.';

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 client = initMtsClient(accessKeyId, accessKeySecret, mpsRegionId);
var input = {};
input.Location = ossLocation;
input.Bucket = ossBucket;
input.Object = encodeURIComponent(ossInputObject);

var outputOSSObject = encodeURIComponent(ossOutputObject);
var output = {};
output.OutputObject = outputOSSObject;
output.TemplateId = templateId;
// Output->OpeningList

var openingVideo = {};
var openingVideoURL = httpStri.concat(ossBucket, dot, ossLocation, dot, 'aliyuncs.com/',
    encodeURIComponent(headObject));
openingVideo.OpenUrl = openingVideoURL;
openingVideo.Width = "640";
openingVideo.Start = "2";
var openingVideoList = new Array();
openingVideoList.push(openingVideo);
output.OpeningList = JSON.stringify(openingVideoList);

// Output->TailSlateList
var tailSlateVideo = {};
var tailSlateVideoURL = httpStri.concat(ossBucket, dot, ossLocation, dot, 'aliyuncs.com/',
    encodeURIComponent(tailObject));
tailSlateVideo.TailUrl = tailSlateVideoURL;
tailSlateVideo.Width = "640";
tailSlateVideo.BlendDuration = "3";
tailSlateVideo.BgColor = "Black";
var tailSlateVideoList = new Array();
tailSlateVideoList.push(tailSlateVideo);
output.TailSlateList = JSON.stringify(tailSlateVideoList);

// Outputs
var outputs = new Array();
outputs.push(output);

var client = 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);
    console.log(JSON.stringify(response));
}).catch(function (response) {
    console.log(JSON.stringify(response));
});