Video transcoding (PHP SDK V1)

Updated at:

Different devices, such as smartphones, tablets, and smart TVs, and various players support different video formats. You can use the video transcoding feature to convert a source video file into a format that plays correctly on a target device or application. This ensures that your video content can be distributed and shared across multiple platforms. During transcoding, you can adjust parameters such as resolution and frame rate. This reduces the video file size, which allows for faster video transmission and saves storage space.

Prerequisites

IMM must be attached. For more information, see Attach IMM.

Notes

  • Before you use video transcoding, make sure that you understand this feature. For more information, see Video transcoding.

  • Video transcoding is supported only in PHP SDK 2.7.0 and later versions.

  • This topic uses the public Endpoint of China (Hangzhou) as an example. If you want to access OSS from other Alibaba Cloud products in the same region, use an internal Endpoint. For more information about the regions and Endpoints that OSS supports, see Regions and Endpoints.

  • The example in this topic reads access credentials from environment variables. To configure access credentials, see Configure access credentials (PHP SDK V1).

  • The example in this topic creates an OSSClient instance using an OSS domain name. To create an OSSClient instance using a custom domain name or Security Token Service (STS), see Initialization (PHP SDK V1).

  • To execute a video transcoding task, you must have the required OSS and IMM permissions. For more information, see Permissions.

Sample code

The following code provides an example of how to perform asynchronous video transcoding.

<?php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\Core\OssException;

try {
    // Obtain access credentials from environment variables and store them in the provider. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
    $provider = new EnvironmentVariableCredentialsProvider();
    // Specify the Endpoint for the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name.
    $bucket = "examplebucket";
    $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "bucket" => $bucket
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
    );
    $ossClient = new OssClient($config);
    // Specify the name of the source video file.
    $source_object = 'src.mp4';
    // Specify the name of the transcoded video file.
    $target_object = 'dest.avi';
    // Transcode the source video file. The container of the output media file is AVI. The video stream is encoded in H.265, the resolution is 1920x1080, the frame rate is 30 fps, and the video bitrate is 2 Mbps. The audio is encoded in AAC with a bitrate of 100 Kbps. The caption stream is disabled.
    $process = 'video/convert,f_avi,vcodec_h265,s_1920x1080,vb_2000000,fps_30,acodec_aac,ab_100000,sn_1' .
        '|sys/saveas' .
        ',o_' . base64url_encode($target_object) .
        ',b_' . base64url_encode($bucket);
    $result = $ossClient->asyncProcessObject($bucket, $source_object, $process);
    var_dump($result);
} catch (OssException $e) {
    printf($e->getMessage() . "\n");
    return;
}

function base64url_encode($data)
{
    return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}