All Products
Search
Document Center

Content Moderation:Video moderation

Last Updated:Aug 18, 2023

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

Description

Content Moderation SDK for .NET supports both synchronous and asynchronous video moderation.

  • 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 /green/video/syncscan.

  • (Recommended) If you use asynchronous video moderation, you can submit a video or a sequence of frames captured from the video for moderation. For more information about the related parameters, see /green/video/asyncscan and /green/video/results.

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 .NET version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.

(Recommended) Submit asynchronous video moderation tasks

Operation

Description

Supported region

VideoAsyncScanRequest

Sends asynchronous requests to moderate videos for risky content across 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

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)
            {
                /**
                 * Common ways to obtain environment variables:
                 *     Obtain the AccessKey ID of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
                 *     Obtain the AccessKey secret of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
                 */
                DefaultProfile profile = DefaultProfile.GetProfile(
                        "cn-shanghai",
                        "We recommend that you obtain the AccessKey ID of your RAM user from environment variables",
                        "We recommend that you obtain the AccessKey secret of your RAM user from environment variables");
                // Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
                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: The two parameters are optional and 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)
            {
                /**
                 * Common ways to obtain environment variables:
                 *     Obtain the AccessKey ID of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
                 *     Obtain the AccessKey secret of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
                 */
                DefaultProfile profile = DefaultProfile.GetProfile(
                        "cn-shanghai",
                        "We recommend that you obtain the AccessKey ID of your RAM user from environment variables",
                        "We recommend that you obtain the AccessKey secret of your RAM user from environment variables");
                // Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
                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: The two parameters are optional and 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)
            {
                /**
                 * Common ways to obtain environment variables:
                 *     Obtain the AccessKey ID of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
                 *     Obtain the AccessKey secret of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
                 */
                DefaultProfile profile = DefaultProfile.GetProfile(
                        "cn-shanghai",
                        "We recommend that you obtain the AccessKey ID of your RAM user from environment variables",
                        "We recommend that you obtain the AccessKey secret of your RAM user from environment variables");
                // Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
                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: The two parameters are optional and 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 the results of asynchronous video moderation

Operation

Description

Supported region

VideoAsyncScanResultsRequest

Queries the results of asynchronous video moderation.

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

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)
        {
            /**
             * Common ways to obtain environment variables:
             *     Obtain the AccessKey ID of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     Obtain the AccessKey secret of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "We recommend that you obtain the AccessKey ID of your RAM user from environment variables",
                    "We recommend that you obtain the AccessKey secret of your RAM user from environment variables");
            // Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
            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

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)
        {
            /**
             * Common ways to obtain environment variables:
             *     Obtain the AccessKey ID of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     Obtain the AccessKey secret of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "We recommend that you obtain the AccessKey ID of your RAM user from environment variables",
                    "We recommend that you obtain the AccessKey secret of your RAM user from environment variables");
            // Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
            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 the captured video frame 1 to be moderated");

            Dictionary<string, object> frame2 = new Dictionary<string, object>();
            frame2.Add("offset", "5");
            frame2.Add("url", "URL of the captured video frame 2 to be moderated");

            Dictionary<string, object> frame3 = new Dictionary<string, object>();
            frame3.Add("offset", "10");
            frame3.Add("url", "URL of the captured video frame 3 to be moderated");

            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);
            }
        }
    }
}

Provide feedback on moderation results

If the moderation results do not meet your expectations, 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 /green/video/feedback.

Operation

Description

Supported region

VideoFeedbackRequest

Provides feedback on video moderation results and modifies the machine-assisted moderation results based on the feedback.

  • cn-shanghai: China (Shanghai)

  • cn-beijing: China (Beijing)

  • cn-shenzhen: China (Shenzhen)

  • ap-southeast-1: 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)
        {
            /**
             * Common ways to obtain environment variables:
             *     Obtain the AccessKey ID of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     Obtain the AccessKey secret of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "We recommend that you obtain the AccessKey ID of your RAM user from environment variables",
                    "We recommend that you obtain the AccessKey secret of your RAM user from environment variables");
            // Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
            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 the captured video frame 1 that was moderated");

            Dictionary<string, object> frame2 = new Dictionary<string, object>();
            frame2.Add("offset", "0");
            frame2.Add("url", "URL of the captured video frame 2 that was moderated");

            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 results 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);
            }
        }
    }
}