This topic describes how to use Content Moderation SDK for .NET 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 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 parameters, see Asynchronous moderation.

Prerequisites

The dependencies for Content Moderation SDK for .NET are installed. For more information about how to install the dependencies, see Installation.
Note You must use the required versions of the dependencies described in the Installation topic. 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
    using System;
    using Newtonsoft.Json;
    using Aliyun.Acs.Core;
    using Aliyun.Acs.Core.Http;
    using Aliyun.Acs.Core.Profile;
    using Aliyun.Acs.Green.Model.V20180509;
    using System.Collections.Generic;
    
    namespace csharp_sdk_sample
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
                IClientProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "Your AccessKey ID",
                    "Your AccessKey secret");
                DefaultAcsClient client = new DefaultAcsClient(profile);
    
                VideoAsyncScanRequest request = new VideoAsyncScanRequest();
                request.AcceptFormat = FormatType.JSON;
                request.ContentType = FormatType.JSON;
                request.Method = MethodType.POST;
                request.Encoding = "UTF-8";
    
                Dictionary<string, object> task1 = new Dictionary<string, object>();
                task1.Add("dataId", "ID of the video to be moderated");
                task1.Add("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. 
                Dictionary<string, object> httpBody = new Dictionary<string, object>();
                httpBody.Add("scenes", new List<string> { "porn", "terrorism" });
                httpBody.Add("bizType", "Business scenario");
                httpBody.Add("callback", "Callback URL");
                httpBody.Add("seed", "Random string");
                httpBody.Add("tasks", new List<Dictionary<string, object>> { task1 });
    
                request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON);
                try
                {
                    VideoAsyncScanResponse response = client.GetAcsResponse(request);
                    if (response.HttpResponse.Status != 200)
                    {
                        Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status);
                    }
                    Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed with error info: {0}", ex.Message);
                }
            }
        }
    }
  • Submit a video live stream for video moderation
    using System;
    using Newtonsoft.Json;
    using Aliyun.Acs.Core;
    using Aliyun.Acs.Core.Http;
    using Aliyun.Acs.Core.Profile;
    using Aliyun.Acs.Green.Model.V20180509;
    using System.Collections.Generic;
    
    namespace csharp_sdk_sample
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
                IClientProfile profile = DefaultProfile.GetProfile(
                     "cn-shanghai",
                     "Your AccessKey ID",
                     "Your AccessKey secret");
                DefaultAcsClient client = new DefaultAcsClient(profile);
    
                VideoAsyncScanRequest request = new VideoAsyncScanRequest();
                request.AcceptFormat = FormatType.JSON;
                request.ContentType = FormatType.JSON;
                request.Method = MethodType.POST;
                request.Encoding = "UTF-8";
    
                Dictionary<string, object> task1 = new Dictionary<string, object>();
                task1.Add("dataId", "ID of the video live stream to be moderated");
                // Set the url parameter to the URL of your video live stream. 
                task1.Add("url", "URL of the video live stream 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. 
                Dictionary<string, object> httpBody = new Dictionary<string, object>();
                httpBody.Add("scenes", new List<string> { "porn", "terrorism" });
                httpBody.Add("live", "true");
                httpBody.Add("bizType", "Business scenario");
                httpBody.Add("callback", "Callback URL");
                httpBody.Add("seed", "Random string");
                httpBody.Add("tasks", new List<Dictionary<string, object>> { task1 });
    
                request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON);
                try
                {
                    VideoAsyncScanResponse response = client.GetAcsResponse(request);
                    if (response.HttpResponse.Status != 200)
                    {
                        Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status);
                    }
                    Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed with error info: {0}", ex.Message);
                }
            }
        }
    }
  • Submit a video live stream to moderate both the video images and the audio
    using System;
    using Newtonsoft.Json;
    using Aliyun.Acs.Core;
    using Aliyun.Acs.Core.Http;
    using Aliyun.Acs.Core.Profile;
    using Aliyun.Acs.Green.Model.V20180509;
    using System.Collections.Generic;
    
    namespace csharp_sdk_sample
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
                IClientProfile profile = DefaultProfile.GetProfile(
                     "cn-shanghai",
                     "Your AccessKey ID",
                     "Your AccessKey secret");
                DefaultAcsClient client = new DefaultAcsClient(profile);
    
                VideoAsyncScanRequest request = new VideoAsyncScanRequest();
                request.AcceptFormat = FormatType.JSON;
                request.ContentType = FormatType.JSON;
                request.Method = MethodType.POST;
                request.Encoding = "UTF-8";
    
                Dictionary<string, object> task1 = new Dictionary<string, object>();
                task1.Add("dataId", "ID of the video live stream to be moderated");
                // Set the url parameter to the URL of your video live stream. 
                task1.Add("url", "URL of the video live stream 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. 
                Dictionary<string, object> httpBody = new Dictionary<string, object>();
                httpBody.Add("scenes", new List<string> { "porn", "terrorism" });
                httpBody.Add("live", "true");
                httpBody.Add("audioScenes", new List<string> { "antispam" });
                httpBody.Add("bizType", "Business scenario");
                httpBody.Add("callback", "Callback URL");
                httpBody.Add("seed", "Random string");
                httpBody.Add("tasks", new List<Dictionary<string, object>> { task1 });
    
                request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON);
                try
                {
                    VideoAsyncScanResponse response = client.GetAcsResponse(request);
                    if (response.HttpResponse.Status != 200)
                    {
                        Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status);
                    }
                    Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed with error info: {0}", ex.Message);
                }
            }
        }
    }

Query asynchronous video moderation results

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
using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;

namespace csharp_sdk_sample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
            IClientProfile profile = DefaultProfile.GetProfile(
                "cn-shanghai",
                "Your AccessKey ID",
                "Your AccessKey secret");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            VideoAsyncScanResultsRequest request = new VideoAsyncScanResultsRequest();
            request.AcceptFormat = FormatType.JSON;
            request.ContentType = FormatType.JSON;
            request.Method = MethodType.POST;
            request.Encoding = "UTF-8";

            List<string> taskIdList=new List<string> { "ID of the asynchronous video moderation task"};

            request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(taskIdList)), "utf-8", FormatType.JSON);
            try
            {
                VideoAsyncScanResultsResponse response = client.GetAcsResponse(request);


                if (response.HttpResponse.Status != 200)
                {
                    Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status);
                }
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}

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 are to be moderated.

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;

namespace csharp_sdk_sample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
            IClientProfile profile = DefaultProfile.GetProfile(
                "cn-shanghai",
                "Your AccessKey ID",
                "Your AccessKey secret");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            VideoSyncScanRequest request = new VideoSyncScanRequest();
            request.AcceptFormat = FormatType.JSON;
            request.ContentType = FormatType.JSON;
            request.Method = MethodType.POST;
            request.Encoding = "UTF-8";

            Dictionary<string, object> frame1 = new Dictionary<string, object>();
            frame1.Add("offset", "0");
            frame1.Add("url", "URL of Video Frame 1");

            Dictionary<string, object> frame2 = new Dictionary<string, object>();
            frame2.Add("offset", "5");
            frame2.Add("url", "URL of Video Frame 2");

            Dictionary<string, object> frame3 = new Dictionary<string, object>();
            frame3.Add("offset", "10");
            frame3.Add("url", "URL of Video Frame 3");

            Dictionary<string, object> task1 = new Dictionary<string, object>();
            task1.Add("dataId", "ID of the video to be moderated");
            task1.Add("frames", new List<Dictionary<string, object>> { frame1, frame2, frame3 });

            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            // scenes: the moderation scenarios. You can specify one or more moderation scenarios. 
            httpBody.Add("scenes", new List<string> { "porn", "terrorism" });
            httpBody.Add("bizType", "Business scenario");
            httpBody.Add("tasks", new List<Dictionary<string, object>> { task1 });

            request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON);
            try
            {
                VideoSyncScanResponse response = client.GetAcsResponse(request);
                if (response.HttpResponse.Status != 200)
                {
                    Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status);
                }
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}

Give 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 frame 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
using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;

namespace csharp_sdk_sample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
            IClientProfile profile = DefaultProfile.GetProfile(
                "cn-shanghai",
                "Your AccessKey ID",
                "Your AccessKey secret");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            VideoFeedbackRequest request = new VideoFeedbackRequest();
            request.AcceptFormat = FormatType.JSON;
            request.ContentType = FormatType.JSON;
            request.Method = MethodType.POST;
            request.Encoding = "UTF-8";

            Dictionary<string, object> frame1 = new Dictionary<string, object>();
            frame1.Add("offset", "0");
            frame1.Add("url", "URL of Video Frame 1");

            Dictionary<string, object> frame2 = new Dictionary<string, object>();
            frame2.Add("offset", "0");
            frame2.Add("url", "URL of Video Frame 2");

            List<Dictionary<string, object>> frames = new List<Dictionary<string, object>> { 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 frame is normal. A value of block indicates that the moderated video frame contains violations. 
            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            httpBody.Add("scenes", new List<string> { "porn", "terrorism" });
            httpBody.Add("suggestion", "block");
            httpBody.Add("taskId", "ID of the video moderation task");
            httpBody.Add("dataId", "ID of the moderated video");
            httpBody.Add("url", "URL of the moderated video");
            httpBody.Add("frames", frames);
            httpBody.Add("note", "Remarks");

            request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON);
            try
            {
                VideoFeedbackResponse response = client.GetAcsResponse(request);


                if (response.HttpResponse.Status != 200)
                {
                    Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status);
                }
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}