All Products
Search
Document Center

ApsaraVideo Media Processing:Transcode media files

Last Updated:Nov 15, 2024

Transcoding is to convert an audio or video file into another one or more audio or video files to adapt to different network bandwidths, terminal devices, and user needs. If the transcoding jobs and workflows created in the ApsaraVideo Media Processing (MPS) console cannot meet your business requirements, you can call the SubmitJobs operation to submit transcoding jobs. This topic provides an example on how to use MPS SDK for Node.js V2.0 to transcode media files.

Sample code

For more information about the API operations that are used to manage transcoding jobs and the parameters of the API operations, see SubmitJobs.

'use strict';


const Mts20140618 = require('@alicloud/mts20140618');
const OpenApi = require('@alicloud/openapi-client');
const Util = require('@alicloud/tea-util');
const Tea = require('@alicloud/tea-typescript');

class Client {

    /**
     * Use your AccessKey ID and AccessKey secret to initialize a client.
     * @return Client
     * @throws Exception
     */
    static createClient() {

        let config = new OpenApi.Config({
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured. 
                accessKeyId: process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'],
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured. 
                accessKeySecret: process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
    });
        // Specify an endpoint. For more information, visit https://api.aliyun.com/product/Mts.
        config.endpoint = `mts.cn-qingdao.aliyuncs.com`;
        return new Mts20140618.default(config);
    }

    static async main(args) {
        let client = Client.createClient();
        let submitJobsRequest = new Mts20140618.SubmitJobsRequest({
                // The job input.
                input: '{"Bucket":"exampleBucket","Location":"oss-cn-hangzhou","Object":"example.flv","Referer": "The parameter that you set in the Object Storage Service (OSS) console to enable the hotlink protection feature"}',
                // The job output configuration.
                outputs: '[{"OutputObject":"exampleOutput.mp4","TemplateId":"6181666213ab41b9bc21da8ff5ff****","WaterMarks":[{"InputFile":{"Bucket":"exampleBucket","Location":"oss-cn-hangzhou","Object":"image_01.png"},"WaterMarkTemplateId":"9b772ce2740d4d55876d8b542d47****"}],"UserData":"testid-001"}]',
                // The OSS bucket that stores the input file.
                outputBucket: 'exampleBucket',
                // The region in which the OSS bucket resides.
                outputLocation: 'oss-cn-hangzhou',
                // The ID of the MPS queue.
                pipelineId: 'dd3dae411e704030b921e52698e5****',
    });
        let runtime = new Util.RuntimeOptions({ });
        try {
            // Write your own code to display the response of the API operation if necessary.
            await client.submitJobsWithOptions(submitJobsRequest, runtime);
        } catch (error) {
            // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for reference only. 
            // The error message.
            console.log(error.message);
            // The URL of the corresponding error diagnostics page.
            console.log(error.data["Recommend"]);
            Util.default.assertAsString(error.message);
        }
    }

}

exports.Client = Client;
Client.main(process.argv.slice(2));

References