edit-icon download-icon

Transcoding

Last Updated: Apr 19, 2018

Procedure

  1. Create AcsClient instance.

    1. $clientProfile = DefaultProfile::getProfile(
    2. $mps_region_id, # Your Region ID
    3. $access_key_id, # Your AccessKey ID
    4. $access_key_secret # Your AccessKey Secret
    5. );
    6. $client = new DefaultAcsClient($clientProfile);
  2. Create request, and set parameters.

    1. $request = new Mts\SubmitJobsRequest();
    2. $request->setAcceptFormat('JSON');
  3. Transcoding parameters.

    • Input

      1. $input = array('Location' => $oss_location,
      2. 'Bucket' => $oss_bucket,
      3. 'Object' => urlencode($oss_input_object));
      4. $request->setInput(json_encode($input));
    • Output

      1. $output = array('OutputObject' => urlencode($oss_output_object));
      • Container

        1. $output['Container'] = array('Format' => 'mp4');
      • Video

        1. $output['Video'] = array('Codec' =>'H.264',
        2. 'Bitrate' => 1500,
        3. 'Width' => 1280,
        4. 'Fps' => 25);
      • Audio

        1. $output['Audio'] = array('Codec' => 'AAC',
        2. 'Bitrate' => 128,
        3. 'Channels' => 2,
        4. 'Samplerate' => 44100);
      • TemplateId

        1. $output['TemplateId'] = $template_id;
    • PipelineId

      1. $request->setPipelineId($pipeline_id);
  4. Initiate API request and display returned value.

    1. $response = $client->getAcsResponse($request);
    2. print 'RequestId is:' . $response->{'RequestId'} . "\n";;
    3. if ($response->{'JobResultList'}->{'JobResult'}[0]->{'Success'}) {
    4. print 'JobId is:' .
    5. $response->{'JobResultList'}->{'JobResult'}[0]->{'Job'}->{'JobId'} . "\n";
    6. } else {
    7. print 'SubmitJobs Failed code:' .
    8. $response->{'JobResultList'}->{'JobResult'}[0]->{'Code'} .
    9. ' message:' .
    10. $response->{'JobResultList'}->{'JobResult'}[0]->{'Message'} . "\n";
    11. }

Full code

  1. <?php
  2. include_once 'aliyun-openapi-php-sdk/aliyun-php-sdk-core/Config.php';
  3. use Mts\Request\V20140618 as Mts;
  4. $access_key_id = 'xxx';
  5. $access_key_secret = 'xxx';
  6. $mps_region_id = 'cn-hangzhou';
  7. $pipeline_id = 'xxx';
  8. $template_id = 'S00000001-200010';
  9. $oss_location = 'oss-cn-hangzhou';
  10. $oss_bucket = 'xxx';
  11. $oss_input_object = 'input.mp4';
  12. $oss_output_object = 'output.mp4';
  13. # Create DefaultAcsClient instance and complete initialization
  14. $clientProfile = DefaultProfile::getProfile(
  15. $mps_region_id, # Your Region ID
  16. $access_key_id, # Your AccessKey ID
  17. $access_key_secret # Your AccessKey Secret
  18. );
  19. $client = new DefaultAcsClient($clientProfile);
  20. # Create API request and set parameters
  21. $request = new Mts\SubmitJobsRequest();
  22. $request->setAcceptFormat('JSON');
  23. # Input
  24. $input = array('Location' => $oss_location,
  25. 'Bucket' => $oss_bucket,
  26. 'Object' => urlencode($oss_input_object));
  27. $request->setInput(json_encode($input));
  28. # Output
  29. $output = array('OutputObject' => urlencode($oss_output_object));
  30. # Ouput->Container
  31. $output['Container'] = array('Format' => 'mp4');
  32. # Ouput->Video
  33. $output['Video'] = array('Codec' =>'H.264',
  34. 'Bitrate' => 1500,
  35. 'Width' => 1280,
  36. 'Fps' => 25);
  37. # Ouput->Audio
  38. $output['Audio'] = array('Codec' => 'AAC',
  39. 'Bitrate' => 128,
  40. 'Channels' => 2,
  41. 'Samplerate' => 44100);
  42. # Ouput->TemplateId
  43. $output['TemplateId'] = $template_id;
  44. $outputs = array($output);
  45. $request->setOUtputs(json_encode($outputs));
  46. $request->setOutputBucket($oss_bucket);
  47. $request->setOutputLocation($oss_location);
  48. # PipelineId
  49. $request->setPipelineId($pipeline_id);
  50. # Initiate request and handle returned value
  51. try {
  52. $response = $client->getAcsResponse($request);
  53. print 'RequestId is:' . $response->{'RequestId'} . "\n";;
  54. if ($response->{'JobResultList'}->{'JobResult'}[0]->{'Success'}) {
  55. print 'JobId is:' .
  56. $response->{'JobResultList'}->{'JobResult'}[0]->{'Job'}->{'JobId'} . "\n";
  57. } else {
  58. print 'SubmitJobs Failed code:' .
  59. $response->{'JobResultList'}->{'JobResult'}[0]->{'Code'} .
  60. ' message:' .
  61. $response->{'JobResultList'}->{'JobResult'}[0]->{'Message'} . "\n";
  62. }
  63. } catch(ServerException $e) {
  64. print 'Error: ' . $e->getErrorCode() . ' Message: ' . $e->getMessage() . "\n";
  65. } catch(ClientException $e) {
  66. print 'Error: ' . $e->getErrorCode() . ' Message: ' . $e->getMessage() . "\n";
  67. }
Thank you! We've received your feedback.