This topic provides sample code for initiating and querying transcoding jobs in Intelligent Media Services using a server-side software development kit (SDK) to call OpenAPI.
Usage notes
To initiate a transcoding job, submit the job with the region, name, inputPath, outputPath, and templateId parameters. After the job is processed, you can use the returned JobId to query the job information.
Note the following about the transcoding job parameters:
The region parameter specifies the region where the service is available.
The name can be empty.
The inputPath and outputPath must be in the same region as the specified region.
You can obtain the templateId from the transcoding template management page.
The following regions are supported for the region parameter:
ApsaraVideo Media Processing for VOD: China (Shanghai), China (Beijing), and China (Shenzhen)
ApsaraVideo Media Processing (Real-time): China (Shanghai)
Sample code
To debug online, use the Alibaba Cloud OpenAPI Developer Portal.
import Console from '@alicloud/tea-console';
import OpenApi, * as $OpenApi from '@alicloud/openapi-client';
import Util from '@alicloud/tea-util';
import ICE20201109, * as $ICE20201109 from '@alicloud/ice20201109';
import * as $tea from '@alicloud/tea-typescript';
import Credential, { Config } from '@alicloud/credentials';
/**
* Your Node.js environment must be version 8.x or later.
* Install the Alibaba Cloud SDK Credentials tool. npm install @alicloud/credentials
* Install the IMS SDK. npm install --save @alicloud/ice20201109@1.3.11
*
*/
export default class Client {
/** Initialize the client using Credentials.
* @param regionId The service region.
* @return The client that sends requests.
*/
static async createClient(regionId: string): Promise<ICE20201109> {
const cred = new Credential();
let config = new $OpenApi.Config({ });
config.credential = cred;
config.endpoint = `ice.${regionId}.aliyuncs.com`;
return new ICE20201109(config);
}
/** Initialize the client using plaintext.
* To hard code the AccessKey ID and AccessKey secret, use the following code. We strongly recommend that you do not save the AccessKey ID and AccessKey secret in your project code. This can lead to an AccessKey pair leak and threaten the security of all resources in your account.
* @param accessKeyId The Alibaba Cloud AccessKey ID.
* @param accessKeySecret The Alibaba Cloud AccessKey secret.
* @param regionId The service region.
* @return The client that sends requests.
*/
// static async createClient(accessKeyId: string, accessKeySecret: string, regionId: string): Promise<ICE20201109> {
// let config = new $OpenApi.Config({ });
// config.accessKeyId = accessKeyId;
// config.accessKeySecret = accessKeySecret;
// config.regionId = regionId;
// config.endpoint = `ice.${regionId}.aliyuncs.com`;
// return new ICE20201109(config);
// }
// Read command line parameters.
static async main(args: string[]): Promise<void> {
let regionId = args[0];
let name = args[1];
let inputPath = args[2];
let outputPath = args[3];
let templateId = args[4];
// Initialize the client.
let client = await Client.createClient(regionId);
// Construct a request to submit the job.
let request = new $ICE20201109.SubmitTranscodeJobRequest({
name: name,
// Input information.
inputGroup: [
new $ICE20201109.SubmitTranscodeJobRequestInputGroup({
type: "OSS",
media: inputPath,
})
],
// Output information.
outputGroup: [
new $ICE20201109.SubmitTranscodeJobRequestOutputGroup({
output: new $ICE20201109.SubmitTranscodeJobRequestOutputGroupOutput({
type: "OSS",
media: outputPath,
}),
processConfig: new $ICE20201109.SubmitTranscodeJobRequestOutputGroupProcessConfig({
transcode: new $ICE20201109.SubmitTranscodeJobRequestOutputGroupProcessConfigTranscode({
templateId: templateId,
}),
}),
})
],
});
// Send the request to submit the job.
let response = await client.submitTranscodeJob(request);
// Print the result.
Console.log(Util.toJSONString($tea.toMap(response)));
// Extract the job ID and construct a request to query the job.
let jobId = response.body.transcodeParentJob.parentJobId;
let queryRequest = new $ICE20201109.GetTranscodeJobRequest({
jobId: jobId,
});
// Send the request to query the job status.
let queryResponse = await client.getTranscodeJob(queryRequest);
// Print the result.
Console.log(Util.toJSONString($tea.toMap(queryResponse)));
}
}
Client.main(process.argv.slice(2));