/// <summary>
        /// Accepts filename of video to process with Azure Media Services
        /// </summary>
        /// <param name="fileName">Filename of Video to process in AMS</param>
        /// <returns>
        /// Task that will complete when processing has completed.
        /// </returns>
        public async Task AddVideo(string fileName)
        {
            try
            {
                IAzureMediaServicesClient client = _amsHelpers.CreateMediaServicesClient();

                // 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;

                var videos = await GetVideos();

                if (!videos.Any(a => a.Name == fileName))
                {
                    await _amsHelpers.ProcessVideo(client, fileName);
                }
                else
                {
                    await _storageHelpers.DeleteBlobAsync(fileName);

                    _log.LogInformation($"Video: {fileName} has already been processed.");
                }
            }
            catch (ApiErrorException ex)
            {
                _log.LogError(ex.Message);
                _log.LogError("ERROR:API call failed with error code: {0} and message: {1}",
                              ex.Body.Error.Code, ex.Body.Error.Message);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Takes input filename and creates, starts, and monitors processing job
        /// When job completes, video is stored in Azure Table Storage
        /// </summary>
        /// <param name="client">The Media Services client.</param>
        /// <param name="fileName">The name of the file to process with Azure Media Services</param>
        /// <returns></returns>
        // <ProcessVideo>
        public async Task ProcessVideo(IAzureMediaServicesClient client, string fileName)
        {
            await GetOrCreateTransformAsync(client, _config.ResourceGroup, _config.AccountName, AdaptiveStreamingTransformName);

            // Creating a unique suffix so that we don't have name collisions if you run the sample
            // multiple times without cleaning up.
            string uniqueness      = Guid.NewGuid().ToString().Substring(0, 13);
            string jobName         = "job-" + uniqueness;
            string locatorName     = "locator-" + uniqueness;
            string outputAssetName = "output-" + uniqueness;
            string inputAssetName  = "input-" + uniqueness;

            // Create a new input Asset and upload the specified local video file into it.
            await CreateInputAssetAsync(client, _config.ResourceGroup, _config.AccountName, inputAssetName, fileName);

            // Use the name of the created input asset to create the job input.
            JobInput jobInput = new JobInputAsset(assetName: inputAssetName);

            // Output from the encoding Job must be written to an Asset, so let's create one
            Asset outputAsset = await CreateOutputAssetAsync(client, _config.ResourceGroup, _config.AccountName, outputAssetName);

            Job job = await SubmitJobAsync(client, _config.ResourceGroup, _config.AccountName, AdaptiveStreamingTransformName, jobName, jobInput, outputAssetName);

            // In this demo code, we will poll for Job status
            // Polling is not a recommended best practice for production applications because of the latency it introduces.
            // Overuse of this API may trigger throttling. Developers should instead use Event Grid.
            job = await WaitForJobToFinishAsync(client, _config.ResourceGroup, _config.AccountName, AdaptiveStreamingTransformName, jobName);

            if (job.State == JobState.Finished)
            {
                StreamingLocator locator = CreateStreamingLocator(client, _config.ResourceGroup, _config.AccountName, outputAsset.Name, locatorName);

                IList <string> urls = GetStreamingURLs(client, _config.ResourceGroup, _config.AccountName, locator.Name);
                _log.LogInformation("Urls Created");
                foreach (var url in urls)
                {
                    _log.LogInformation(url);
                }
                // Get url of manifest file supported for Azure Media Player Playback
                var streamingUrl = urls.Where(a => a.EndsWith("manifest")).FirstOrDefault();

                await _storageHelpers.InsertTableRecord(fileName, streamingUrl);

                await _storageHelpers.DeleteBlobAsync(fileName);

                _log.LogInformation("Stream Processing Complete");
            }
        }