This topic describes how to use ApsaraVideo Media Processing (MPS) SDK for PHP to transcode media files. This topic also provides the complete sample code.

For more information about the API operation for transcoding and parameters of the operation, see SubmitJobs.

  1. Create an AcsClient instance.
    $clientProfile = DefaultProfile::getProfile(
    $mps_region_id,                   # The ID of the region in which you use the MPS service.
    $access_key_id,                   # Your AccessKey ID.
    $access_key_secret                # Your AccessKey secret.
    );
    $client = new DefaultAcsClient($clientProfile);
  2. Create a request and set the required parameters.
    $request = new Mts\SubmitJobsRequest();
    $request->setAcceptFormat('JSON');
  3. Set the transcoding parameters.
    • Input
      $input = array('Location' => $oss_location,
                      'Bucket' => $oss_bucket,
                      'Object' => urlencode($oss_input_object));
      $request->setInput(json_encode($input));
    • Output
      $output = array('OutputObject' => urlencode($oss_output_object));
      • Container
        $output['Container'] = array('Format' => 'mp4');
      • Video
        $output['Video'] = array('Codec' =>'H.264',
                                    'Bitrate' => 1500,
                                    'Width' => 1280,
                                    'Fps' => 25);
      • Audio
        $output['Audio'] = array('Codec' => 'AAC',
                                    'Bitrate' => 128,
                                    'Channels' => 2,
                                    'Samplerate' => 44100);
      • TemplateId
        $output['TemplateId'] = $template_id;
    • PipelineId
      $request->setPipelineId($pipeline_id);
  4. Send the request and handle the response or exception.
    $response = $client->getAcsResponse($request);
    print 'RequestId is:' . $response->{'RequestId'} . "\n";;
    if ($response->{'JobResultList'}->{'JobResult'}[0]->{'Success'}) {
    print 'JobId is:' .
          $response->{'JobResultList'}->{'JobResult'}[0]->{'Job'}->{'JobId'} . "\n";
    } else {
    print 'SubmitJobs Failed code:' .
          $response->{'JobResultList'}->{'JobResult'}[0]->{'Code'} .
          ' message:' .
          $response->{'JobResultList'}->{'JobResult'}[0]->{'Message'} . "\n";
    }
Sample code
<?php
include_once 'aliyun-openapi-php-sdk/aliyun-php-sdk-core/Config.php';
use Mts\Request\V20140618 as Mts;
$access_key_id = 'xxx';
$access_key_secret = 'xxx';
$mps_region_id = 'cn-hangzhou';
$pipeline_id = 'xxx';                 # The ID of the MPS queue.
$template_id = 'S00000001-200010';    # The ID of the transcoding template.
$oss_location = 'oss-cn-hangzhou';
$oss_bucket = 'xxx';
$oss_input_object = 'input.mp4';      # The path of the input file, which is an Object Storage Service (OSS) object.
$oss_output_object = 'output.mp4';    # The path of the input file, which is an OSS object.
# Create a DefaultAcsClient instance and initialize the instance.
$clientProfile = DefaultProfile::getProfile(
    $mps_region_id,                   # The ID of the region in which you use the MPS service.
    $access_key_id,                   # Your AccessKey ID.
    $access_key_secret                # Your AccessKey secret.
);
$client = new DefaultAcsClient($clientProfile);
# Create a request and set the required parameters.
$request = new Mts\SubmitJobsRequest();
$request->setAcceptFormat('JSON');
# Input
$input = array('Location' => $oss_location,
               'Bucket' => $oss_bucket,
               'Object' => urlencode($oss_input_object));
$request->setInput(json_encode($input));
# Output
$output = array('OutputObject' => urlencode($oss_output_object));
# Container
$output['Container'] = array('Format' => 'mp4');
# Video
$output['Video'] = array('Codec' =>'H.264',
                         'Bitrate' => 1500,
                         'Width' => 1280,
                         'Fps' => 25);
# Audio
$output['Audio'] = array('Codec' => 'AAC',
                         'Bitrate' => 128,
                         'Channels' => 2,
                         'Samplerate' => 44100);
# TemplateId
$output['TemplateId'] = $template_id;
$outputs = array($output);
$request->setOUtputs(json_encode($outputs));
$request->setOutputBucket($oss_bucket);
$request->setOutputLocation($oss_location);
# PipelineId
$request->setPipelineId($pipeline_id);
# Send the request and handle the response or exception.
try {
    $response = $client->getAcsResponse($request);
    print 'RequestId is:' . $response->{'RequestId'} . "\n";;
    if ($response->{'JobResultList'}->{'JobResult'}[0]->{'Success'}) {
        print 'JobId is:' .
               $response->{'JobResultList'}->{'JobResult'}[0]->{'Job'}->{'JobId'} . "\n";
    } else {
        print 'SubmitJobs Failed code:' .
               $response->{'JobResultList'}->{'JobResult'}[0]->{'Code'} .
               ' message:' .
               $response->{'JobResultList'}->{'JobResult'}[0]->{'Message'} . "\n";
    }
} catch(ServerException $e) {
    print 'Error: ' . $e->getErrorCode() . ' Message: ' . $e->getMessage() . "\n";
} catch(ClientException $e) {
    print 'Error: ' . $e->getErrorCode() . ' Message: ' . $e->getMessage() . "\n";
}