This topic describes how to use the PHP SDK to moderate videos for risky content. You can moderate both images and audio in videos.

  • The PHP SDK supports both synchronous and asynchronous video moderation. If you use synchronous video moderation, you can only submit a sequence of image frames captured from a video for moderation. If you use asynchronous video moderation, you can submit either a video or a sequence of image frames captured from the video for moderation. We recommend that you use asynchronous video moderation.
  • You can submit the URL of an online video, the URL of a local video, a binary video stream, or a video live stream for video moderation.

Preparations

Before calling operations, make the following preparations:
  1. Create an AccessKey ID and AccessKey secret of your Alibaba Cloud account. For more information, see Create an AccessKey.
  2. Install PHP dependencies. For more information, see Installation.

(Recommended) Submit asynchronous video moderation tasks

Operation description
Operation Feature Region Description
VideoAsyncScanRequest Sends asynchronous requests to moderate videos for risky content.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore
  • us-west-1: US (Silicon Valley)
This operation moderates videos for risky content in multiple moderation scenarios, including pornography, terrorist content, ad, undesirable scene, and logo detection.
Sample code
  • Submit the URL of an online video for video moderation
    <? php
    
    require_once 'aliyuncs/aliyun-oss-php-sdk/autoload.php';
    include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
    
    use Green\Request\V20180509 as Green;
    
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
    DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    $client = new DefaultAcsClient($iClientProfile);
    $request = new Green\VideoAsyncScanRequest();
    $request->setMethod("POST");
    $request->setAcceptFormat("JSON");
    
    // The system charges you based on the moderation scenario that you specify.
    // You can send a request to moderate multiple videos at a time and specify multiple moderation scenarios for each video. The system charges you based on the number of frames captured from each video and the number of moderation scenarios for each video.
    // For example, if you moderate two videos for both pornography and terrorist content at a time, the total expense is calculated based on the following formula: Total expense = Number of frames captured from the two videos × (Unit price in the pornography detection scenario + Unit price in the terrorist content detection scenario).
    $task1 = array('dataId' =>  uniqid(),
        'url' => 'http://xxxx.mp4'
    );
    $request->setContent(json_encode(array("tasks" => array($task1),
        "scenes" => array("porn", "terrorism"))));
    try {
        $response = $client->getAcsResponse($request);
        print_r($response);
        if(200 == $response->code){
            $taskResults = $response->data;
            foreach ($taskResults as $taskResult) {
                if(200 == $taskResult->code){
                    $taskId = $taskResult->taskId;
                    // Save the task ID, which is used to poll the moderation results at the specified interval. For more information, see the description of the VideoAsyncScanResultsRequest operation.
                    print_r($taskId);
                }else{
                    print_r("task process fail:" + $response->code);
                }
            }
        }else{
            print_r("detect not success. code:" + $response->code);
        }
    } catch (Exception $e) {
        print_r($e);
    }
  • Submit the URL of a local video for video moderation
    <? php
    
    require_once 'aliyuncs/aliyun-oss-php-sdk/autoload.php';
    include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
    
    use Green\Request\V20180509 as Green;
    use Green\Request\Extension\ClientUploader;
    
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
    DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    $client = new DefaultAcsClient($iClientProfile);
    $request = new Green\VideoAsyncScanRequest();
    $request->setMethod("POST");
    $request->setAcceptFormat("JSON");
    
    // The system charges you based on the moderation scenario that you specify.
    // You can send a request to moderate multiple videos at a time and specify multiple moderation scenarios for each video. The system charges you based on the number of frames captured from each video and the number of moderation scenarios for each video.
    // For example, if you moderate two videos for both pornography and terrorist content at a time, the total expense is calculated based on the following formula: Total expense = Number of frames captured from the two videos × (Unit price in the pornography detection scenario + Unit price in the terrorist content detection scenario). // Submit your local video before moderation.
    $uploader = ClientUploader::getVideoClientUploader($client);
    $url = $uploader->uploadFile("d:/Terrorist content 1.mp4");
    $task1 = array('dataId' =>  uniqid(),
        'url' => $url
    );
    $request->setContent(json_encode(array("tasks" => array($task1),
        "scenes" => array("porn", "terrorism"))));
    try {
        $response = $client->getAcsResponse($request);
        print_r($response);
        if(200 == $response->code){
            $taskResults = $response->data;
            foreach ($taskResults as $taskResult) {
                if(200 == $taskResult->code){
                    $taskId = $taskResult->taskId;
                    // Save the task ID, which is used to poll the moderation results at the specified interval. For more information, see the description of the VideoAsyncScanResultsRequest operation.
                    print_r($taskId);
                }else{
                    print_r("task process fail:" + $response->code);
                }
            }
        }else{
            print_r("detect not success. code:" + $response->code);
        }
    } catch (Exception $e) {
        print_r($e);
    }
  • Submit a binary video stream for video moderation
    <? php
    
    require_once 'aliyuncs/aliyun-oss-php-sdk/autoload.php';
    include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
    
    use Green\Request\V20180509 as Green;
    use Green\Request\Extension\ClientUploader;
    
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
    DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    $client = new DefaultAcsClient($iClientProfile);
    $request = new Green\VideoAsyncScanRequest();
    $request->setMethod("POST");
    $request->setAcceptFormat("JSON");
    
    // The system charges you based on the moderation scenario that you specify.
    // You can send a request to moderate multiple videos at a time and specify multiple moderation scenarios for each video. The system charges you based on the number of frames captured from each video and the number of moderation scenarios for each video.
    // For example, if you moderate two videos for both pornography and terrorist content at a time, the total expense is calculated based on the following formula: Total expense = Number of frames captured from the two videos × (Unit price in the pornography detection scenario + Unit price in the terrorist content detection scenario).
    $uploader = ClientUploader::getVideoClientUploader($client);
    $bytes = file_get_contents('d:/test.mp4');
    $url = $uploader->uploadBytes($bytes);
    $task1 = array('dataId' =>  uniqid(),
        'url' => $url
    );
    $request->setContent(json_encode(array("tasks" => array($task1),
        "scenes" => array("porn", "terrorism"))));
    try {
        $response = $client->getAcsResponse($request);
        print_r($response);
        if(200 == $response->code){
            $taskResults = $response->data;
            foreach ($taskResults as $taskResult) {
                if(200 == $taskResult->code){
                    $taskId = $taskResult->taskId;
                    // Save the task ID, which is used to poll the moderation results at the specified interval. For more information, see the description of the VideoAsyncScanResultsRequest operation.
                    print_r($taskId);
                }else{
                    print_r("task process fail:" + $response->code);
                }
            }
        }else{
            print_r("detect not success. code:" + $response->code);
        }
    } catch (Exception $e) {
        print_r($e);
    }
  • Submit a video live stream for video moderation
    <? php
    
    require_once 'aliyuncs/aliyun-oss-php-sdk/autoload.php';
    include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
    
    use Green\Request\V20180509 as Green;
    
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
    DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    $client = new DefaultAcsClient($iClientProfile);
    $request = new Green\VideoAsyncScanRequest();
    $request->setMethod("POST");
    $request->setAcceptFormat("JSON");
    
    // The system charges you based on the moderation scenario that you specify.
    // You can send a request to moderate multiple video live streams at a time and specify multiple moderation scenarios for each video live stream. The system charges you based on the number of frames captured from each video live stream and the number of moderation scenarios for each video live stream.
    // For example, if you moderate two videos for both pornography and terrorist content at a time, the total expense is calculated based on the following formula: Total expense = Number of frames captured from the two videos × (Unit price in the pornography detection scenario + Unit price in the terrorist content detection scenario).
    $task1 = array('dataId' =>  uniqid(),
        'url' => 'http://URL of your video live stream.flv'
    );
    $request->setContent(json_encode(array("tasks" => array($task1),
        "scenes" => array("porn", "terrorism"), "live" => true)));
    try {
        $response = $client->getAcsResponse($request);
        print_r($response);
        if(200 == $response->code){
            $taskResults = $response->data;
            foreach ($taskResults as $taskResult) {
                if(200 == $taskResult->code){
                    $taskId = $taskResult->taskId;
                    // Save the task ID, which is used to poll the moderation results at the specified interval. For more information, see the description of the VideoAsyncScanResultsRequest operation.
                    print_r($taskId);
                }else{
                    print_r("task process fail:" + $response->code);
                }
            }
        }else{
            print_r("detect not success. code:" + $response->code);
        }
    } catch (Exception $e) {
        print_r($e);
    }
  • Submit a video live stream to moderate both the video images and audio
    <? php
    
    require_once 'aliyuncs/aliyun-oss-php-sdk/autoload.php';
    include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
    
    use Green\Request\V20180509 as Green;
    
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
    DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    $client = new DefaultAcsClient($iClientProfile);
    $request = new Green\VideoAsyncScanRequest();
    $request->setMethod("POST");
    $request->setAcceptFormat("JSON");
    
    # Create a video moderation task. By default, you can only submit one task in a request. If you want to submit more than one task in a request, submit a ticket.
    # The system charges you based on the moderation scenario that you specify.
    # You can send a request to moderate multiple videos at a time and specify multiple moderation scenarios for each video. The system charges you based on the number of frames captured from each video and the number of moderation scenarios for each video.
    # For example, if you moderate two videos for both pornography and terrorist content at a time, the total expense is calculated based on the following formula: Total expense = Number of frames captured from the two videos × (Unit price in the pornography detection scenario + Unit price in the terrorist content detection scenario).
    # If you moderate both images and audio in a video, the expense for image moderation is calculated based on the preceding rule and that for audio moderation is calculated based on the audio duration.
    $task1 = array('dataId' =>  uniqid(),
        'url' => 'http://URL of your video live stream.mp4'
    );
    $request->setContent(json_encode(array("tasks" => array($task1),
        "scenes" => array("porn", "terrorism"), "audioScenes" => 'antispam')));
    try {
        $response = $client->getAcsResponse($request);
        print_r($response);
        if(200 == $response->code){
            $taskResults = $response->data;
            foreach ($taskResults as $taskResult) {
                if(200 == $taskResult->code){
                    $taskId = $taskResult->taskId;
                    // Save the task ID, which is used to poll the moderation results at the specified interval. For more information, see the description of the VideoAsyncScanResultsRequest operation.
                    print_r($taskId);
                }else{
                    print_r("task process fail:" + $response->code);
                }
            }
        }else{
            print_r("detect not success. code:" + $response->code);
        }
    } catch (Exception $e) {
        print_r($e);
    }

Query the results of asynchronous video moderation

Operation description
Operation Feature Region Description
VideoAsyncScanResultsRequest Queries the results of asynchronous video moderation tasks.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore
  • us-west-1: US (Silicon Valley)
This operation queries the video moderation results through polling. We recommend that you configure a callback notification to receive the moderation results.
Sample code
<? php
include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
use Green\Request\V20180509 as Green;

// Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account.
$iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new DefaultAcsClient($iClientProfile);
$request = new Green\VideoAsyncScanResultsRequest();
$request->setMethod("POST");
$request->setAcceptFormat("JSON");

// Specify the task ID returned after you submit the asynchronous moderation task.
$request->setContent(json_encode(array("testtaskid")));
try {
    $response = $client->getAcsResponse($request);
    print_r($response);
    if(200 == $response->code){
        $taskResults = $response->data;
        foreach ($taskResults as $taskResult) {
            if(200 == $taskResult->code){
                $sceneResults = $taskResult->results;
                foreach ($sceneResults as $sceneResult) {
                    $scene = $sceneResult->scene;
                    $suggestion = $sceneResult->suggestion;
                    // Take a further action on the video based on the values of the scene and suggestion parameters.
                    // do something
                }
            }else{
                print_r("task process fail:" + $response->code);
            }
        }
    }else{
        print_r("detect not success. code:" + $response->code);
    }
} catch (Exception $e) {
    print_r($e);
}

Submit synchronous video moderation tasks

Operation description
Operation Feature Region Description
VideoSyncScanRequest Sends synchronous requests to moderate videos for risky content.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore
  • us-west-1: US (Silicon Valley)
If you use synchronous video moderation, you can only submit a sequence of image frames captured from a video for moderation. We recommend that you use asynchronous video moderation.
Sample code
<? php

require_once 'aliyuncs/aliyun-oss-php-sdk/autoload.php';
include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';

use Green\Request\V20180509 as Green;

$iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new DefaultAcsClient($iClientProfile);
$request = new Green\VideoSyncScanRequest();
$request->setMethod("POST");
$request->setAcceptFormat("JSON");

// The system charges you based on the moderation scenario that you specify.
// You can send a request to moderate multiple videos at a time and specify multiple moderation scenarios for each video. The system charges you based on the number of frames captured from each video and the number of moderation scenarios for each video.
// For example, if you moderate two videos for both pornography and terrorist content at a time, the total expense is calculated based on the following formula: Total expense = Number of frames captured from the two videos × (Unit price in the pornography detection scenario + Unit price in the terrorist content detection scenario).
$task1 = array('frames' => array(['offset' => '0', 'url'=>'https://test1.jpg'] ,
    ['offset' => '1', 'url'=>'https://test2.jpg'] ,
    ['offset' => '2', 'url'=>'https://test3.jpg'] ));

$request->setContent(json_encode(array("tasks" => array($task1),
    "scenes" => array("porn", "terrorism"))));
try {
    $response = $client->getAcsResponse($request);
    print_r($response);
    if(200 == $response->code){
        $taskResults = $response->data;
        foreach ($taskResults as $taskResult) {
            if(200 == $taskResult->code){
                $sceneResults = $taskResult->results;
                foreach ($sceneResults as $sceneResult) {
                    $scene = $sceneResult->scene;
                    $suggestion = $sceneResult->suggestion;
                    // Take a further action on the video based on the values of the scene and suggestion parameters.
                    // do something
                }
            }else{
                print_r("task process fail:" + $response->code);
            }
        }
} catch (Exception $e) {
    print_r($e);
}