Video transcoding (PHP SDK V1)

Updated at:

Video transcoding converts a source video into a format supported by a target device or platform. During transcoding, you can adjust parameters such as resolution, frame rate, and bitrate — reducing file size for faster delivery and lower storage costs.

OSS delegates transcoding work to Intelligent Media Management (IMM) and runs jobs asynchronously. Submit a job, and OSS returns immediately; the transcoded output is saved to the specified object path in your bucket once the job completes.

Prerequisites

Before you begin, ensure that you have:

  • IMM attached to your OSS bucket. See Attach IMM.

  • The required OSS and IMM permissions. See Permissions.

  • OSS PHP SDK 2.7.0 or later.

Usage notes

  • The example below uses the public endpoint for the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For supported regions and endpoints, see Regions and endpoints.

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

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

  • For background on video transcoding concepts and supported formats, see Video transcoding.

Submit an asynchronous transcoding job

Call asyncProcessObject with a video/convert process string. The call returns immediately. OSS runs the transcoding job in the background and saves the output to the path you specify in the sys/saveas directive.

<?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), '+/', '-_'), '=');
}

Process string parameters

The $process string has two pipe-delimited directives: video/convert (transcoding settings) and sys/saveas (output destination).

`video/convert` parameters

ParameterDescriptionExample value
f_<format>Output container formatf_avi, f_mp4
vcodec_<codec>Video codecvcodec_h265, vcodec_h264
s_<width>x<height>Resolution in pixelss_1920x1080, s_1280x720
vb_<bps>Video bitrate in bits per secondvb_2000000 (2 Mbps)
fps_<rate>Frame ratefps_30, fps_24
acodec_<codec>Audio codecacodec_aac, acodec_mp3
ab_<bps>Audio bitrate in bits per secondab_100000 (100 Kbps)
sn_<flag>Caption stream: 1 disables captionssn_1

`sys/saveas` parameters

ParameterDescriptionExample value
o_<base64url>Base64url-encoded output object nameo_ZGVzdC5hdmk
b_<base64url>Base64url-encoded destination bucket nameb_ZXhhbXBsZWJ1Y2tldA

The base64url_encode helper produces URL-safe Base64 encoding (RFC 4648): it replaces + with - and / with _, and strips trailing = padding.

What's next