本文提供了Node.js SDK设置截图的操作步骤及完整的代码示例。
操作步骤
- 创建client实例。
var RPCClient = require('@alicloud/pop-core').RPCClient; var client = initVodClient('<Your AccessKeyId>', '<Your AccessKeySecret>', '<YourRegionId>');
- 设置截图参数。
- Input(作业输入)
var input={ Location: ossLocation, Bucket:ossBucket, Object:encodeURIComponent(ossInputObject) }
- SnapshotConfig(截图配置)
var snapshotConfig = {};
- OutputFile(输出文件)
var output = {}; output.Location=ossLocation; output.Bucket=ossBucket; output.Object=encodeURIComponent(ossOutputObject); snapshotConfig.OutputFile=JSON.stringify(output);
- Time(开始时间)
snapshotConfig.Time="2";
- Interval/Num(截图间隔)
snapshotConfig.Interval="2"; snapshotConfig.Num= "3";
- Width/Height(宽度、高度)
snapshotConfig.Height="360";
- Input(作业输入)
- 发起API请求并显示返回值。
var reponse=client.request('submitSnapshotJob',{ Input:Json.stringify(input), SnapshotConfig:Json.stringify(snapshotConfig)}); response = client.request() console.log("RequestId is:"+response.RequestId); console.log("JobId is:" + response.SnapshotJob.Id); console.log( "http://"+ossBucket+"."+ ossLocation+".aliyuncs.com/output_00001.jpg"); console.log( "http://"+ossBucket+"."+ ossLocation+".aliyuncs.com/output_00002.jpg"); console.log( "http://"+ossBucket+"."+ ossLocation+".aliyuncs.com/output_00003.jpg");
var accessKeyId = "xxx";
var accessKeySecret = "xxx";
var RegionId = "xxx";
var pipelineId = "xxx";
var ossLocation = "xxx";
var ossBucket = "xxx";
var ossInputObject = "input.mp4";//按需配置
var ossOutputObject = "output_{Count}.jpg";//按需配置
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 RPCClient = require('@alicloud/pop-core').RPCClient;
var client = initMtsClient(accessKeyId, accessKeySecret, RegionId);
var input = {
Location: ossLocation,
Bucket: ossBucket,
Object: encodeURIComponent(ossInputObject)
};
var snapshotConfig = {};
var output = {};
output.Location = ossLocation;
output.Bucket = ossBucket;
output.Object = encodeURIComponent(ossOutputObject);
snapshotConfig.OutputFile = JSON.stringify(output);
snapshotConfig.Time = "2";
snapshotConfig.Interval = "2";
snapshotConfig.Num = "3";
snapshotConfig.Height = "360";
client.request('submitSnapshotJob', {
Input: JSON.stringify(input),
SnapshotConfig: JSON.stringify(snapshotConfig),
PipelineId: pipelineId
}).then(function (response) {
console.log("RequestId is:" + response.RequestId);
console.log("JobId is:" + response.SnapshotJob.Id);
console.log("http://" + ossBucket + "." + ossLocation + ".aliyuncs.com/output_00001.jpg");
console.log("http://" + ossBucket + "." + ossLocation + ".aliyuncs.com/output_00002.jpg");
console.log("http://" + ossBucket + "." + ossLocation + ".aliyuncs.com/output_00003.jpg");
}).catch(function (response) {
console.log(JSON.stringify(response));
});