使用PHP SDK视频审核接口检测视频中是否包含风险内容。

  • 支持同步检测和异步检测两种方式:同步检测只支持传递视频的截帧图片序列;(推荐)异步检测支持对原始视频或视频的截帧图片序列进行检测。
  • 视频支持传入互联网视频URL、本地视频文件路径以及二进制视频文件流。

准备工作

在进行具体的服务调用之前,请参考以下步骤,完整准备工作:
  1. 创建阿里云AccessKeyId和AccessKeySecret。具体请参考创建AccessKey
  2. 安装PHP依赖。具体请参考安装PHP依赖

(推荐)提交视频异步检测任务

接口描述
接口 功能 支持的Region 描述
VideoAsyncScanRequest 异步视频风险检测。 cn-shanghaicn-beijing 对视频进行多个风险场景的识别,包括色情、暴恐涉政、广告、二维码、不良场景、Logo(商标台标)识别。
示例代码
  • 提交视频url检测任务
    <?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", "您自己的AccessKeyId", "您自己的AccessKeySecret");
    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");
    
    // 计费按照该处传递的场景进行
    // 一次请求中可以同时检测多个视频,每个视频可以同时检测多个风险场景,计费按照场景和视频截帧数计算
    // 例如:检测2个视频,场景传递porn,terrorism,计费会按照2个视频的截帧总数乘以鉴黄场景的价格加上2个视频的截帧总数乘以暴恐检测场景的费用计算//本地文件先进行上传,然后进行检测
    $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;
                    // 将taskId保存下来,间隔一段时间来轮询结果。具体请参照VideoAsyncScanResultsRequest接口说明
                    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);
    }
  • 提交本地视频文件检测任务
    <?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", "您自己的AccessKeyId", "您自己的AccessKeySecret");
    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");
    
    // 计费按照该处传递的场景进行
    // 一次请求中可以同时检测多个视频,每个视频可以同时检测多个风险场景,计费按照场景和视频截帧数计算
    // 例如:检测2个视频,场景传递porn,terrorism,计费会按照2个视频的截帧总数乘以鉴黄场景的价格加上2个视频的截帧总数乘以暴恐检测场景的费用计算//本地文件先进行上传,然后进行检测
    $uploader = ClientUploader::getVideoClientUploader($client);
    $url = $uploader->uploadFile("d:/暴恐涉政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;
                    // 将taskId保存下来,间隔一段时间来轮询结果。具体请参照VideoAsyncScanResultsRequest接口说明
                    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);
    }
  • 提交视频文件二进制检测任务
    <?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", "您自己的AccessKeyId", "您自己的AccessKeySecret");
    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");
    
    // 计费按照该处传递的场景进行
    // 一次请求中可以同时检测多个视频,每个视频可以同时检测多个风险场景,计费按照场景和视频截帧数计算
    // 例如:检测2个视频,场景传递porn,terrorism,计费会按照2个视频的截帧总数乘以鉴黄场景的价格加上2个视频的截帧总数乘以暴恐检测场景的费用计算//本地文件先进行上传,然后进行检测
    $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;
                    // 将taskId保存下来,间隔一段时间来轮询结果。具体请参照VideoAsyncScanResultsRequest接口说明
                    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);
    }
  • 提交视频直播流检测任务
    <?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", "您自己的AccessKeyId", "您自己的AccessKeySecret");
    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");
    
    // 计费按照该处传递的场景进行
    // 一次请求中可以同时检测多个视频流,每个视频流可以同时检测多个风险场景,计费按照场景和视频截帧数计算
    // 例如:检测2个视频,场景传递porn,terrorism,计费会按照2个视频的截帧总数乘以鉴黄场景的价格加上2个视频的截帧总数乘以暴恐检测场景的费用计算//本地文件先进行上传,然后进行检测
    $task1 = array('dataId' =>  uniqid(),
        'url' => 'http://您的直播流地址.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;
                    // 将taskId保存下来,间隔一段时间来轮询结果。具体请参照VideoAsyncScanResultsRequest接口说明
                    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);
    }
  • 提交视频语音综合检测任务
    <?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", "您自己的AccessKeyId", "您自己的AccessKeySecret");
    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");
    
    # 设置待检测视频,默认一次请求只支持1个task,如果需要开放到更多,请通过工单联系我们。
    # 计费按照该处传递的场景进行
    # 一次请求中可以同时检测多个视频,每个视频可以同时检测多个风险场景,计费按照场景和视频截帧数计算
    # 例如:检测2个视频,场景传递porn,terrorism,计费会按照2个视频的截帧总数乘以鉴黄场景的价格加上2个视频的截帧总数乘以暴恐检测场景的费用计算
    # 如果同时检测视频画面和视频中的语音,视频中的画面还是按照上述示例计费,语音部分按照视频语音的总时长进行计费
    $task1 = array('dataId' =>  uniqid(),
        'url' => 'http://视频地址.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;
                    // 将taskId保存下来,间隔一段时间来轮询结果。具体请参照VideoAsyncScanResultsRequest接口说明
                    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);
    }

查询视频异步检测结果

接口描述
接口 功能 支持的Region 描述
VideoAsyncScanResultsRequest 查询异步视频风险检测结果。 cn-shanghaicn-beijing 查询视频检测的结果,该方法需要轮询结果,建议使用callback的方式获取结果。
示例代码
<?php
include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
use Green\Request\V20180509 as Green;

// 请替换成您自己的accessKeyId、accessKeySecret
$iClientProfile = DefaultProfile::getProfile("cn-shanghai", "您的accessKeyId", "您的accessKeySecret");
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");

//提交异步检测任务返回的taskId
$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;
                    //根据scene和suggetion做相关处理
                    //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);
}

视频截帧同步检测

接口描述
接口 功能 支持的Region 描述
VideoSyncScanRequest 同步检测视频中的风险内容。 cn-shanghaicn-beijing 同步检测只支持传递视频帧序列,不支持检测视频文件,推荐使用异步检测接口。
示例代码(视频帧序列同步检测)
<?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", "您自己的AccessKeyId", "您自己的AccessKeySecret");
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");

// 计费按照该处传递的场景进行
// 一次请求中可以同时检测多个视频,每个视频可以同时检测多个风险场景,计费按照场景和视频截帧数计算
// 例如:检测2个视频,场景传递porn,terrorism,计费会按照2个视频的截帧总数乘以鉴黄场景的价格加上2个视频的截帧总数乘以暴恐检测场景的费用计算//本地文件先进行上传,然后进行检测
$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;
                    //根据scene和suggetion做相关处理
                    //do something
                }
            }else{
                print_r("task process fail:" + $response->code);
            }
        }
} catch (Exception $e) {
    print_r($e);
}