This topic describes how to use Content Moderation SDK for Go to moderate videos for risky content.

Background information

Synchronous video moderation and asynchronous video moderation are supported.
  • If you use synchronous video moderation, you can submit only a sequence of frames captured from a video for moderation. For more information about the related parameters, see Synchronous moderation.
  • If you use asynchronous video moderation, you can submit a video or a sequence of frames captured from the video for moderation. We recommend that you use asynchronous video moderation. For more information about the related parameters, see Asynchronous moderation.

Prerequisites

Go dependencies are installed. For more information, see Installation.
Note You must use the required Go version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.

Submit asynchronous video moderation tasks (recommended)

Operation Description Supported region
VideoAsyncScanRequest Sends asynchronous requests to moderate videos for risky content in multiple moderation scenarios, including pornography, terrorist content, ad, undesirable scene, and logo detection.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore (Singapore)
Sample code
  • Submit the URL of an online video for video moderation
    package main
    
    import (
       "encoding/json"
       "fmt"
       "github.com/aliyun/alibaba-cloud-sdk-go/services/green"
       "strconv"
    )
    
    func main() {
       // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
       client, err := green.NewClientWithAccessKey("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret")
       if err != nil {
          fmt.Println(err.Error())
          return
       }
    
       task := map[string]interface{}{"dataId": "ID of the video to be moderated", "url": "URL of the video to be moderated"}
      // scenes: the moderation scenarios. You can specify one or more moderation scenarios. 
       // callback and seed: optional. The two parameters are used to configure a callback notification. 
       content, _ := json.Marshal(
          map[string]interface{}{
             "tasks": [...]map[string]interface{}{task}, "scenes": [...]string{"porn", "terrorism"},
             "bizType": "Business scenario", "callback": "Callback URL", "seed": "Random string",
          },
       )
    
       request := green.CreateVideoAsyncScanRequest()
       request.SetContent(content)
       response, _err := client.VideoAsyncScan(request)
       if _err != nil {
          fmt.Println(_err.Error())
          return
       }
       if response.GetHttpStatus() != 200 {
          fmt.Println("response not success. status:" + strconv.Itoa(response.GetHttpStatus()))
       }
       fmt.Println(response.GetHttpContentString())
    }
  • Submit a video live stream for video moderation
    package main
    
    import (
       "encoding/json"
       "fmt"
       "github.com/aliyun/alibaba-cloud-sdk-go/services/green"
       "strconv"
    )
    
    func main() {
       // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
       client, err := green.NewClientWithAccessKey("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret")
       if err != nil {
          fmt.Println(err.Error())
          return
       }
       // Set the url parameter to the URL of your live stream. 
       task := map[string]interface{}{"dataId": "ID of the video to be moderated", "url": "URL of the video to be moderated"}
       // scenes: the moderation scenarios. You can specify one or more moderation scenarios. 
       // callback and seed: optional. The two parameters are used to configure a callback notification. 
       content, _ := json.Marshal(
          map[string]interface{}{
             "tasks": [...]map[string]interface{}{task}, "scenes": [...]string{"porn", "terrorism"},
             "live": "true", "bizType": "Business scenario", "callback": "Callback URL", "seed": "Random string",
          },
       )
    
       request := green.CreateVideoAsyncScanRequest()
       request.SetContent(content)
       response, _err := client.VideoAsyncScan(request)
       if _err != nil {
          fmt.Println(_err.Error())
          return
       }
       if response.GetHttpStatus() != 200 {
          fmt.Println("response not success. status:" + strconv.Itoa(response.GetHttpStatus()))
       }
       fmt.Println(response.GetHttpContentString())
    }
  • Submit a video live stream to moderate both the video images and audio
    package main
    
    import (
       "encoding/json"
       "fmt"
       "github.com/aliyun/alibaba-cloud-sdk-go/services/green"
       "strconv"
    )
    
    func main() {
       // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
       client, err := green.NewClientWithAccessKey("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret")
       if err != nil {
          fmt.Println(err.Error())
          return
       }
       // Set the url parameter to the URL of your live stream. 
       task := map[string]interface{}{"dataId": "ID of the video to be moderated", "url": "URL of the video to be moderated"}
       // scenes: the moderation scenarios. You can specify one or more moderation scenarios. 
       // callback and seed: optional. The two parameters are used to configure a callback notification. 
       content, _ := json.Marshal(
          map[string]interface{}{
             "tasks": [...]map[string]interface{}{task}, "scenes": [...]string{"porn", "terrorism"},
             "live": "true", "audioScenes": [...]string{"antispam"}, "bizType": "Business scenario",
             "callback": "Callback URL",
             "seed": "Random string",
          },
       )
    
       request := green.CreateVideoAsyncScanRequest()
       request.SetContent(content)
       response, _err := client.VideoAsyncScan(request)
       if _err != nil {
          fmt.Println(_err.Error())
          return
       }
       if response.GetHttpStatus() != 200 {
          fmt.Println("response not success. status:" + strconv.Itoa(response.GetHttpStatus()))
       }
       fmt.Println(response.GetHttpContentString())
    }

Query the results of asynchronous video moderation

Operation Description Supported region
VideoAsyncScanResultsRequest Queries asynchronous video moderation results.
Note Instead of calling this operation to poll the moderation results, we recommend that you set the callback parameter when you submit asynchronous video moderation tasks to receive the moderation results.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore (Singapore)
Sample code
package main

import (
   "encoding/json"
   "fmt"
   "github.com/aliyun/alibaba-cloud-sdk-go/services/green"
   "strconv"
)

func main() {
   // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
   client, err := green.NewClientWithAccessKey("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret")
   if err != nil {
      fmt.Println(err.Error())
      return
   }

   content, _ := json.Marshal(
      [...]string{"ID of the asynchronous video moderation task"},
   )

   request := green.CreateVideoAsyncScanResultsRequest()
   request.SetContent(content)
   response, _err := client.VideoAsyncScanResults(request)
   if _err != nil {
      fmt.Println(_err.Error())
      return
   }
   if response.GetHttpStatus() != 200 {
      fmt.Println("response not success. status:" + strconv.Itoa(response.GetHttpStatus()))
   }
   fmt.Println(response.GetHttpContentString())
}

Submit synchronous video moderation tasks

Operation Description Supported region
VideoSyncScanRequest Sends synchronous requests to moderate videos for risky content.
Note You can submit only a sequence of frames that are captured from a video for video moderation. To submit other types of videos, we recommend that you use the VideoAsyncScanRequest operation.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore (Singapore)

Sample code

In this example, a sequence of frames that are captured from a video is to be moderated.

package main

import (
   "encoding/json"
   "fmt"
   "github.com/aliyun/alibaba-cloud-sdk-go/services/green"
   "strconv"
)

func main() {
   // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
   client, err := green.NewClientWithAccessKey("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret")
   if err != nil {
      fmt.Println(err.Error())
      return
   }

   frame1 := map[string]interface{}{
      "offset": "0", "url": "URL of the captured video frame 1 to be moderated",
   }
   frame2 := map[string]interface{}{
      "offset": "5", "url": "URL of the captured video frame 2 to be moderated",
   }
   frame3 := map[string]interface{}{
      "offset": "10", "url": "URL of the captured video frame 3 to be moderated",
   }
// frames: the captured video frames to be moderated. 
   task := map[string]interface{}{"dataId": "ID of the video to be moderated", "frames": [...]map[string]interface{}{frame1, frame2, frame3}}
   // scenes: the moderation scenarios. You can specify one or more moderation scenarios. 
   content, _ := json.Marshal(
      map[string]interface{}{
         "tasks": [...]map[string]interface{}{task}, "scenes": [...]string{"porn", "terrorism"},
         "bizType": "Business scenario",
      },
   )

   request := green.CreateVideoSyncScanRequest()
   request.SetContent(content)
   response, _err := client.VideoSyncScan(request)
   if _err != nil {
      fmt.Println(_err.Error())
      return
   }
   if response.GetHttpStatus() != 200 {
      fmt.Println("response not success. status:" + strconv.Itoa(response.GetHttpStatus()))
   }
   fmt.Println(response.GetHttpContentString())
}

Provide feedback on moderation results

If the moderation results are not as expected, you can call the VideoFeedbackRequest operation to modify the results. Content Moderation adds the moderated video frames to the similar image blacklist or whitelist based on your feedback. When you submit a similar video for moderation, Content Moderation returns moderation results based on the label in your feedback.

For more information, see Give feedback on moderation results.

Operation Description Supported region
VideoFeedbackRequest Provides feedback on a video moderation result and modifies the machine-assisted moderation result based on the feedback.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore (Singapore)
Sample code
package main

import (
   "encoding/json"
   "fmt"
   "github.com/aliyun/alibaba-cloud-sdk-go/services/green"
   "strconv"
)

func main() {
   // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
   client, _err := green.NewClientWithAccessKey("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret")
   if _err != nil {
      fmt.Println(_err.Error())
      return
   }

   frame1 := map[string]interface{}{"url": "URL of the captured and moderated video frame 1", "offset": "Offset of the captured and moderated video frame 1"}
   frame2 := map[string]interface{}{"url": "URL of the captured and moderated video frame 2", "offset": "Offset of the captured and moderated video frame 2"}
   frames := [...]map[string]interface{}{frame1, frame2}

   // scenes: the moderation scenarios. You can specify one or more moderation scenarios. 
   // suggestion: the moderation result that you expect to return. A value of pass indicates that the moderated video is normal. A value of block indicates that the moderated video contains violations. 
   content, _ := json.Marshal(
      map[string]interface{}{
         "taskId": "Video moderation task ID", "dataId": "Moderated video ID", "url": "Moderated video URL", "frames": frames,
         "suggestion": "block", "scenes": [...]string{"ad", "terrorism"}, "note": "Remarks",
      },
   )

   request := green.CreateVideoFeedbackRequest()
   request.SetContent(content)
   response, err := client.VideoFeedback(request)
   if err != nil {
      fmt.Println(err.Error())
      return
   }
   if response.GetHttpStatus() != 200 {
      fmt.Println("response not success. status:" + strconv.Itoa(response.GetHttpStatus()))
   }
   fmt.Println(response.GetHttpContentString())
}