Пример #1
0
        static void Main(string[] args)
        {
            ConfigWrapper config = new ConfigWrapper(new ConfigurationBuilder()
                                                     .SetBasePath(Directory.GetCurrentDirectory())
                                                     .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                                                     .AddEnvironmentVariables()
                                                     .Build());
            TextModerator textModerator = new TextModerator(config);

            string videoPath = "https://shigeyfampdemo.azurewebsites.net/videos/ignite.mp4";

            try
            {
                Task <TextModerationResult> task1  = textModerator.ModerateVideo(videoPath);
                TextModerationResult        result = task1.Result;
                Task <string> task2 = WebVttParser.LoadWebVtt(result.StreamingUrlDetails.VttUrl);
                result.WebVtt = task2.Result;
                List <CaptionTextModerationResult>         captionTextResults = WebVttParser.ParseWebVtt(result.WebVtt);
                Task <List <CaptionTextModerationResult> > task3 = textModerator.TextScreen(captionTextResults);
                result.CaptionTextResults = task3.Result;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: ", e.ToString());
            }
            Console.WriteLine("Visual Modeation has finished.");
            Console.WriteLine("Press any key to stop...");
            Console.ReadKey();
        }
Пример #2
0
        public async Task <string> AddVideoTranscriptModerationResultToVideoReview(TextModerationResult result, string reviewId)
        {
            // Step 2 (c) - Add video transcript moderation result to Video Review

            bool isSuccess = true;

            isSuccess = await AddVideoTranscriptModerationResultToReviewWithContentModeratorReviewAPI(reviewId, result.captionTextResults);

            if (!isSuccess)
            {
                throw new Exception("AddVideoTranscriptModerationResultToReviewWithContentModeratorReviewAPI call failed.");
            }

            return(reviewId);
        }
Пример #3
0
        private static async Task <TextModerationResult> ProcessTextModeration(string webVttUrl)
        {
            Console.WriteLine("[Content Moderator] Text moderation process started...");
            TextModerationResult result = new TextModerationResult();

            try
            {
                string webVttString = await WebVttParser.LoadWebVtt(webVttUrl);

                result.webVtt = webVttString;
                List <CaptionTextModerationResult> captionTextResults = WebVttParser.ParseWebVtt(webVttString);
                result.captionTextResults = await textModerator.TextScreen(captionTextResults);
            }
            catch (Exception e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("[Content Moderator] Text moderation process failed. Error: ", e.ToString());
                Console.ForegroundColor = ConsoleColor.White;
            }

            Console.WriteLine("[Content Moderator] Text moderation process completed...");
            return(result);
        }
Пример #4
0
        private static async Task <string> UploadVideoReview(VisualModerationResult vmResult, TextModerationResult tmResult)
        {
            Console.WriteLine("[Content Moderator] Video Review publishing started...");
            string reviewId      = string.Empty;
            string videoFilePath = vmResult.VideoFilePath;

            try
            {
                reviewId = await videoReviewUploader.CreateVideoReview(vmResult, tmResult);

                await videoReviewUploader.AddVideoFramesToVideoReview(vmResult, reviewId);

                await videoReviewUploader.AddVideoTranscriptToVideoReview(tmResult, reviewId);

                await videoReviewUploader.AddVideoTranscriptModerationResultToVideoReview(tmResult, reviewId);

                await videoReviewUploader.PublishVideoReview(reviewId);
            }
            catch (Exception e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("[Content Moderator] Video Review publishing failed. Error: ", e.ToString());
                Console.ForegroundColor = ConsoleColor.White;
            }

            FFmpegComponent.CleanUp(videoFilePath, reviewId);
            Console.WriteLine("[Content Moderator] Video Review published Successfully and the review Id {0}", reviewId);

            return(reviewId);
        }
Пример #5
0
        public async Task <string> CreateVideoReview(VisualModerationResult vmResult, TextModerationResult tmResult)
        {
            // Step 1 - Create Video Review

            string            reviewId       = string.Empty;
            List <FrameEvent> frameEventList = ExtractFrameEventListFromJson(vmResult.VisualModeratedJson);

            frameEventList = MergeTextModerationResultToFrameEventList(frameEventList, tmResult.captionTextResults);

            reviewId = await CreateVideoReviewWithContentModeratorReviewAPI(vmResult, frameEventList);

            this.frameEventList = FixFrameNameInFrameEventList(frameEventList, reviewId);

            return(reviewId);
        }
Пример #6
0
        public async Task <TextModerationResult> ModerateVideo(string videoPath)
        {
            string resourceGroup = this.textModeratorConfig.ResourceGroup;
            string accountName   = this.textModeratorConfig.AccountName;

            IAzureMediaServicesClient client = await CreateMediaServicesClientAsync();

            // Set the polling interval for long running operations to 2 seconds.
            // The default value is 30 seconds for the .NET client SDK
            client.LongRunningOperationRetryTimeout = 2;
            TextModerationResult result = null;

            try
            {
                string uniqueness = Guid.NewGuid().ToString();
                string jobName    = JobNamePrefix + uniqueness;
                string streamingOutputAssetName = textModeratorConfig + uniqueness;
                string analyticsOutputAssetName = textModeratorConfig + uniqueness;

                JobInput     jobInput               = null;
                List <Asset> outputAssetList        = new List <Asset>();
                Transform    videoAnalyzerTransform = EnsureTransformExists(client, resourceGroup, accountName, VisualModerationTransformName);

                if (videoPath.StartsWith("http://") || videoPath.StartsWith("https://"))
                {
                    Uri    inputVideoUri = new Uri(videoPath);
                    string baseUri       = inputVideoUri.Scheme + "://" + inputVideoUri.Host;
                    for (int i = 0; i < inputVideoUri.Segments.Length - 1; i++)
                    {
                        baseUri = baseUri + inputVideoUri.Segments[i];
                    }
                    jobInput = new JobInputHttp(
                        baseUri: baseUri,
                        files: new List <String> {
                        inputVideoUri.Segments[inputVideoUri.Segments.Length - 1]
                    },
                        label: JobInputLabel
                        );
                }
                else
                {
                    string inputAssetName = InputAssetNamePrefix + uniqueness;
                    CreateInputAssetWithUploading(client, resourceGroup, accountName, inputAssetName, videoPath).Wait();
                    jobInput = new JobInputAsset(assetName: inputAssetName, label: JobInputLabel);
                }

                List <JobOutput> jobOutputList        = new List <JobOutput>();
                Asset            analyticsOutputAsset = CreateOutputAsset(client, resourceGroup, accountName, analyticsOutputAssetName);
                outputAssetList.Add(analyticsOutputAsset);
                jobOutputList.Add(new JobOutputAsset(assetName: analyticsOutputAsset.Name, label: JobAnalyticsOutputLabel));
                if (this.textModeratorConfig.EnableStreamingVideo)
                {
                    Asset streamingOutputAsset = CreateOutputAsset(client, resourceGroup, accountName, streamingOutputAssetName);
                    outputAssetList.Add(streamingOutputAsset);
                    jobOutputList.Add(new JobOutputAsset(assetName: streamingOutputAsset.Name, label: JobVideoOutputLabel));
                }

                JobOutput[] jobOutputs = jobOutputList.ToArray();
                Job         job        = SubmitJob(client, resourceGroup, accountName, videoAnalyzerTransform.Name, jobName, jobInput, jobOutputs);
                job = WaitForJobToFinish(client, resourceGroup, accountName, videoAnalyzerTransform.Name, jobName);

                if (job.State == JobState.Finished)
                {
                    Console.WriteLine("\nAMSv3 Job finished.");
                    List <StreamingLocator> locators = PublishAssets(client, resourceGroup, accountName, jobOutputList);
                    result = CreateVisualModeratorResult(client, resourceGroup, accountName, videoPath, locators);

                    using (var webClient = new WebClient())
                    {
                        webClient.Encoding          = Encoding.UTF8;
                        result.VisualModerationJson = webClient.DownloadString(result.StreamingUrlDetails.ContentModerationJsonUrl);
                        result.OcrJson = webClient.DownloadString(result.StreamingUrlDetails.OcrJsonUrl);
                    }
                }
                else if (job.State == JobState.Error)
                {
                    Console.WriteLine($"ERROR: Job finished with error message: {job.Outputs[0].Error.Message}");
                    Console.WriteLine($"ERROR:                   error details: {job.Outputs[0].Error.Details[0].Message}");
                }
            }
            catch (ApiErrorException ex)
            {
                string code    = ex.Body.Error.Code;
                string message = ex.Body.Error.Message;

                Console.WriteLine("ERROR:API call failed with error code: {0} and message: {1}", code, message);
            }

            return(result);
        }
Пример #7
0
        private TextModerationResult CreateVisualModeratorResult(IAzureMediaServicesClient client, string resourceGroup, string accountName, string videoPath, List <StreamingLocator> locators)
        {
            TextModerationResult result = new TextModerationResult();

            result.StreamingUrlDetails = new PublishedUrlDetails();
            result.VideoFilePath       = videoPath;

            var        streamingEndpoint = client.StreamingEndpoints.Get(resourceGroup, accountName, "default");
            UriBuilder uriBuilder        = new UriBuilder();

            uriBuilder.Scheme = "https";
            uriBuilder.Host   = streamingEndpoint.HostName;

            foreach (var locator in locators)
            {
                if (locator.Name.StartsWith(VideoStreamingLocatorPrefix))
                {
                    var paths = client.StreamingLocators.ListPaths(resourceGroup, accountName, locator.Name);
                    for (int i = 0; i < paths.StreamingPaths.Count; i++)
                    {
                        if (paths.StreamingPaths[i].Paths.Count > 0)
                        {
                            if (paths.StreamingPaths[i].StreamingProtocol == StreamingPolicyStreamingProtocol.SmoothStreaming)
                            {
                                uriBuilder.Path = paths.StreamingPaths[i].Paths[0];
                                result.StreamingUrlDetails.SmoothUri = uriBuilder.ToString();
                            }
                            else if (paths.StreamingPaths[i].StreamingProtocol == StreamingPolicyStreamingProtocol.Dash)
                            {
                                uriBuilder.Path = paths.StreamingPaths[i].Paths[0];
                                result.StreamingUrlDetails.MpegDashUri = uriBuilder.ToString();
                            }
                            else if (paths.StreamingPaths[i].StreamingProtocol == StreamingPolicyStreamingProtocol.Hls)
                            {
                                uriBuilder.Path = paths.StreamingPaths[i].Paths[0];
                                result.StreamingUrlDetails.HlsUri = uriBuilder.ToString();
                            }
                        }
                    }
                    result.VideoName   = locator.Name;
                    result.AccessToken = null;
                }
                else if (locator.Name.StartsWith(AnalyticsStreamingLocatorPrefix))
                {
                    var dlpaths = client.StreamingLocators.ListPaths(resourceGroup, accountName, locator.Name);
                    for (int i = 0; i < dlpaths.DownloadPaths.Count; i++)
                    {
                        if (dlpaths.DownloadPaths[i].EndsWith("transcript.vtt"))
                        {
                            uriBuilder.Path = dlpaths.DownloadPaths[i];
                            result.StreamingUrlDetails.VttUrl = uriBuilder.ToString();
                        }
                        else if (dlpaths.DownloadPaths[i].EndsWith("contentmoderation.json"))
                        {
                            uriBuilder.Path = dlpaths.DownloadPaths[i];
                            result.StreamingUrlDetails.VttUrl = uriBuilder.ToString();
                        }
                        else if (dlpaths.DownloadPaths[i].EndsWith("ocr.json"))
                        {
                            uriBuilder.Path = dlpaths.DownloadPaths[i];
                            result.StreamingUrlDetails.VttUrl = uriBuilder.ToString();
                        }
                    }
                }
            }
            return(result);
        }