文档

视频审核增强版接入指南

更新时间:

视频审核增强版支持SDK方式调用和原生HTTPS方式调用。推荐您使用SDK接入,可免去签名验证以及Body格式构建等细节步骤。本文介绍视频审核增强版接入的方式。

步骤一:开通服务

访问开通内容审核增强版服务页面,开通视频审核增强版服务。接口接入使用后系统会按使用量自动出账,具体信息,请参见计费说明您也可以购买按量抵扣资源包,资源包相较于后付费存在一定阶梯折扣,适合使用量级可预期和较大的用户。

开通视频审核增强版服务后,默认付费方式是按量后付费,且按照实际用量结算当日费用,不调用服务不收费。

步骤二:为RAM用户授权

在接入SDK或者API之前,您需要为RAM用户授权。您可以为RAM用户创建一个访问密钥(AccessKey)。在调用阿里云API时您需要使用AccessKey完成身份验证。获取方式,请参见获取AccessKey

  1. 使用阿里云账号(主账号)或RAM管理员登录RAM控制台

  2. 创建RAM用户。

    具体操作,请参见创建RAM用户

  3. 向RAM用户授权系统策略权限:AliyunYundunGreenWebFullAccess

    具体操作,请参见为RAM用户授权

    完成以上配置后,您可以使用RAM用户调用内容安全API。

步骤三:安装并接入视频审核服务

目前支持的接入地域为:

地域

外网接入地址

内网接入地址

支持的服务

华东2(上海)

green-cip.cn-shanghai.aliyuncs.com

green-cip-vpc.cn-shanghai.aliyuncs.com

videoDetection、liveStreamDetection

华北2(北京)

green-cip.cn-beijing.aliyuncs.com

green-cip-vpc.cn-beijing.aliyuncs.com

华东1(杭州)

green-cip.cn-hangzhou.aliyuncs.com

green-cip-vpc.cn-hangzhou.aliyuncs.com

新加坡

green-cip.ap-southeast-1.aliyuncs.com

green-cip-vpc.ap-southeast-1.aliyuncs.com

videoDetection_cb、liveStreamDetection_cb

说明

如果需要其他语言的SDK示例代码,您可以通过OpenAPI 开发者门户在线调试工具调试API接口,该工具会自动生成相应API的SDK调用示例代码。在线调试API接口如下:

阿里云SDK代码通过定义ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET环境变量来创建默认的访问凭证。调用接口时,程序直接访问凭证,读取您的访问密钥(即AccessKey)并自动完成鉴权。您在接入SDK代码示例之前,请先配置环境变量。具体操作,请参见身份验证配置

Java SDK

在pom.xml中加入相应依赖,就可以在Maven工程中使用SDK。

  1. dependencies中添加如下依赖:

    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>green20220302</artifactId>
      <version>2.1.0</version>
    </dependency>
  2. 接入Java SDK。

    • 提交视频检测任务代码示例

      import com.alibaba.fastjson.JSON;
      import com.alibaba.fastjson.JSONObject;
      import com.aliyun.green20220302.Client;
      import com.aliyun.green20220302.models.VideoModerationRequest;
      import com.aliyun.green20220302.models.VideoModerationResponse;
      import com.aliyun.green20220302.models.VideoModerationResponseBody;
      import com.aliyun.teaopenapi.models.Config;
      
      
      public class VideoModerationDemo {
      
          public static void main(String[] args) throws Exception {
              Config config = new Config();
             /**
               * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
               * 常见获取环境变量方式:
               * 方式一:
               *     获取RAM用户AccessKey ID:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
               *     获取RAM用户AccessKey Secret:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
               * 方式二:
               *     获取RAM用户AccessKey ID:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
               *     获取RAM用户AccessKey Secret:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
               */
              config.setAccessKeyId("建议从环境变量中获取RAM用户AccessKey ID");
              config.setAccessKeySecret("建议从环境变量中获取RAM用户AccessKey Secret");
              //接入区域和地址请根据实际情况修改。
              config.setRegionId("cn-shanghai");
              config.setEndpoint("green-cip.cn-shanghai.aliyuncs.com");
              //连接时超时时间,单位毫秒(ms)。
              config.setReadTimeout(6000);
              //读取时超时时间,单位毫秒(ms)。
              config.setConnectTimeout(3000);
              //设置http代理。
              //config.setHttpProxy("http://10.10.xx.xx:xxxx");
              //设置https代理。
              //config.setHttpsProxy("https://10.10.xx.xx:xxxx");
              Client client = new Client(config);
      
              JSONObject serviceParameters = new JSONObject();
              serviceParameters.put("url", "https://xxx.oss.aliyuncs.com/xxx.mp4");
      
              VideoModerationRequest videoModerationRequest = new VideoModerationRequest();
              // 检测类型:videoDetection
              videoModerationRequest.setService("videoDetection");
              videoModerationRequest.setServiceParameters(serviceParameters.toJSONString());
      
              try {
                  VideoModerationResponse response = client.videoModeration(videoModerationRequest);
                  if (response.getStatusCode() == 200) {
                      VideoModerationResponseBody result = response.getBody();
                      System.out.println(JSON.toJSONString(result));
                      System.out.println("requestId = " + result.getRequestId());
                      System.out.println("code = " + result.getCode());
                      System.out.println("msg = " + result.getMessage());
                      Integer code = result.getCode();
                      if (200 == code) {
                          VideoModerationResponseBody.VideoModerationResponseBodyData data = result.getData();
                          System.out.println("taskId = [" + data.getTaskId() + "]");
                      } else {
                          System.out.println("video moderation not success. code:" + code);
                      }
                  } else {
                      System.out.println("response not success. status:" + response.getStatusCode());
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
    • 获取视频检测任务代码示例

      import com.alibaba.fastjson.JSON;
      import com.alibaba.fastjson.JSONObject;
      import com.aliyun.green20220302.Client;
      import com.aliyun.green20220302.models.VideoModerationResultRequest;
      import com.aliyun.green20220302.models.VideoModerationResultResponse;
      import com.aliyun.green20220302.models.VideoModerationResultResponseBody;
      import com.aliyun.teaopenapi.models.Config;
      
      public class VideoModerationResultDemo {
      
          public static void main(String[] args) throws Exception {
              Config config = new Config();
              /**
               * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
               * 常见获取环境变量方式:
               * 方式一:
               *     获取RAM用户AccessKey ID:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
               *     获取RAM用户AccessKey Secret:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
               * 方式二:
               *     获取RAM用户AccessKey ID:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
               *     获取RAM用户AccessKey Secret:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
               */
              config.setAccessKeyId("建议从环境变量中获取RAM用户AccessKey ID");
              config.setAccessKeySecret("建议从环境变量中获取RAM用户AccessKey Secret");
              //接入区域和地址请根据实际情况修改。
              config.setRegionId("cn-shanghai");
              config.setEndpoint("green-cip.cn-shanghai.aliyuncs.com");
              //连接时超时时间,单位毫秒(ms)。
              config.setReadTimeout(6000);
              //读取时超时时间,单位毫秒(ms)。
              config.setConnectTimeout(3000);
              //设置http代理。
              //config.setHttpProxy("http://10.10.xx.xx:xxxx");
              //设置https代理。
              //config.setHttpsProxy("https://10.10.xx.xx:xxxx");
              Client client = new Client(config);
      
              JSONObject serviceParameters = new JSONObject();
              // 提交任务时返回的taskId。
              serviceParameters.put("taskId", "vi_f_O5z5iaIis3iI0X2oNYj7qa-1x****");
      
      
              VideoModerationResultRequest videoModerationResultRequest = new VideoModerationResultRequest();
              // 检测类型:videoDetection
              videoModerationResultRequest.setService("videoDetection");
              videoModerationResultRequest.setServiceParameters(serviceParameters.toJSONString());
      
              try {
                  VideoModerationResultResponse response = client.videoModerationResult(videoModerationResultRequest);
                  if (response.getStatusCode() == 200) {
                      VideoModerationResultResponseBody result = response.getBody();
                      System.out.println("requestId=" + result.getRequestId());
                      System.out.println("code=" + result.getCode());
                      System.out.println("msg=" + result.getMessage());
                      if (200 == result.getCode()) {
                          VideoModerationResultResponseBody.VideoModerationResultResponseBodyData data = result.getData();
                          System.out.println("dataId = " + data.getDataId());
                          System.out.println("audioResult = " + JSON.toJSONString(data.getAudioResult()));
                          System.out.println("frameResult = " + JSON.toJSONString(data.getFrameResult()));
                      } else {
                          System.out.println("video moderation result not success. code:" + result.getCode());
                      }
                  } else {
                      System.out.println("response not success. status:" + response.getStatusCode());
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
    • 取消视频直播流检测任务代码示例

      import com.alibaba.fastjson.JSONObject;
      import com.aliyun.green20220302.Client;
      import com.aliyun.green20220302.models.VideoModerationCancelRequest;
      import com.aliyun.green20220302.models.VideoModerationCancelResponse;
      import com.aliyun.green20220302.models.VideoModerationCancelResponseBody;
      import com.aliyun.teaopenapi.models.Config;
      
      
      public class VideoModerationCancelDemo {
      
          public static void main(String[] args) throws Exception {
              Config config = new Config();
          		/**
               * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
               * 常见获取环境变量方式:
               * 方式一:
               *     获取RAM用户AccessKey ID:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
               *     获取RAM用户AccessKey Secret:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
               * 方式二:
               *     获取RAM用户AccessKey ID:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
               *     获取RAM用户AccessKey Secret:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
               */
              config.setAccessKeyId("建议从环境变量中获取RAM用户AccessKey ID");
              config.setAccessKeySecret("建议从环境变量中获取RAM用户AccessKey Secret");
              //接入区域和地址请根据实际情况修改
              config.setRegionId("cn-shanghai");
              config.setEndpoint("green-cip.cn-shanghai.aliyuncs.com");
              //连接时超时时间,单位毫秒(ms)。
              config.setReadTimeout(6000);
              //读取时超时时间,单位毫秒(ms)。
              config.setConnectTimeout(3000);
              //设置http代理。
              //config.setHttpProxy("http://xx.xx.xx.xx:xxxx");
              //设置https代理。
              //config.setHttpsProxy("https://xx.xx.xx.xx:xxxx");
              Client client = new Client(config);
      
              JSONObject serviceParameters = new JSONObject();
              // 提交任务时返回的taskId
              serviceParameters.put("taskId", "vi_f_O5z5iaIis3iI0X2oNYj7qa-1x****");
      
              VideoModerationCancelRequest videoModerationCancelRequest = new VideoModerationCancelRequest();
              
              videoModerationCancelRequest.setService("liveStreamDetection");
              videoModerationCancelRequest.setServiceParameters(serviceParameters.toJSONString());
      
              try {
                  VideoModerationCancelResponse response = client.videoModerationCancel(videoModerationCancelRequest);
                  if (response.getStatusCode() == 200) {
                      VideoModerationCancelResponseBody result = response.getBody();
                      System.out.println("requestId=" + result.getRequestId());
                      System.out.println("code=" + result.getCode());
                      System.out.println("msg=" + result.getMessage());
                      if (200 != result.getCode()) {
                          System.out.println("video moderation cancel not success. code:" + result.getCode());
                      }
                  } else {
                      System.out.println("response not success. status:" + response.getStatusCode());
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }

Python SDK

支持Python 3.6及以上版本。

  1. 执行如下命令安装pip。

    pip install alibabacloud_green20220302==2.1.0
  2. 接入Python SDK。

    • 提交视频检测任务代码示例

      #encoding:utf-8
      # python version >= 3.6
      from alibabacloud_green20220302.client import Client
      from alibabacloud_green20220302 import models
      from alibabacloud_tea_openapi.models import Config
      import json
      
      config = Config(
                  # 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
                  # 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
                  # 常见获取环境变量方式:
                  # 获取RAM用户AccessKey ID:os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
                  # 获取RAM用户AccessKey Secret:os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
                  access_key_id='建议从环境变量中获取RAM用户AccessKey ID',
                  access_key_secret='建议从环境变量中获取RAM用户AccessKey Secret',
                  # 连接时超时时间,单位毫秒(ms)。
                  connect_timeout=3000,
                  # 读取时超时时间,单位毫秒(ms)。
                  read_timeout=6000,
                  # 接入区域和地址请根据实际情况修改。
                  region_id='cn-shanghai',
                  endpoint='green-cip.cn-shanghai.aliyuncs.com'
      )
      
      clt = Client(config)
      
      serviceParameters = {
          'url': 'https://xxx.oss.aliyuncs.com/xxx.mp4'
      }
      videoModerationRequest = models.VideoModerationRequest(
          # 检测类型:videoDetection
          service='videoDetection',
          service_parameters=json.dumps(serviceParameters)
      )
      
      try:
          response = clt.video_moderation(videoModerationRequest)
          if response.status_code == 200:
              # 调用成功。
              # 获取审核结果。
              result = response.body
              print('response success. result:{}'.format(result))
          else:
              print('response not success. status:{} ,result:{}'.format(response.status_code, response))
      except Exception as err:
          print(err)
    • 获取视频检测任务结果

      # coding=utf-8
      # python version >= 3.6
      from alibabacloud_green20220302.client import Client
      from alibabacloud_green20220302 import models
      from alibabacloud_tea_openapi.models import Config
      import json
      
      config = Config(
                  # 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
                  # 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
                  # 常见获取环境变量方式:
                  # 获取RAM用户AccessKey ID:os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
                  # 获取RAM用户AccessKey Secret:os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
                  access_key_id='建议从环境变量中获取RAM用户AccessKey ID',
                  access_key_secret='建议从环境变量中获取RAM用户AccessKey Secret',
                  # 连接时超时时间,单位毫秒(ms)。
                  connect_timeout=3000,
                  # 读取时超时时间,单位毫秒(ms)。
                  read_timeout=6000,
                  # 接入区域和地址请根据实际情况修改。
                  region_id='cn-shanghai',
                  endpoint='green-cip.cn-shanghai.aliyuncs.com'
      )
      
      clt = Client(config)
      
      # 提交任务时返回的taskId。
      serviceParameters = {
          "taskId": 'vi_f_11w5THcbVYctjdxz2C0Afa-1x****'
      }
      videoModerationResultRequest = models.VideoModerationResultRequest(
          # 检测类型:videoDetection
          service='videoDetection',
          service_parameters=json.dumps(serviceParameters)
      )
      
      try:
          response = clt.video_moderation_result(videoModerationResultRequest)
          if response.status_code == 200:
              # 调用成功。
              # 获取审核结果。
              result = response.body
              print('response success. result:{}'.format(result))
          else:
              print('response not success. status:{} ,result:{}'.format(response.status_code, response))
      except Exception as err:
          print(err)
    • 取消视频直播流检测任务代码示例

      # coding=utf-8
      # python version >= 3.6
      from alibabacloud_green20220302.client import Client
      from alibabacloud_green20220302 import models
      from alibabacloud_tea_openapi.models import Config
      import json
      
      config = Config(
          # 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
          # 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
          # 常见获取环境变量方式:
          # 获取RAM用户AccessKey ID:os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
          # 获取RAM用户AccessKey Secret:os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
          access_key_id='建议从环境变量中获取RAM用户AccessKey ID',
          access_key_secret='建议从环境变量中获取RAM用户AccessKey Secret',
          # 连接超时时间 单位毫秒(ms)
          connect_timeout=10000,
          # 读超时时间 单位毫秒(ms)
          read_timeout=3000,
          region_id='cn-shanghai',
          endpoint='green-cip.cn-shanghai.aliyuncs.com'
      )
      
      clt = Client(config)
      
      # 提交任务时返回的taskId
      serviceParameters = {
          "taskId": 'vi_f_O5z5iaIis3iI0X2oNYj7qa-1x****'
      }
      videoModerationCancelRequest = models.VideoModerationCancelRequest(
          # 检测类型
          service='liveStreamDetection',
          service_parameters=json.dumps(serviceParameters)
      )
      
      try:
          response = clt.video_moderation_cancel(videoModerationCancelRequest)
          if response.status_code == 200:
              # 调用成功
              result = response.body
              print('response success. result:{}'.format(result))
          else:
              print('response not success. status:{} ,result:{}'.format(response.status_code, response))
      except Exception as err:
          print(err)

PHP SDK

支持PHP 5.6及以上版本。

  1. 执行如下命令引入相关依赖。

    composer require alibabacloud/green-20220302
  2. 接入PHP SDK。

    • 提交视频检测任务代码示例

      <?php
      require('vendor/autoload.php');
      
      use AlibabaCloud\SDK\Green\V20220302\Models\VideoModerationRequest;
      use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
      use Darabonba\OpenApi\Models\Config;
      use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
      use AlibabaCloud\SDK\Green\V20220302\Green;
      
      $config = new Config([
          /**
           * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
           * 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
           * 常见获取环境变量方式:
           * 获取RAM用户AccessKey ID:getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
           * 获取RAM用户AccessKey Secret:getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
           */
          "accessKeyId" => '建议从环境变量中获取RAM用户AccessKey ID',
          "accessKeySecret" => '建议从环境变量中获取RAM用户AccessKey Secret',
          // 设置HTTP代理。
          // "httpProxy" => "http://10.10.xx.xx:xxxx",
          // 设置HTTPS代理。
          // "httpsProxy" => "https://10.10.xx.xx:xxxx",
          "endpoint" => "green-cip.cn-shanghai.aliyuncs.com",
          "regionId" => "cn-shanghai"
      
      ]);
      // 注意,此处实例化的client请尽可能重复使用,避免重复建立连接,提升检测性能。
      $client = new Green($config);
      
      $request = new VideoModerationRequest();
      // 检测类型: videoDetection 视频文件检测, liveStreamDetection 视频直播流检测
      $request->service = "videoDetection";
      $serviceParameters = array("url" => "https://xxx.oss.aliyuncs.com/xxx.mp4");
      
      $request->serviceParameters = json_encode($serviceParameters);
      
      $runtime = new RuntimeOptions();
      $runtime->readTimeout = 6000;
      $runtime->connectTimeout = 3000;
      
      try {
          $response = $client->videoModerationWithOptions($request, $runtime);
          print_r($response->body);
          if (200 != $response->statusCode) {
              print_r("response not success. code:" . $response->statusCode);
              return;
          }
          $body = $response->body;
          print_r("requestId = " . $body->requestId . "\n");
          print_r("code = " . $body->code . "\n");
          print_r("message = " . $body->message . "\n");
          if (200 != $body->code) {
              print_r("video moderation not success. code:" . $body->code);
          }
          $data = $body->data;
          print_r("taskId = " . $data->taskId);
      } catch (TeaUnableRetryError $e) {
          var_dump($e->getMessage());
          var_dump($e->getErrorInfo());
          var_dump($e->getLastException());
          var_dump($e->getLastRequest());
      }
    • 获取视频检测任务代码示例

      <?php
      require('vendor/autoload.php');
      
      use AlibabaCloud\SDK\Green\V20220302\Models\VideoModerationResultRequest;
      use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
      use Darabonba\OpenApi\Models\Config;
      use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
      use AlibabaCloud\SDK\Green\V20220302\Green;
      
      $config = new Config([
          /**
           * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
           * 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
           * 常见获取环境变量方式:
           * 获取RAM用户AccessKey ID:getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
           * 获取RAM用户AccessKey Secret:getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
           */
          "accessKeyId" => '建议从环境变量中获取RAM用户AccessKey ID',
          "accessKeySecret" => '建议从环境变量中获取RAM用户AccessKey Secret',
          // 设置HTTP代理。
          // "httpProxy" => "http://10.10.xx.xx:xxxx",
          // 设置HTTPS代理。
          // "httpsProxy" => "https://10.10.xx.xx:xxxx",
          "endpoint" => "green-cip.cn-shanghai.aliyuncs.com",
          "regionId" => "cn-shanghai"
      
      ]);
      // 注意,此处实例化的client请尽可能重复使用,避免重复建立连接,提升检测性能。
      $client = new Green($config);
      
      $request = new VideoModerationResultRequest();
      // 检测类型: videoDetection 视频文件检测, liveStreamDetection 视频直播流检测
      $request->service = "videoDetection";
      $serviceParameters = array("taskId" => "vi_f_O5z5iaIis3iI0X2oNYj7qa-1x****");
      
      $request->serviceParameters = json_encode($serviceParameters);
      
      $runtime = new RuntimeOptions();
      $runtime->readTimeout = 6000;
      $runtime->connectTimeout = 3000;
      
      try {
          $response = $client->videoModerationResultWithOptions($request, $runtime);
          if (200 != $response->statusCode) {
              print_r("response not success. code:" . $response->statusCode);
              return;
          }
          $body = $response->body;
          print_r("requestId = " . $body->requestId . "\n");
          print_r("code = " . $body->code . "\n");
          print_r("message = " . $body->message . "\n");
          if (280 == $body->code) {
              print_r("processing video moderation. code:" . $body->code);
              return;
          }
          if (200 != $body->code) {
              print_r("video moderation result not success. code:" . $body->code);
              return;
          }
          $data = $body->data;
          print_r("liveId = " . $data->liveId . "\n");
          print_r("dataId = " . $data->dataId . "\n");
          print_r("audioResult = " . json_encode($data->audioResult) . "\n");
          print_r("frameResult = " . json_encode($data->frameResult) . "\n");
      } catch (TeaUnableRetryError $e) {
          var_dump($e->getMessage());
          var_dump($e->getErrorInfo());
          var_dump($e->getLastException());
          var_dump($e->getLastRequest());
      }
    • 取消视频直播流检测任务代码示例

      <?php
        require('vendor/autoload.php');
      
      use AlibabaCloud\SDK\Green\V20220302\Models\VideoModerationCancelRequest;
      use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
      use Darabonba\OpenApi\Models\Config;
      use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
      use AlibabaCloud\SDK\Green\V20220302\Green;
      
      $config = new Config([
          /**
           * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
           * 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
           * 常见获取环境变量方式:
           * 获取RAM用户AccessKey ID:getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
           * 获取RAM用户AccessKey Secret:getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
           */
          "accessKeyId" => '建议从环境变量中获取RAM用户AccessKey ID',
          "accessKeySecret" => '建议从环境变量中获取RAM用户AccessKey Secret',
          // 设置HTTP代理。
          // "httpProxy" => "http://10.10.xx.xx:xxxx",
          // 设置HTTPS代理。
          // "httpsProxy" => "https://10.10.xx.xx:xxxx",
          "endpoint" => "green-cip.cn-shanghai.aliyuncs.com",
          "regionId" => "cn-shanghai"
      
      ]);
      // 注意,此处实例化的client请尽可能重复使用,避免重复建立连接,提升检测性能。
      $client = new Green($config);
      
      $request = new VideoModerationCancelRequest();
      // 检测类型: videoDetection 视频文件检测, liveStreamDetection 视频直播流检测
      $request->service = "liveStreamDetection";
      $serviceParameters = array('taskId' => 'vi_s_lyxBXzWhSsxuJjiNHcpQ0N-*****');
      
      $request->serviceParameters = json_encode($serviceParameters);
      
      $runtime = new RuntimeOptions();
      $runtime->readTimeout = 6000;
      $runtime->connectTimeout = 3000;
      
      try {
        $response = $client->videoModerationCancel($request, $runtime);
        print_r($response->body);
        if (200 == $response->statusCode) {
          $body = $response->body;
          print_r("requestId = " . $body->requestId);
          print_r("code = " . $body->code);
          print_r("message = " . $body->message);
          if (200 != $body->code) {
            print_r("video moderation cancel not success. code:" . $body->code);
          }
        } else {
          print_r("response not success. code:" . $response->statusCode);
        }
      } catch (TeaUnableRetryError $e) {
        var_dump($e->getMessage());
        var_dump($e->getErrorInfo());
        var_dump($e->getLastException());
        var_dump($e->getLastRequest());
      }

Go SDK

  1. 执行如下命令引入相关依赖。

    go get github.com/alibabacloud-go/green-20220302
  2. 接入Go SDK。

    • 提交视频检测任务代码示例

      package main
      
      import (
          "encoding/json"
          "fmt"
          openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
          green20220302 "github.com/alibabacloud-go/green-20220302/client"
          util "github.com/alibabacloud-go/tea-utils/v2/service"
          "github.com/alibabacloud-go/tea/tea"
          "net/http"
      )
      
      func main() {
          // 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的STS方式。
          config := &openapi.Config{
              /**
               * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
               * 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
               * 常见获取环境变量方式:
               * 获取RAM用户AccessKey ID:os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
               * 获取RAM用户AccessKey Secret:os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
               */
              AccessKeyId: tea.String("建议从环境变量中获取RAM用户AccessKey ID"),
              AccessKeySecret: tea.String("建议从环境变量中获取RAM用户AccessKey Secret"),
              // 设置HTTP代理。
              // HttpProxy: tea.String("http://xx.xx.xx.xx:xxxx"),
              // 设置HTTPS代理。
              // HttpsProxy: tea.String("https://username:password@xxx.xxx.xxx.xxx:9999"),
              RegionId: tea.String("cn-shanghai"),
              Endpoint: tea.String("green-cip.cn-shanghai.aliyuncs.com"),
              /**
      		 * 请设置超时时间。服务端全链路处理超时时间为10秒,请做相应设置。
      		 * 如果您设置的ReadTimeout小于服务端处理的时间,程序中会获得一个ReadTimeout异常。
      		 */
              ConnectTimeout: tea.Int(3000),
              ReadTimeout:    tea.Int(6000),
          }
          client, _err := green20220302.NewClient(config)
          if _err != nil {
              panic(_err)
          }
      
          // 创建RuntimeObject实例并设置运行参数。
          runtime := &util.RuntimeOptions{}
          runtime.ReadTimeout = tea.Int(10000)
          runtime.ConnectTimeout = tea.Int(10000)
      
          serviceParameters, _ := json.Marshal(
              map[string]interface{}{
                  "url": "https://xxx.oss.aliyuncs.com/xxx.mp4",
              },
          )
          request := green20220302.VideoModerationRequest{
              Service:           tea.String("videoDetection"),
              ServiceParameters: tea.String(string(serviceParameters)),
          }
      
          result, _err := client.VideoModerationWithOptions(&request, runtime)
          if _err != nil {
              panic(_err)
          }
      
          if *result.StatusCode != http.StatusOK {
              fmt.Printf("response not success. status:%d\n", *result.StatusCode)
              return
          }
          body := result.Body
          fmt.Printf("response success. requestId:%s, code:%d, msg:%s\n", *body.RequestId, *body.Code, *body.Message)
          if *body.Code != http.StatusOK {
              fmt.Printf("video moderation not success. code:%d\n", *body.Code)
              return
          }
          
          data := body.Data
          fmt.Printf("video moderation taskId:%s\n", *data.TaskId)
      }
    • 获取视频检测任务结果

      package main
      
      import (
      	"encoding/json"
      	"fmt"
      	openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
      	green20220302 "github.com/alibabacloud-go/green-20220302/client"
      	util "github.com/alibabacloud-go/tea-utils/v2/service"
      	"github.com/alibabacloud-go/tea/tea"
      	"net/http"
      )
      
      func main() {
      	// 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的STS方式。
          config := &openapi.Config{
              /**
               * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
               * 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
               * 常见获取环境变量方式:
               * 获取RAM用户AccessKey ID:os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
               * 获取RAM用户AccessKey Secret:os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
               */
              AccessKeyId: tea.String("建议从环境变量中获取RAM用户AccessKey ID"),
              AccessKeySecret: tea.String("建议从环境变量中获取RAM用户AccessKey Secret"),
              // 设置HTTP代理。
              // HttpProxy: tea.String("http://xx.xx.xx.xx:xxxx"),
              // 设置HTTPS代理。
              // HttpsProxy: tea.String("https://username:password@xxx.xxx.xxx.xxx:9999"),
              RegionId: tea.String("cn-shanghai"),
              Endpoint: tea.String("green-cip.cn-shanghai.aliyuncs.com"),
              /**
      		 * 请设置超时时间。服务端全链路处理超时时间为10秒,请做相应设置。
      		 * 如果您设置的ReadTimeout小于服务端处理的时间,程序中会获得一个ReadTimeout异常。
      		 */
              ConnectTimeout: tea.Int(3000),
              ReadTimeout:    tea.Int(6000),
          }
      	client, _err := green20220302.NewClient(config)
      	if _err != nil {
      		panic(_err)
      	}
      
      	// 创建RuntimeObject实例并设置运行参数。
      	runtime := &util.RuntimeOptions{}
      	runtime.ReadTimeout = tea.Int(10000)
      	runtime.ConnectTimeout = tea.Int(10000)
      
      	serviceParameters, _ := json.Marshal(
      		map[string]interface{}{
      			"taskId": "vi_f_O5z5iaIis3iI0X2oNYj7qa-1x****",
      		},
      	)
      	request := green20220302.VideoModerationResultRequest{
      		Service:           tea.String("videoDetection"),
      		ServiceParameters: tea.String(string(serviceParameters)),
      	}
      
      	result, _err := client.VideoModerationResultWithOptions(&request, runtime)
      	if _err != nil {
      		panic(_err)
      	}
      
      	if *result.StatusCode != http.StatusOK {
      		fmt.Printf("response not success. status:%d\n", *result.StatusCode)
      		return
      	}
      	body := result.Body
      	fmt.Printf("response success. requestId:%s, code:%d, msg:%s\n", *body.RequestId, *body.Code, *body.Message)
      	if *body.Code == 280 {
      		fmt.Printf("processing video moderation. code:%d\n", *body.Code)
      		return
      	}
      	if *body.Code != http.StatusOK {
      		fmt.Printf("video moderation result not success. code:%d\n", *body.Code)
      		return
      	}
      
      	data := body.Data
          fmt.Printf("video moderation result:%s\n", data)
      	fmt.Printf("video moderation result audioResult:%s\n", data.AudioResult)
      	fmt.Printf("video moderation result frameResult:%s\n", data.FrameResult)
      }
    • 取消视频直播流检测任务代码示例

      package main
      
      import (
          "encoding/json"
          "fmt"
          openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
          green20220302 "github.com/alibabacloud-go/green-20220302/client"
          util "github.com/alibabacloud-go/tea-utils/v2/service"
          "github.com/alibabacloud-go/tea/tea"
          "net/http"
      )
      
      func main() {
      	// 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的STS方式。
          config := &openapi.Config{
              /**
               * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
               * 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
               * 常见获取环境变量方式:
               * 获取RAM用户AccessKey ID:os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
               * 获取RAM用户AccessKey Secret:os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
               */
              AccessKeyId: tea.String("建议从环境变量中获取RAM用户AccessKey ID"),
              AccessKeySecret: tea.String("建议从环境变量中获取RAM用户AccessKey Secret"),
              // 设置HTTP代理。
              // HttpProxy: tea.String("http://xx.xx.xx.xx:xxxx"),
              // 设置HTTPS代理。
              // HttpsProxy: tea.String("https://username:password@xxx.xxx.xxx.xxx:9999"),
              RegionId: tea.String("cn-shanghai"),
              Endpoint: tea.String("green-cip.cn-shanghai.aliyuncs.com"),
              /**
      		 * 请设置超时时间。服务端全链路处理超时时间为10秒,请做相应设置。
      		 * 如果您设置的ReadTimeout小于服务端处理的时间,程序中会获得一个ReadTimeout异常。
      		 */
              ConnectTimeout: tea.Int(3000),
              ReadTimeout:    tea.Int(6000),
          }
      	client, _err := green20220302.NewClient(config)
      	if _err != nil {
      		panic(_err)
      	}
      
          // 创建RuntimeObject实例并设置运行参数。
          runtime := &util.RuntimeOptions{}
          runtime.ReadTimeout = tea.Int(10000)
          runtime.ConnectTimeout = tea.Int(10000)
      
          serviceParameters, _ := json.Marshal(
              map[string]interface{}{
                  "taskId": "vi_s_lyxBXzWhSsxuJjiNHcpQ0N-*****",
              },
          )
          request := green20220302.VideoModerationCancelRequest{
              Service:           tea.String("liveStreamDetection"),
              ServiceParameters: tea.String(string(serviceParameters)),
          }
      
          result, _err := client.VideoModerationCancelWithOptions(&request, runtime)
          if _err != nil {
              panic(_err)
          }
      
          if *result.StatusCode != http.StatusOK {
              fmt.Printf("response not success. status:%d\n", *result.StatusCode)
              return
          }
          body := result.Body
          fmt.Printf("response success. requestId:%s, code:%d, msg:%s\n", *body.RequestId, *body.Code, *body.Message)
          if *body.Code != http.StatusOK {
              fmt.Printf("video moderation cancel not success. code:%d\n", *body.Code)
          }
      }

  • 本页导读 (0)
文档反馈