You can call the Video Moderation 2.0 API by using SDKs or over HTTPS. We recommend that you use SDKs to call the API so as to skip some operations such as signature verification and body format construction. This topic describes how to use Video Moderation 2.0.
Step 1: Activate Video Moderation 2.0
Open the Content Moderation V2.0 page and activate the Video Moderation 2.0 service. After you call API operations, the billing system automatically charges you based on your usage. For more information, see Billing methods.
After you activate the Video Moderation 2.0 service, the default billing method is pay-as-you-go. Daily fees are calculated based on the actual usage. If the service is not called, no charge is made.
Step 2: Grant permissions to a RAM user
Before you call API operations or use SDKs as a Resource Access Management (RAM) user, you must grant permissions to the RAM user. You can create an AccessKey pair for the RAM user. When you call API operations, you must use the AccessKey pair to complete identity verification. For information about how to obtain an AccessKey pair, see Obtain an AccessKey pair.
Procedure
Log on to the RAM console as a RAM administrator.
- Create a RAM user.
For more information, see Create a RAM user.
- Grant the
AliyunYundunGreenWebFullAccess
system policy to the RAM user.For more information, see Grant permissions to a RAM user.
After completing the preceding operations, you can call the Content Moderation API as the RAM user.
Step 3: Install SDKs and use the SDKs to call the Video Moderation 2.0 service
The following table provides the supported regions.
Region | Public endpoint | Internal endpoint | Supported service |
Singapore | green-cip.ap-southeast-1.aliyuncs.com | green-cip-vpc.ap-southeast-1.aliyuncs.com | videoDetection_global and liveStreamDetection_global |
If you need SDK sample code in other programming languages, you can call an API operation in OpenAPI Explorer. OpenAPI Explorer dynamically generates the sample code of the operation for different SDKs. The following API operations are available for online debugging:
In Alibaba Cloud SDK code, you can create a default access credential by defining ALIBABA_CLOUD_ACCESS_KEY_ID
and ALIBABA_CLOUD_ACCESS_KEY_SECRET
environment variables. When you call API operations of Alibaba Cloud services, the system directly accesses the credential, reads your AccessKey pair, and then automatically completes authentication. Before you use the SDK sample code, you must configure environment variables. For more information, see Configure credentials.
For explanations of the API fields, see Video File Moderation 2.0 API and Live Stream Moderation 2.0 API.
SDK for Java
After you add the related dependencies to the pom.xml file, you can use the SDK in Maven projects.
Add the following dependencies to the dependencies field:
<dependency> <groupId>com.aliyun</groupId> <artifactId>green20220302</artifactId> <version>2.2.14</version> </dependency>
Use the SDK for Java.
Sample code for submitting a video moderation task
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(); /** * The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M. * Common ways to obtain environment variables: * Method 1: * Obtain the AccessKey ID of your RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"); * Obtain the AccessKey secret of your RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); * Method 2: * Obtain the AccessKey ID of your RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID"); * Obtain the AccessKey secret of your RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); */ config.setAccessKeyId("We recommend that you obtain the AccessKey ID of your RAM user from environment variables"); config.setAccessKeySecret("We recommend that you obtain the AccessKey secret of your RAM user from environment variables"); // Change the access region ID and endpoint based on your business requirements. config.setRegionId("ap-southeast-1"); config.setEndpoint("green-cip.ap-southeast-1.aliyuncs.com"); // The connection timeout period. Unit: milliseconds. config.setReadTimeout(6000); // The read timeout period. Unit: milliseconds. config.setConnectTimeout(3000); // Configure an HTTP proxy. //config.setHttpProxy("http://10.10.xx.xx:xxxx"); // Configure an HTTPS proxy. //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(); // Moderation type: videoDetection_global videoModerationRequest.setService("videoDetection_global"); 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(); } } }
Sample code for obtaining video moderation results
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(); /** * The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M. * Common ways to obtain environment variables: * Method 1: * Obtain the AccessKey ID of your RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"); * Obtain the AccessKey secret of your RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); * Method 2: * Obtain the AccessKey ID of your RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID"); * Obtain the AccessKey secret of your RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); */ config.setAccessKeyId("We recommend that you obtain the AccessKey ID of your RAM user from environment variables"); config.setAccessKeySecret("We recommend that you obtain the AccessKey secret of your RAM user from environment variables"); // Change the access region ID and endpoint based on your business requirements. config.setRegionId("ap-southeast-1"); config.setEndpoint("green-cip.ap-southeast-1.aliyuncs.com"); // The connection timeout period. Unit: milliseconds. config.setReadTimeout(6000); // The read timeout period. Unit: milliseconds. config.setConnectTimeout(3000); // Configure an HTTP proxy. //config.setHttpProxy("http://10.10.xx.xx:xxxx"); // Configure an HTTPS proxy. //config.setHttpsProxy("https://10.10.xx.xx:xxxx"); Client client = new Client(config); JSONObject serviceParameters = new JSONObject(); // The task ID that is returned when a video moderation task is submitted. serviceParameters.put("taskId", "vi_f_O5z5iaIis3iI0X2oNYj7qa-1x****"); VideoModerationResultRequest videoModerationResultRequest = new VideoModerationResultRequest(); // Moderation type: videoDetection_global videoModerationResultRequest.setService("videoDetection_global"); 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(); } } }
Sample code for canceling a live stream moderation task
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(); /** * The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M. * Common ways to obtain environment variables: * Method 1: * Obtain the AccessKey ID of your RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"); * Obtain the AccessKey secret of your RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); * Method 2: * Obtain the AccessKey ID of your RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID"); * Obtain the AccessKey secret of your RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); */ config.setAccessKeyId("We recommend that you obtain the AccessKey ID of your RAM user from environment variables"); config.setAccessKeySecret("We recommend that you obtain the AccessKey secret of your RAM user from environment variables"); // Change the region ID and endpoint based on your business requirements. config.setRegionId("ap-southeast-1"); config.setEndpoint("green-cip.ap-southeast-1.aliyuncs.com"); // The connection timeout period. Unit: milliseconds. config.setReadTimeout(6000); // The read timeout period. Unit: milliseconds. config.setConnectTimeout(3000); // Configure an HTTP proxy. //config.setHttpProxy("http://xx.xx.xx.xx:xxxx"); // Configure an HTTPS proxy. //config.setHttpsProxy("https://xx.xx.xx.xx:xxxx"); Client client = new Client(config); JSONObject serviceParameters = new JSONObject(); // The task ID that is returned when the task is submitted. serviceParameters.put("taskId", "vi_f_O5z5iaIis3iI0X2oNYj7qa-1x****"); VideoModerationCancelRequest videoModerationCancelRequest = new VideoModerationCancelRequest(); videoModerationCancelRequest.setService("liveStreamDetection_global"); 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(); } } }
SDK for Python
The Python version must be 3.6 or later.
Run the following command to install pip:
pip install alibabacloud_green20220302==2.2.14
Use the SDK for Python.
Sample code for submitting a video moderation task
#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( # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M. # We recommend that you do not save the AccessKey ID and the AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, and the security of resources within your account may be compromised. # Common ways to obtain environment variables: # Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'] # Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'] access_key_id='We recommend that you obtain the AccessKey ID of your RAM user from environment variables', access_key_secret='We recommend that you obtain the AccessKey secret of your RAM user from environment variables', # The connection timeout period. Unit: milliseconds. connect_timeout=3000, # The read timeout period. Unit: milliseconds. read_timeout=6000, # Change the access region ID and endpoint based on your business requirements. region_id='ap-southeast-1', endpoint='green-cip.ap-southeast-1.aliyuncs.com' ) clt = Client(config) serviceParameters = { 'url': 'https://xxx.oss.aliyuncs.com/xxx.mp4' } videoModerationRequest = models.VideoModerationRequest( // Moderation type: videoDetection_global service='videoDetection_global', service_parameters=json.dumps(serviceParameters) ) try: response = clt.video_moderation(videoModerationRequest) if response.status_code == 200: # The call is successful. # Obtain the moderation results. 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)
Sample code for obtaining video moderation results
# 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( # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M. # We recommend that you do not save the AccessKey ID and the AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, and the security of resources within your account may be compromised. # Common ways to obtain environment variables: # Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'] # Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'] access_key_id='We recommend that you obtain the AccessKey ID of your RAM user from environment variables', access_key_secret='We recommend that you obtain the AccessKey secret of your RAM user from environment variables', # The connection timeout period. Unit: milliseconds. connect_timeout=3000, # The read timeout period. Unit: milliseconds. read_timeout=6000, # Change the access region ID and endpoint based on your business requirements. region_id='ap-southeast-1', endpoint='green-cip.ap-southeast-1.aliyuncs.com' ) clt = Client(config) // The task ID that is returned when a video moderation task is submitted. serviceParameters = { "taskId": 'vi_f_11w5THcbVYctjdxz2C0Afa-1x****' } videoModerationResultRequest = models.VideoModerationResultRequest( // Moderation type: videoDetection_global service='videoDetection_global', service_parameters=json.dumps(serviceParameters) ) try: response = clt.video_moderation_result(videoModerationResultRequest) if response.status_code == 200: # The call is successful. # Obtain the moderation results. 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)
Sample code for canceling a live stream moderation task
# 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( # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M. # We recommend that you do not save the AccessKey ID and the AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of resources within your account may be compromised. # Common ways to obtain environment variables: # Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'] # Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'] access_key_id='We recommend that you obtain the AccessKey ID of your RAM user from environment variables', access_key_secret='We recommend that you obtain the AccessKey secret of your RAM user from environment variables', # The connection timeout period. Unit: milliseconds. connect_timeout=10000, # The read timeout period. Unit: milliseconds. read_timeout=3000, region_id='ap-southeast-1', endpoint='green-cip.ap-southeast-1.aliyuncs.com' ) clt = Client(config) # The task ID returned when the task is submitted. serviceParameters = { "taskId": 'vi_f_O5z5iaIis3iI0X2oNYj7qa-1x****' } videoModerationCancelRequest = models.VideoModerationCancelRequest( # The moderation type. service='liveStreamDetection_global', service_parameters=json.dumps(serviceParameters) ) try: response = clt.video_moderation_cancel(videoModerationCancelRequest) if response.status_code == 200: # The call is successful. 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)
SDK for PHP
The PHP version must be 5.6 or later.
Run the following command to install the related dependencies:
composer require alibabacloud/green-20220302 2.2.14
Use the SDK for PHP.
Sample code for submitting a video moderation task
<?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([ /** * The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M. * We recommend that you do not save the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, and the security of resources within your account may be compromised. * Common ways to obtain environment variables: * Obtain the AccessKey ID of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"); * Obtain the AccessKey secret of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); */ "accessKeyId" => 'We recommend that you obtain the AccessKey ID of your RAM user from environment variables', "accessKeySecret" => 'We recommend that you obtain the AccessKey secret of your RAM user from environment variables', // Configure an HTTP proxy. // "httpProxy" => "http://10.10.xx.xx:xxxx", // Configure an HTTPS proxy. // "httpsProxy" => "https://10.10.xx.xx:xxxx", "endpoint" => "green-cip.ap-southeast-1.aliyuncs.com", "regionId" => "ap-southeast-1" ]); // Note: We recommend that you reuse the instantiated client as much as possible to avoid repeatedly establishing connections and improve the moderation performance. $client = new Green($config); $request = new VideoModerationRequest(); // Moderation type: videoDetection_global for video file moderation and liveStreamDetection_global for live stream moderation. $request->service = "videoDetection_global"; $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()); }
Sample code for obtaining video moderation results
<?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([ /** * The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M. * We recommend that you do not save the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, and the security of resources within your account may be compromised. * Common ways to obtain environment variables: * Obtain the AccessKey ID of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"); * Obtain the AccessKey secret of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); */ "accessKeyId" => 'We recommend that you obtain the AccessKey ID of your RAM user from environment variables', "accessKeySecret" => 'We recommend that you obtain the AccessKey secret of your RAM user from environment variables', // Configure an HTTP proxy. // "httpProxy" => "http://10.10.xx.xx:xxxx", // Configure an HTTPS proxy. // "httpsProxy" => "https://10.10.xx.xx:xxxx", "endpoint" => "green-cip.ap-southeast-1.aliyuncs.com", "regionId" => "ap-southeast-1" ]); // Note: We recommend that you reuse the instantiated client as much as possible to avoid repeatedly establishing connections and improve the moderation performance. $client = new Green($config); $request = new VideoModerationResultRequest(); // Moderation type: videoDetection for video file moderation and liveStreamDetection for live stream moderation. $request->service = "videoDetection_global"; $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()); }
Sample code for canceling a live stream moderation task
<?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([ /** * The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M. * We recommend that you do not save the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, and the security of resources within your account may be compromised. * Common ways to obtain environment variables: * Obtain the AccessKey ID of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"); * Obtain the AccessKey secret of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); */ "accessKeyId" => 'We recommend that you obtain the AccessKey ID of your RAM user from environment variables', "accessKeySecret" => 'We recommend that you obtain the AccessKey secret of your RAM user from environment variables', // Configure an HTTP proxy. // "httpProxy" => "http://10.10.xx.xx:xxxx", // Configure an HTTPS proxy. // "httpsProxy" => "https://10.10.xx.xx:xxxx", "endpoint" => "green-cip.ap-southeast-1.aliyuncs.com", "regionId" => "ap-southeast-1" ]); // Note: We recommend that you reuse the instantiated client as much as possible to avoid repeatedly establishing connections and improve the moderation performance. $client = new Green($config); $request = new VideoModerationCancelRequest(); // Moderation type: videoDetection for video file moderation and liveStreamDetection for live stream moderation. $request->service = "liveStreamDetection_global"; $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()); }
SDK for Go
Run the following command to install the related dependencies:
go git clone --branch v2.2.15 github.com/alibabacloud-go/green-20220302/v2
Use the SDK for Go.
Sample code for submitting a video moderation task
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() { // If the project code is leaked, the AccessKey pair may be leaked and the security of resources within your account may be compromised. The following sample code is provided for reference only. We recommend that you use a securer STS method. config := &openapi.Config{ /** * The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M. * We recommend that you do not save the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, and the security of resources within your account may be compromised. * Common ways to obtain environment variables: * Obtain the AccessKey ID of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID") * Obtain the AccessKey secret of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") */ AccessKeyId: tea.String("We recommend that you obtain the AccessKey ID of your RAM user from environment variables"), AccessKeySecret: tea.String("We recommend that you obtain the AccessKey secret of your RAM user from environment variables"), // Configure an HTTP proxy. // HttpProxy: tea.String("http://xx.xx.xx.xx:xxxx"), // Configure an HTTPS proxy. // HttpsProxy: tea.String("https://username:password@xxx.xxx.xxx.xxx:9999"), RegionId: tea.String("ap-southeast-1"), Endpoint: tea.String("green-cip.ap-southeast-1.aliyuncs.com"), /** * Set the connection timeout period and the read timeout period. The timeout period for the server to complete a voice moderation request is 10s. * If you set the read timeout to a period shorter than 10s, the server may generate a read timeout error during request processing. */ ConnectTimeout: tea.Int(3000), ReadTimeout: tea.Int(6000), } client, _err := green20220302.NewClient(config) if _err != nil { panic(_err) } // Create a RuntimeOptions instance and specify runtime parameters. 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_global"), 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) }
Sample code for obtaining video moderation results
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() { // If the project code is leaked, the AccessKey pair may be leaked and the security of resources within your account may be compromised. The following sample code is provided for reference only. We recommend that you use a securer STS method. config := &openapi.Config{ /** * The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M. * We recommend that you do not save the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, and the security of resources within your account may be compromised. * Common ways to obtain environment variables: * Obtain the AccessKey ID of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID") * Obtain the AccessKey secret of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") */ AccessKeyId: tea.String("We recommend that you obtain the AccessKey ID of your RAM user from environment variables"), AccessKeySecret: tea.String("We recommend that you obtain the AccessKey secret of your RAM user from environment variables"), // Configure an HTTP proxy. // HttpProxy: tea.String("http://xx.xx.xx.xx:xxxx"), // Configure an HTTPS proxy. // HttpsProxy: tea.String("https://username:password@xxx.xxx.xxx.xxx:9999"), RegionId: tea.String("ap-southeast-1"), Endpoint: tea.String("green-cip.ap-southeast-1.aliyuncs.com"), /** * Set the connection timeout period and the read timeout period. The timeout period for the server to complete a voice moderation request is 10s. * If you set the read timeout to a period shorter than 10s, the server may generate a read timeout error during request processing. */ ConnectTimeout: tea.Int(3000), ReadTimeout: tea.Int(6000), } client, _err := green20220302.NewClient(config) if _err != nil { panic(_err) } // Create a RuntimeOptions instance and specify runtime parameters. 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_global"), 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) }
Sample code for canceling a live stream moderation task
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() { // If the project code is leaked, the AccessKey pair may be leaked and the security of resources within your account may be compromised. The following sample code is provided for reference only. We recommend that you use a securer STS method. config := &openapi.Config{ /** * The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M. * We recommend that you do not save the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, and the security of resources within your account may be compromised. * Common ways to obtain environment variables: * Obtain the AccessKey ID of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID") * Obtain the AccessKey secret of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") */ AccessKeyId: tea.String("We recommend that you obtain the AccessKey ID of your RAM user from environment variables"), AccessKeySecret: tea.String("We recommend that you obtain the AccessKey secret of your RAM user from environment variables"), // Configure an HTTP proxy. // HttpProxy: tea.String("http://xx.xx.xx.xx:xxxx"), // Configure an HTTPS proxy. // HttpsProxy: tea.String("https://username:password@xxx.xxx.xxx.xxx:9999"), RegionId: tea.String("ap-southeast-1"), Endpoint: tea.String("green-cip.ap-southeast-1.aliyuncs.com"), /** * Set the connection timeout period and the read timeout period. The timeout period for the server to complete a voice moderation request is 10s. * If you set the read timeout to a period shorter than 10s, the server may generate a read timeout error during request processing. */ ConnectTimeout: tea.Int(3000), ReadTimeout: tea.Int(6000), } client, _err := green20220302.NewClient(config) if _err != nil { panic(_err) } // Create a RuntimeOptions instance and specify runtime parameters. 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_global"), 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) } }