All Products
Document Center

ApsaraVideo Media Processing:Transcode media files

Last Updated:Mar 14, 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 requirements. 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 the sample code for using MPS SDK for Node.js 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.

import Console from '@alicloud/tea-console';
import OpenApi, * as $OpenApi from '@alicloud/openapi-client';
import Env from '@alicloud/darabonba-env';
import Util from '@alicloud/tea-util';
import mts20140618, * as $mts20140618 from '@alicloud/mts20140618';
import * as $tea from '@alicloud/tea-typescript';

 * Install Node.js 8.x or later. 
 * Install Alibaba Cloud SDK for Node.js. npm install @alicloud/pop-core --save
 * Install the Alibaba Cloud SDK Credentials package.   npm install @alicloud/credentials
 * Install MPS SDK for Node.js. npm install --save @alicloud/mts20140618
/** The ID of the MPS queue. You can log on to the MPS console to view the ID. */
var pipelineId = "d7cedd984be7dd63395c*****"; 
/** The ID of the transcoding template. The output file is in the M3U8 format. Select a template based on your business requirements. */
var templateId = "S00000001-100020"; 
var ossLocation = "oss-cn-shanghai";
var bucket = "<bucket name>";
var ossInputObject = "input.mp4";
var ossOutputObject = "output.mp4";

export default class Client {

/** Initialize the SDK client. */
static async createClient(accessKeyId: string, accessKeySecret: string, regionId: string): Promise<mts20140618> {
    let config = new $OpenApi.Config({ });
    config.accessKeyId = accessKeyId;
    config.accessKeySecret = accessKeySecret;
    /** The ID of the region in which your MPS service is deployed. */
    config.regionId = "cn-shanghai";
    return new mts20140618(config);

static async main(args: string[]): Promise<void> {
    let client = await Client.createClient(Env.getEnv("ALIBABA_CLOUD_ACCESS_KEY_ID"), Env.getEnv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), args[0]);

    let request = new $mts20140618.SubmitJobsRequest({
        input: inputParam(),
        outputs: outputParam(),
        outputBucket: bucket,
        pipelineId: pipelineId,
        outputLocation: ossLocation
    let response = await client.submitJobs(request);


function inputParam() {
    var input:any = {};
    input.Location = ossLocation;
    input.Bucket = bucket;
    input.Object = encodeURIComponent(ossInputObject);
    return JSON.stringify(input);

function outputParam() {
    var outputOSSObject = encodeURIComponent(ossOutputObject);
    var output:any = {};
    output.OutputObject = outputOSSObject;
    output.TemplateId = templateId;
    /** The video-related parameters. */
    var video = {
        'Codec': "H.264",
        'Bitrate': "1500",
        'Width': "1280",
        'Fps': "25",
    /** The audio-related parameters. */
    var audio = {
        'Codec': "AAC",
        'Bitrate': "128",
        'Channels': "2",
        'Samplerate': "44100"
    output.Video = JSON.stringify(video);
    output.Audio = JSON.stringify(audio);

    var outputs = new Array();

    return JSON.stringify(outputs);