/// <summary>
        /// Function to create video operation settings. Emotion operations should not have any settings
        /// </summary>
        /// <param name="operation">The currently selected API operation</param>
        /// <returns>Returns default <see cref="VideoOperationSettings"/> for the currently selected operation</returns>
        public VideoOperationSettings CreateVideoOperationSettings(AvailableOperations operation)
        {
            VideoOperationSettings videoOperationSettings = null;

            switch (operation)
            {
            case AvailableOperations.Emotion:
                videoOperationSettings = null;
                break;

            case AvailableOperations.FaceDetection:
                videoOperationSettings = new FaceDetectionOperationSettings();
                break;

            case AvailableOperations.Stabilization:
                videoOperationSettings = new VideoStabilizationOperationSettings();
                break;

            case AvailableOperations.MotionDetection:
                videoOperationSettings = new MotionDetectionOperationSettings()
                {
                    DetectLightChange  = true,
                    FrameSamplingValue = 10,
                    MergeTimeThreshold = 10,
                    SensitivityLevel   = MotionDetectionOperationSettings.SensitivityLevels.Medium
                };
                break;

            case AvailableOperations.Thumbnail:
                videoOperationSettings = new VideoThumbnailOperationSettings()
                {
                    FadeInFadeOut = true,
                    MaxMotionThumbnailDurationInSecs = 10,
                    OutputAudio = true,
                    OutputType  = VideoThumbnailOperationSettings.OutputTypes.Video
                };
                break;

            default:
                break;
            }

            return(videoOperationSettings);
        }
Exemplo n.º 2
0
        // -----------------------------------------------------------------------
        // KEY SAMPLE CODE STARTS HERE
        // -----------------------------------------------------------------------
        private async Task GenerateVideoThumbnail(string subscriptionKey, string originalFilePath)
        {
            _dataContext.IsWorking = true;
            _dataContext.SourceUri = null;
            _dataContext.ResultUri = null;

            Helpers.Log(LogIdentifier, "Start generating thumbnail");
            Microsoft.ProjectOxford.Video.VideoServiceClient client =
                new Microsoft.ProjectOxford.Video.VideoServiceClient(subscriptionKey);

            client.Timeout = TimeSpan.FromMinutes(10);

            using (FileStream originalStream = new FileStream(originalFilePath, FileMode.Open, FileAccess.Read))
            {
                VideoThumbnailOperationSettings vtOperationSettings = new VideoThumbnailOperationSettings
                {
                    OutputType = VideoThumbnailOperationSettings.OutputTypes.Video,
                    MaxMotionThumbnailDurationInSecs = 10,
                    FadeInFadeOut = true,
                    OutputAudio   = true
                };
                // Creates a video operation of video thumbnail
                Helpers.Log(LogIdentifier, "Start uploading video");
                Operation operation = await client.CreateOperationAsync(originalStream, vtOperationSettings);

                Helpers.Log(LogIdentifier, "Uploading video done");

                // Starts querying service status
                OperationResult result = await client.GetOperationResultAsync(operation);

                while (result.Status != OperationStatus.Succeeded && result.Status != OperationStatus.Failed)
                {
                    Helpers.Log(LogIdentifier, "Server status: {0}, wait {1} seconds...", result.Status, QueryWaitTime.TotalSeconds);
                    await Task.Delay(QueryWaitTime);

                    result = await client.GetOperationResultAsync(operation);
                }
                Helpers.Log(LogIdentifier, "Finish processing with server status: " + result.Status);

                // Processing finished, checks result
                if (result.Status == OperationStatus.Succeeded)
                {
                    // Downloads generated video
                    string tmpFilePath = Path.GetTempFileName() + Path.GetExtension(originalFilePath);
                    Helpers.Log(LogIdentifier, "Start downloading result video");
                    using (Stream resultStream = await client.GetResultVideoAsync(result.ResourceLocation))
                        using (FileStream stream = new FileStream(tmpFilePath, FileMode.Create))
                        {
                            byte[] b      = new byte[2048];
                            int    length = 0;
                            while ((length = await resultStream.ReadAsync(b, 0, b.Length)) > 0)
                            {
                                await stream.WriteAsync(b, 0, length);
                            }
                        }
                    Helpers.Log(LogIdentifier, "Downloading result video done");

                    _dataContext.SourceUri = new Uri(originalFilePath);
                    _dataContext.ResultUri = new Uri(tmpFilePath);
                }
                else
                {
                    // Failed
                    Helpers.Log(LogIdentifier, "Fail reason: " + result.Message);
                }

                _dataContext.IsWorking = false;
            }
        }