All Products
Search
Document Center

ApsaraVideo VOD:Media processing

Last Updated:Nov 28, 2022

This topic provides examples on how to use the API operations of the media processing module. The API operations are encapsulated in the server operation for PHP. You can call the API operations to submit transcoding and snapshot jobs, query snapshot data, and preprocess videos in a production studio.

Initialize a client

Before you can use the SDK, initialize a client. For more information, see Initialization.

Submit a transcoding job

You can call the SubmitTranscodeJobs operation to submit a transcoding job.

For more information about the request and response parameters of this operation, see SubmitTranscodeJobs. Sample code:

/**
 * Construct watermark parameters to be overridden. You can override only the URL of an image watermark or the content of a text watermark.
 * Configure the watermark parameters to be overridden. Make sure that the ID of the watermark is associated with the ID of the transcoding template that you use. The ID of the transcoding template is specified by TranscodeTemplateId.
 * You can call this operation to add only a watermark whose ID is associated with the ID of the transcoding template that you use.
 */
function buildOverrideParams() {
    $overrideParams = array();

    # Override watermarks.
    $watermarks = array();

    // Override an image watermark.
    $watermarks1 = array();
    $watermarks1["WatermarkId"] = "2ea587477c5a1bc8b52****";
    // Example of the URL of an image file: https://example-bucket-****.oss-cn-shanghai.aliyuncs.com/watermarks/02A1B22DF25D46C3C7****-6-2.png.
    $watermarks1["FileUrl"] = "<your File URL>";
    $watermarks[] = $watermarks1;

    // Override a text watermark.
    $watermarks2 = array();
    $watermarks2["WatermarkId"] = "d297ba31ac5242d2052****";
    $watermarks2["Content"] = "User ID: 6****";
    $watermarks[] = $watermarks2;

    $overrideParams["Watermarks"] = $watermarks;
    return json_encode($overrideParams);
}

/**
 * Optional configurations of HLS encryption. If you do not use HLS encryption, these configurations are not required.
 */
function buildEncryptConfig($client) {
    try {
        // Generate a random data key for encryption. The response contains the plaintext and ciphertext of the data key.
        // Only the ciphertext is used for standard video encryption.
        $request = new vod\GenerateKMSDataKeyRequest();
        $response = $client->getAcsResponse($request);

       $encryptConfig = array();
        # The URI of the operation that is used to decrypt the data key. To obtain the URI, concatenate the URL of the decryption service and the ciphertext of the data key. The ciphertext to decrypt varies among videos. Take note that you must deploy the decryption service. 
        # Example DecryptKeyUri: http://192.168.0.0/16/decrypt?Ciphertext=" + $response->getCiphertextBlob()
        $encryptConfig["DecryptKeyUri"] = "<your DecryptKeyUri>";
        // The type of the key service. Set the value to KMS.
        $encryptConfig["KeyServiceType"] = "KMS";
        # You can customize the name of the Ciphertext parameter. The name in this example is only for reference.
        $encryptConfig["CipherText"] = $response->getCiphertextBlob();

        return json_encode($encryptConfig);
    } catch (Exception $e) {
        print $e->getMessage()."\n";
        return null;
    }
}

/**
 * Submit a transcoding job.
 */
function submitTranscodeJobs($client) {
    $request = new vod\SubmitTranscodeJobsRequest();
    // Specify the ID of the video that you want to transcode.
    $request->setVideoId("6893fca9814640c8821efa523e52****");
    // Specify the ID of the transcoding template.
    $request->setTemplateGroupId("44f915b63a2375a6121533c6b252****");
    // Construct watermark parameters to be overridden. These parameters are required only if you want to override watermark-related information.
    $request->setOverrideParams(buildOverrideParams());
    // Configure the parameters for HLS encryption. These parameters are required only for HLS encryption.
    $request->setEncryptConfig(buildEncryptConfig($client));

    return $client->getAcsResponse($request);
}

/**
 * Sample code
 */
try {
    $client = initVodClient("<AccessKeyId>", "<AccessKeySecret>");

    $result = submitTranscodeJobs($client);
    var_dump($result);
} catch (Exception $e) {
    print $e->getMessage()."\n";
}

Submit a snapshot job

You can call the SubmitSnapshotJob operation to submit a snapshot job.

For more information about the request and response parameters of this operation, see SubmitSnapshotJob. Sample code:

Note

For more information about how to create a snapshot template, see Create a snapshot template.

/**
 * Construct the parameters for image sprite snapshots.
 * @return
 */
function buildSnapshotTemplateConfig() {
    $spriteSnapshotConfig = array();
    $spriteSnapshotConfig["CellWidth"] = "120";
    $spriteSnapshotConfig["CellHeight"] = "68";
    $spriteSnapshotConfig["Columns"] = "3";
    $spriteSnapshotConfig["Lines"] = "10";
    $spriteSnapshotConfig["Padding"] = "20";
    $spriteSnapshotConfig["Margin"] = "50";

    // Specify whether to retain the source image after an image sprite is generated.
    $spriteSnapshotConfig["KeepCellPic"] = "keep";
    $spriteSnapshotConfig["Color"] = "tomato";

    return json_encode($spriteSnapshotConfig);
}


/**
 * Submit a snapshot job.
 */
function submitSnapshotJob($client) {
    $request = new vod\SubmitSnapshotJobRequest();
    // The ID of the video from which you want to capture snapshots. We recommend that you specify SnapshotTemplateId.
    $request->setVideoId("6893fca9814640c8821efa523e52****");
    // The ID of the snapshot template.
    $request->setSnapshotTemplateId("44f915b63a2375a6121533c6b252****");

    // If you specify the SnapshotTemplateId parameter, the following parameters are ignored:
    $request->setCount(50);
    $request->setSpecifiedOffsetTime(0);
    $request->setInterval(1);
    $request->setWidth("200");
    $request->setHeight("200");
    $request->setSpriteSnapshotConfig(buildSnapshotTemplateConfig());

    return $client->getAcsResponse($request);
}

/**
 * Sample code
 */
try {
    $client = initVodClient("<AccessKeyId>", "<AccessKeySecret>");

    $result = submitSnapshotJob($client);
    var_dump($result);
} catch (Exception $e) {
    print $e->getMessage()."\n";
}

Query snapshot data

You can call the listSnapshots operation to query snapshot data.

For more information about the request and response parameters of this operation, see listSnapshots. Sample code:

/**
 * Query snapshot data.
 */
function listSnapshots($client) {
    $request = new vod\ListSnapshotsRequest();
    // The video ID.
    $request->setVideoId("6893fca9814640c8821efa523e52****");
    /// The snapshot type.
    $request->setSnapshotType("CoverSnapshot");
    // The page number.
    $request->setPageNo("1");
    $request->setPageSize("20");

    return $client->getAcsResponse($request);
}

/**
 * Sample code
 */
try {
    $client = initVodClient("<AccessKeyId>", "<AccessKeySecret>");

    $result = listSnapshots($client);
    var_dump($result);
} catch (Exception $e) {
    print $e->getMessage()."\n";
}

Preprocess videos in a production studio

You can call the SubmitPreprocessJobs operation to preprocess videos in a production studio.

For more information about the request and response parameters of this operation, see SubmitPreprocessJobs. Sample code:

/**
 * Preprocess videos in a production studio.
 */
function submitPreprocessJobs($client) {
    $request = new vod\SubmitPreprocessJobsRequest();
    // The video ID.
    $request->setVideoId("6893fca9814640c8821efa523e52****");
    /// The preprocessing type.
    $request->setPreprocessType("PreprocessType");

    return $client->getAcsResponse($request);
}

/**
 * Sample code
 */
try {
    $client = initVodClient("<AccessKeyId>", "<AccessKeySecret>");

    $result = submitPreprocessJobs($client);
    var_dump($result);
} catch (Exception $e) {
    print $e->getMessage()."\n";
}