All Products
Search
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 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 the sample code for using MPS SDK for PHP to transcode media files.

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

Sample code

<?php
require_once './aliyun-php-sdk-core/Config.php'; 
use Mts\Request\V20140618 as Mts;

function initMtsClient($accessKeyId, $accessKeySecret) {
    $regionId = 'cn-shanghai';  // The ID of the region in which your MPS service is deployed.
    $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
    return new DefaultAcsClient($profile);
}

try {
    $client = initMtsClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'));
    $response = submitClipJob($client);
    print_r($response);
} catch (Exception $e) {
    print $e->getMessage()."\n";
}

function submitClipJob($client) {
    $pipelineId = 'd7cedd984be7dd63395c*****';   # The ID of the MPS queue. You can log on to the MPS console to view the ID.
    $templateId = "S00000001-100020"; # The ID of the transcoding template. You can log on to the MPS console to view the ID.
    $ossLocation = 'oss-cn-shanghai';
    $bucket = '<bucket name>';
    $ossInputObject = 'input.mp4';
    $ossOutputObject = 'output.mp4';
    $request = new Mts\SubmitJobsRequest();
    # The job input.
    $input = array('Location' => $ossLocation,
               'Bucket' => $bucket,
               'Object' => urlencode($ossInputObject));

    # The job output.
    $output = array('OutputObject' => urlencode($ossOutputObject));
    # The video-related parameters.
    $output['Video'] = array('Codec' =>'H.264',
                            'Bitrate' => 1500,
                            'Width' => 1280,
                            'Fps' => 25);
    # The audio-related parameters.
    $output['Audio'] = array('Codec' => 'AAC',
                            'Bitrate' => 128,
                            'Channels' => 2,
                            'Samplerate' => 44100);
    $output['TemplateId'] = $templateId;

    $outputs = array($output);

    $request->setInput(json_encode($input));
    $request->setOUtputs(json_encode($outputs));
    $request->setOutputBucket($bucket);
    $request->setOutputLocation($ossLocation);
    $request->setPipelineId($$pipelineId);  
    return $client->getAcsResponse($request);
}

References