Video moderation

更新时间:
复制 MD 格式

Use the AI Guardrails SDK for .NET to scan videos for risky content, including explicit content, terrorism, ads, undesirable scenes, and logos.

Two detection modes are available: asynchronous (recommended) and synchronous.

ModeInputUse when
Asynchronous (recommended)Video URL or frame sequenceModerating full videos or live streams
SynchronousFrame sequence onlyModerating pre-extracted video frames
The SDK accepts only public video URLs. Local files and binary data are not supported. HTTP and HTTPS URLs up to 2,048 characters in length are supported.

Prerequisites

Before you begin, ensure that you have:

  • Installed the .NET dependencies. See Install .NET dependencies. Use the .NET version specified in that topic — otherwise, subsequent API calls will fail.

  • A RAM user AccessKey ID and AccessKey secret stored as environment variables:

    • ALIBABA_CLOUD_ACCESS_KEY_ID

    • ALIBABA_CLOUD_ACCESS_KEY_SECRET

Submit asynchronous video moderation tasks

VideoAsyncScanRequest sends an asynchronous request to moderate a video across one or more moderation scenarios, such as porn, terrorism, ad, undesirable scene, and logo.

Supported regions: cn-shanghai, cn-beijing, cn-shenzhen, ap-southeast-1

Set the scenes parameter to specify which moderation scenarios to apply. Configure callback and seed to receive results via webhook instead of polling.

Submit a video URL for 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 get environment variables:
             *     Get the AccessKey ID of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     Get the AccessKey secret of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "Get the AccessKey ID of the RAM user from an environment variable",
                    "Get the AccessKey secret of the RAM user from an environment variable");
            // Note: Reuse the instantiated client as much as possible to improve detection performance and avoid repeated 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", "Data ID for detection");
            task1.Add("url", "URL of the video to be detected");

            // scenes: The detection scenarios. You can specify multiple scenarios.
            // callback and seed are optional parameters used for callback notifications.
            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            httpBody.Add("scenes", new List<string> { "porn", "terrorism" });
            httpBody.Add("bizType", "Business scenario");
            httpBody.Add("callback", "Webhook address");
            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 live stream for moderation

Set live to "true" and provide the streaming URL in url.

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 get environment variables:
             *     Get the AccessKey ID of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     Get the AccessKey secret of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "Get the AccessKey ID of the RAM user from an environment variable",
                    "Get the AccessKey secret of the RAM user from an environment variable");
            // Note: Reuse the instantiated client as much as possible to improve detection performance and avoid repeated 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", "Data ID for detection");
            // Set url to the streaming URL.
            task1.Add("url", "URL of the video to be detected");

            // scenes: The detection scenarios. You can specify multiple scenarios.
            // callback and seed are optional parameters used for callback notifications.
            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", "Webhook address");
            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 live stream for combined video and audio moderation

Add audioScenes to the request body to moderate both video and audio content in the same task.

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 get environment variables:
             *     Get the AccessKey ID of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     Get the AccessKey secret of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "Get the AccessKey ID of the RAM user from an environment variable",
                    "Get the AccessKey secret of the RAM user from an environment variable");
            // Note: Reuse the instantiated client as much as possible to improve detection performance and avoid repeated 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", "Data ID for detection");
            // Set url to the streaming URL.
            task1.Add("url", "URL of the video to be detected");

            // scenes: The detection scenarios. You can specify multiple scenarios.
            // callback and seed are optional parameters used for callback notifications.
            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", "Webhook address");
            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 moderation results

VideoAsyncScanResultsRequest retrieves the results of previously submitted asynchronous tasks by task ID.

Supported regions: cn-shanghai, cn-beijing, cn-shenzhen, ap-southeast-1

Set the callback parameter when submitting tasks to receive results via webhook — this avoids polling.

For API parameter details, see /green/video/asyncscan and /green/video/results.

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 get environment variables:
             *     Get the AccessKey ID of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     Get the AccessKey secret of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "Get the AccessKey ID of the RAM user from an environment variable",
                    "Get the AccessKey secret of the RAM user from an environment variable");
            // Note: Reuse the instantiated client as much as possible to improve detection performance and avoid repeated 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> { "Asynchronous video detection task ID" };

            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 frame moderation tasks

VideoSyncScanRequest scans a sequence of video frames synchronously. Submit a list of frame objects, each with an offset and a url pointing to the frame image.

Supported regions: cn-shanghai, cn-beijing, cn-shenzhen, ap-southeast-1

Synchronous moderation accepts only pre-extracted frame sequences, not full video URLs. For full video files and live streams, use VideoAsyncScanRequest.

For API parameter details, see /green/video/syncscan.

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 get environment variables:
             *     Get the AccessKey ID of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     Get the AccessKey secret of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "Get the AccessKey ID of the RAM user from an environment variable",
                    "Get the AccessKey secret of the RAM user from an environment variable");
            // Note: Reuse the instantiated client as much as possible to improve detection performance and avoid repeated 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 your video snapshot 1");

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

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

            Dictionary<string, object> task1 = new Dictionary<string, object>();
            task1.Add("dataId", "Data ID for detection");
            task1.Add("frames", new List<Dictionary<string, object>> { frame1, frame2, frame3 });

            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            // scenes: The detection scenarios. You can specify multiple 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);
            }
        }
    }
}

Submit moderation result feedback

If a moderation result is incorrect, use VideoFeedbackRequest to submit a correction. AI Guardrails adds the video frames to the similar image blacklist or whitelist based on your feedback and applies the corrected label to similar frames in future scans.

Supported regions: cn-shanghai, cn-beijing, cn-shenzhen, ap-southeast-1

For details, see Detection result feedback.

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 get environment variables:
             *     Get the AccessKey ID of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     Get the AccessKey secret of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "Get the AccessKey ID of the RAM user from an environment variable",
                    "Get the AccessKey secret of the RAM user from an environment variable");
            // Note: Reuse the instantiated client as much as possible to improve detection performance and avoid repeated 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 your video snapshot 1");

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

            List<Dictionary<string, object>> frames = new List<Dictionary<string, object>> { frame1, frame2 };

            // scenes: The detection scenarios. You can specify multiple scenarios.
            // suggestion: The expected detection result. pass: normal. block: violation.
            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            httpBody.Add("scenes", new List<string> { "porn", "terrorism" });
            httpBody.Add("suggestion", "block");
            httpBody.Add("taskId", "Video moderation task ID");
            httpBody.Add("dataId", "Data ID for detection");
            httpBody.Add("url", "Video URL");
            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);
            }
        }
    }
}