使用 .NET 创建视频脚本评论Create video transcript reviews using .NET

本文提供了信息和代码示例,帮助你快速开始结合使用内容审查器 SDK 和 C# 来执行以下操作:This article provides information and code samples to help you quickly get started using the Content Moderator SDK with C# to:

  • 为人工审查器创建视频评论Create a video review for human moderators
  • 向评论中添加已审查的脚本Add a moderated transcript to the review
  • 发布评论Publish the review

先决条件Prerequisites

  • 如果尚未登录,请在内容审查器审阅工具站点上登录或创建帐户。Sign in or create an account on the Content Moderator Review tool site if you haven't done so already.
  • 本文假定你已审查视频,并在评论工具中创建了视频评论以便进行人工决策。This article assumes that you have moderated the video and created the video review in the review tool for human decision making. 你现在想要在评论工具中添加已审查的视频脚本。You now want to add moderated video transcripts in the review tool.

确保 API 密钥可以调用评审 API(创建作业)Ensure your API key can call the review API (Job creation)

完成上述步骤后,如果从 Azure 门户着手,最终可能会得到两个内容审查器密钥。After completing the previous steps, you may end up with two Content Moderator keys if you started from the Azure portal.

如果计划在 SDK 示例中使用 Azure 提供的 API 密钥,请按照将 Azure 密钥与评审 API 配合使用部分中提到的步骤操作,以允许应用程序调用评审 API 并创建评审。If you plan to use the Azure-provided API key in your SDK sample, follow the steps mentioned in the Using Azure key with the review API section to allow your application to call the review API and create reviews.

如果使用评审工具生成的免费试用密钥,则评审工具帐户已经知道密钥,因此无需其他步骤。If you use the free trial key generated by the review tool, your review tool account already knows about the key and therefore, no additional steps are required.

准备视频以供评论Prepare your video for review

将脚本添加到视频评论中。Add the transcript to a video review. 视频必须在线发布。The video must be published online. 你需要它的流式处理终结点。You need its streaming endpoint. 流式处理终结点允许评论工具视频播放器播放视频。The streaming endpoint allows the review tool video player to play the video.

视频演示缩略图

创建 Visual Studio 项目Create your Visual Studio project

  1. 向解决方案添加新的“控制台应用(.NET Framework)”**** 项目。Add a new Console app (.NET Framework) project to your solution.

  2. 将项目命名为“VideoTranscriptReviews”****。Name the project VideoTranscriptReviews.

  3. 将此项目选为解决方案的单一启动项目。Select this project as the single startup project for the solution.

安装所需程序包Install required packages

为 TermLists 项目安装以下 NuGet 包。Install the following NuGet packages for the TermLists project.

  • Microsoft.Azure.CognitiveServices.ContentModeratorMicrosoft.Azure.CognitiveServices.ContentModerator
  • Microsoft.Rest.ClientRuntimeMicrosoft.Rest.ClientRuntime
  • Microsoft.Rest.ClientRuntime.AzureMicrosoft.Rest.ClientRuntime.Azure
  • Newtonsoft.JsonNewtonsoft.Json

更新程序的 using 语句Update the program's using statements

按下面所示修改程序的 using 语句。Modify the program's using statements as follows.

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Microsoft.Azure.CognitiveServices.ContentModerator;
using Microsoft.Azure.CognitiveServices.ContentModerator.Models;
using Newtonsoft.Json;

添加私有属性Add private properties

将以下专用属性添加到 VideoTranscriptReviews**** 命名空间中的 Program**** 类。Add the following private properties to namespace VideoTranscriptReviews, class Program. 使用终结点 URL 和订阅密钥的值更新 AzureEndpointCMSubscriptionKey 字段。Update the AzureEndpoint and CMSubscriptionKey fields with the values of your endpoint URL and subscription key. 可在 Azure 门户中资源的“快速启动”**** 选项卡中找到它们。You can find these in the Quick start tab of your resource in the Azure portal.

namespace VideoReviews
{
    class Program
    {
        // NOTE: Enter a valid endpoint URL
        /// <summary>
        /// The endpoint URL of your subscription
        /// </summary>
        private static readonly string AzureEndpoint = "YOUR ENDPOINT URL";

        // NOTE: Enter a valid subscription key.
        /// <summary>
        /// Your Content Moderator subscription key.
        /// </summary>
        private static readonly string CMSubscriptionKey = "YOUR CONTENT MODERATOR KEY";

        // NOTE: Replace this example team name with your Content Moderator team name.
        /// <summary>
        /// The name of the team to assign the job to.
        /// </summary>
        /// <remarks>This must be the team name you used to create your 
        /// Content Moderator account. You can retrieve your team name from
        /// the Content Moderator web site. Your team name is the Id associated 
        /// with your subscription.</remarks>
        private const string TeamName = "YOUR CONTENT MODERATOR TEAM ID";

        /// <summary>
        /// The minimum amount of time, in milliseconds, to wait between calls
        /// to the Content Moderator APIs.
        /// </summary>
        private const int throttleRate = 2000;

创建内容审查器客户端对象Create Content Moderator Client object

将以下方法定义添加到 VideoTranscriptReviews 命名空间中的 Program 类。Add the following method definition to namespace VideoTranscriptReviews, class Program.

/// <summary>
/// Returns a new Content Moderator client for your subscription.
/// </summary>
/// <returns>The new client.</returns>
/// <remarks>The <see cref="ContentModeratorClient"/> is disposable.
/// When you have finished using the client,
/// you should dispose of it either directly or indirectly. </remarks>
public static ContentModeratorClient NewClient()
{
    return new ContentModeratorClient(new ApiKeyServiceClientCredentials(CMSubscriptionKey))
    {
        Endpoint = AzureEndpoint
    };
}

创建视频评论Create a video review

使用 ContentModeratorClient.Reviews.CreateVideoReviews 创建视频评论。Create a video review with ContentModeratorClient.Reviews.CreateVideoReviews. 有关详细信息,请参阅 API 参考For more information, see the API reference.

CreateVideoReviews 具有以下必需参数:CreateVideoReviews has the following required parameters:

  1. 一个字符串,包含应为“application/json”的 MIME 类型。A string that contains a MIME type, which should be "application/json."
  2. 内容审查器团队名称。Your Content Moderator team name.
  3. 一个 IList<CreateVideoReviewsBodyItem> 对象。An IList<CreateVideoReviewsBodyItem> object. 每个 CreateVideoReviewsBodyItem **** 对象表示一次视频审查。Each CreateVideoReviewsBodyItem object represents a video review. 本快速入门一次创建一条评论。This quickstart creates one review at a time.

CreateVideoReviewsBodyItem 具有多个属性。CreateVideoReviewsBodyItem has several properties. 至少应设置以下属性:At a minimum, you set the following properties:

  • Content****。Content. 要评论的视频的 URL。The URL of the video to be reviewed.
  • ContentId****。ContentId. 要分配给视频评论的 ID。An ID to assign to the video review.
  • Status****。Status. 将该值设置为“未发布”。Set the value to "Unpublished." 如果未进行设置,则默认为“挂起”,这意味着视频评论已发布并且正在等待人工评论。If you do not set it, it defaults to "Pending", which means the video review is published and pending human review. 视频评论发布后,就无法再向其中添加视频帧、脚本或脚本审查结果。Once a video review is published, you can no longer add video frames, a transcript, or a transcript moderation result to it.

备注

CreateVideoReviews 返回 IList<string>。CreateVideoReviews returns an IList<string>. 这些字符串中的每一个都包含视频评论 ID。Each of these strings contains an ID for a video review. 这些 ID 是 GUID,与 ContentId 属性的值不同。These IDs are GUIDs and are not the same as the value of the ContentId property.

将以下方法定义添加到 VideoReviews 命名空间中的 Program 类。Add the following method definition to namespace VideoReviews, class Program.

/// <summary>
/// Create a video review. For more information, see the API reference:
/// https://dev.cognitive.azure.cn/docs/services/580519463f9b070e5c591178/operations/580519483f9b0709fc47f9c4 
/// </summary>
/// <param name="client">The Content Moderator client.</param>
/// <param name="id">The ID to assign to the video review.</param>
/// <param name="content">The URL of the video to review.</param>
/// <returns>The ID of the video review.</returns>
private static string CreateReview(ContentModeratorClient client, string id, string content)
{
    Console.WriteLine("Creating a video review.");

    List<CreateVideoReviewsBodyItem> body = new List<CreateVideoReviewsBodyItem>() {
        new CreateVideoReviewsBodyItem
        {
            Content = content,
            ContentId = id,
            /* Note: to create a published review, set the Status to "Pending".
            However, you cannot add video frames or a transcript to a published review. */
            Status = "Unpublished",
        }
    };

    var result = client.Reviews.CreateVideoReviews("application/json", TeamName, body);

    Thread.Sleep(throttleRate);

    // We created only one review.
    return result[0];
}

备注

内容审查器服务密钥有一个每秒请求数 (RPS) 速率限制。Your Content Moderator service key has a requests per second (RPS) rate limit. 如果超过该限制,SDK 会引发错误代码为 429 的异常。If you exceed the limit, the SDK throws an exception with a 429 error code.

免费层密钥有一个 RPS 速率限制。A free tier key has a one RPS rate limit.

将脚本添加到视频评论中Add transcript to video review

可以使用 ContentModeratorClient.Reviews.AddVideoTranscript 将脚本添加到视频评论中。You add a transcript to a video review with ContentModeratorClient.Reviews.AddVideoTranscript. AddVideoTranscript 具有以下必需参数:AddVideoTranscript has the following required parameters:

  1. 内容审查器团队 ID。Your Content Moderator team ID.
  2. CreateVideoReviews 返回的视频评论 ID。The video review ID returned by CreateVideoReviews.
  3. 包含脚本的 Stream 对象。A Stream object that contains the transcript.

脚本必须采用 WebVTT 格式。The transcript must be in the WebVTT format. 有关更多信息,请参阅 WebVTT:Web 视频文本轨道格式For more information, see WebVTT: The Web Video Text Tracks Format.

备注

该程序使用 VTT 格式的示例脚本。The program uses a sample transcript in the VTT format. 在实际解决方案中,可使用 Azure Media Indexer 服务根据视频生成脚本In a real-world solution, you use the Azure Media Indexer service to generate a transcript from a video.

将以下方法定义添加到 VideotranscriptReviews 命名空间中的 Program 类。Add the following method definition to namespace VideotranscriptReviews, class Program.

/// <summary>
/// Add a transcript to the indicated video review.
/// The transcript must be in the WebVTT format.
/// For more information, see the API reference:
/// https://dev.cognitive.azure.cn/docs/services/580519463f9b070e5c591178/operations/59e7b8b2e7151f0b10d451fe
/// </summary>
/// <param name="client">The Content Moderator client.</param>
/// <param name="review_id">The video review ID.</param>
/// <param name="transcript">The video transcript.</param>
static void AddTranscript(ContentModeratorClient client, string review_id, string transcript)
{
    Console.WriteLine("Adding a transcript to the review with ID {0}.", review_id);
    client.Reviews.AddVideoTranscript(TeamName, review_id, new MemoryStream(System.Text.Encoding.UTF8.GetBytes(transcript)));
    Thread.Sleep(throttleRate);
}

将脚本审查结果添加到视频评论中Add a transcript moderation result to video review

除了向视频评论中添加脚本外,还可以添加该脚本的审查结果。In addition to adding a transcript to a video review, you also add the result of moderating that transcript. 可使用 ContentModeratorClient.Reviews.AddVideoTranscriptModerationResult 执行此操作。You do so with ContentModeratorClient.Reviews.AddVideoTranscriptModerationResult. 有关详细信息,请参阅 API 参考For more information, see the API reference.

AddVideoTranscriptModerationResult 具有以下必需参数:AddVideoTranscriptModerationResult has the following required parameters:

  1. 一个字符串,包含应为“application/json”的 MIME 类型。A string that contains a MIME type, which should be "application/json."
  2. 内容审查器团队名称。Your Content Moderator team name.
  3. CreateVideoReviews 返回的视频评论 ID。The video review ID returned by CreateVideoReviews.
  4. 一个 IList<TranscriptModerationBodyItem>。An IList<TranscriptModerationBodyItem>. TranscriptModerationBodyItem 具有以下属性:A TranscriptModerationBodyItem has the following properties:
  5. TermsTerms. 一个 IList<TranscriptModerationBodyItemTermsItem>。An IList<TranscriptModerationBodyItemTermsItem>. TranscriptModerationBodyItemTermsItem 具有以下属性:A TranscriptModerationBodyItemTermsItem has the following properties:
  6. IndexIndex. 该词语从零开始的索引。The zero-based index of the term.
  7. TermTerm. 一个包含该词语的字符串。A string that contains the term.
  8. TimestampTimestamp. 一个字符串,包含在脚本中找到词语的时间(以秒为单位)。A string that contains, in seconds, the time in the transcript at which the terms are found.

脚本必须采用 WebVTT 格式。The transcript must be in the WebVTT format. 有关更多信息,请参阅 WebVTT:Web 视频文本轨道格式For more information, see WebVTT: The Web Video Text Tracks Format.

将以下方法定义添加到 VideoTranscriptReviews 命名空间中的 Program 类。Add the following method definition to namespace VideoTranscriptReviews, class Program. 此方法将脚本提交到 ContentModeratorClient.TextModeration.ScreenText 方法。This method submits a transcript to the ContentModeratorClient.TextModeration.ScreenText method. 它还将结果转换为 IList<TranscriptModerationBodyItem>,并提交到 AddVideoTranscriptModerationResultIt also translates the result into an IList<TranscriptModerationBodyItem>, and submits to AddVideoTranscriptModerationResult.

/// <summary>
/// Add the results of moderating a video transcript to the indicated video review.
/// For more information, see the API reference:
/// https://dev.cognitive.azure.cn/docs/services/580519463f9b070e5c591178/operations/59e7b93ce7151f0b10d451ff
/// </summary>
/// <param name="client">The Content Moderator client.</param>
/// <param name="review_id">The video review ID.</param>
/// <param name="transcript">The video transcript.</param>
static void AddTranscriptModerationResult(ContentModeratorClient client, string review_id, string transcript)
{
    Console.WriteLine("Adding a transcript moderation result to the review with ID {0}.", review_id);

    // Screen the transcript using the Text Moderation API. For more information, see:
    // https://dev.cognitive.azure.cn/docs/services/57cf753a3f9b070c105bd2c1/operations/57cf753a3f9b070868a1f66f
    Screen screen = client.TextModeration.ScreenText("eng", "text/plain", transcript);

    // Map the term list returned by ScreenText into a term list we can pass to AddVideoTranscriptModerationResult.
    List<TranscriptModerationBodyItemTermsItem> terms = new List<TranscriptModerationBodyItemTermsItem>();
    if (null != screen.Terms)
    {
        foreach (var term in screen.Terms)
        {
            if (term.Index.HasValue)
            {
                terms.Add(new TranscriptModerationBodyItemTermsItem(term.Index.Value, term.Term));
            }
        }
    }

    List<TranscriptModerationBodyItem> body = new List<TranscriptModerationBodyItem>()
    {
        new TranscriptModerationBodyItem()
        {
            Timestamp = "0",
            Terms = terms
        }
    };

    client.Reviews.AddVideoTranscriptModerationResult("application/json", TeamName, review_id, body);

    Thread.Sleep(throttleRate);
}

发布视频评论Publish video review

使用 ContentModeratorClient.Reviews.PublishVideoReview 发布视频评论。You publish a video review with ContentModeratorClient.Reviews.PublishVideoReview. PublishVideoReview 具有以下必需参数:PublishVideoReview has the following required parameters:

  1. 内容审查器团队名称。Your Content Moderator team name.
  2. CreateVideoReviews 返回的视频评论 ID。The video review ID returned by CreateVideoReviews.

将以下方法定义添加到 VideoReviews 命名空间中的 Program 类。Add the following method definition to namespace VideoReviews, class Program.

/// <summary>
/// Publish the indicated video review. For more information, see the reference API:
/// https://dev.cognitive.azure.cn/docs/services/580519463f9b070e5c591178/operations/59e7bb29e7151f0b10d45201
/// </summary>
/// <param name="client">The Content Moderator client.</param>
/// <param name="review_id">The video review ID.</param>
private static void PublishReview(ContentModeratorClient client, string review_id)
{
    Console.WriteLine("Publishing the review with ID {0}.", review_id);
    client.Reviews.PublishVideoReview(TeamName, review_id);
    Thread.Sleep(throttleRate);
}

汇总Putting it all together

Main 方法定义添加到 VideoTranscriptReviews 命名空间中的 Program 类。Add the Main method definition to namespace VideoTranscriptReviews, class Program. 最后,关闭 Program 类和 VideoTranscriptReviews 命名空间。Finally, close the Program class and the VideoTranscriptReviews namespace.

备注

该程序使用 VTT 格式的示例脚本。The program uses a sample transcript in the VTT format. 在实际解决方案中,可使用 Azure Media Indexer 服务根据视频生成脚本In a real-world solution, you use the Azure Media Indexer service to generate a transcript from a video.

static void Main(string[] args)
{
    using (ContentModeratorClient client = NewClient())
    {
        // Create a review with the content pointing to a streaming endpoint (manifest)
        var streamingcontent = "https://amssamples.streaming.mediaservices.windows.net/91492735-c523-432b-ba01-faba6c2206a2/AzureMediaServicesPromo.ism/manifest";
        string review_id = CreateReview(client, "review1", streamingcontent);

        var transcript = @"WEBVTT

        01:01.000 --> 02:02.000
        First line with a negative word in a transcript.

        02:03.000 --> 02:25.000
        This is another line in the transcript.
        ";

        AddTranscript(client, review_id, transcript);

        AddTranscriptModerationResult(client, review_id, transcript);

        // Publish the review
        PublishReview(client, review_id);

        Console.WriteLine("Open your Content Moderator Dashboard and select Review > Video to see the review.");
        Console.WriteLine("Press any key to close the application.");
        Console.ReadKey();
    }
}

运行程序并查看输出Run the program and review the output

运行应用程序时,将显示以下行中的输出:When you run the application, you see an output on the following lines:

Creating a video review.
Adding a transcript to the review with ID 201801v5b08eefa0d2d4d64a1942aec7f5cacc3.
Adding a transcript moderation result to the review with ID 201801v5b08eefa0d2d4d64a1942aec7f5cacc3.
Publishing the review with ID 201801v5b08eefa0d2d4d64a1942aec7f5cacc3.
Open your Content Moderator Dashboard and select Review > Video to see the review.
Press any key to close the application.

转到“评论”>“视频”>“视频”屏幕上内容审查器评论工具中的的视频脚本评论**** **** ****。Go to the video transcript review in your Content Moderator review tool on the Review>Video>Transcript screen.

你会看到以下功能:You see the following features:

  • 你添加的两行脚本The two lines of transcript you added
  • 由文本审查服务找到并突出显示的不敬词The profanity term found and highlighted by the text moderation service
  • 选择脚本文本将从该时间戳开始播放视频Selecting a transcription text starts the video from that timestamp

用于人工审查器的视频脚本评论

后续步骤Next steps

为适用于 .NET 的此内容审查器快速入门以及其他内容审查器快速入门获取内容审查器 .NET SDKVisual Studio 解决方案Get the Content Moderator .NET SDK and the Visual Studio solution for this and other Content Moderator quickstarts for .NET.

了解如何在评论工具中生成视频评论Learn how to generate video reviews in the review tool.

查看有关如何开发完整视频审查解决方案的详细教程。Check out the detailed tutorial on how to develop a complete video moderation solution.