Пример #1
0
        public ActionResult EncodeToAdaptiveBitrateMP4s(string assetId, string fileName)
        {
            // Note: You need atleast 1 reserve streaming unit for dynamic packaging of encoded media. If you don't have that, you can't see video file playing
            try
            {
                CloudMediaContext context = new CloudMediaContext(mediaAccountName, mediaAccountKey);
                IAsset            asset   = GetAssetById(context, assetId);

                IJob job = context.Jobs.CreateWithSingleTask(MediaProcessorNames.AzureMediaEncoder,
                                                             MediaEncoderTaskPresetStrings.H264AdaptiveBitrateMP4Set720p,
                                                             asset,
                                                             "UploadedVideo-" + Guid.NewGuid().ToString() + "-Adaptive-Bitrate-MP4",
                                                             AssetCreationOptions.None);

                job.Submit();

                IAsset encodedAsset = job.OutputMediaAssets[0];


                //// Encryption (AES or DRM)
                // Encode outputAsset with AES Key
                //string token = AESEncryption.CreateAESEncryption(context, encodedAsset);

                string smoothStreamingUri = PublishAssetGetURLs(encodedAsset, fileName);
                string assetDetails       = "MediaServiceFileName:" + encodedAsset.Name + ", MediaServiceContainerUri:" + encodedAsset.Uri + ", AssetId:" + encodedAsset.Id;

                // Save video URI in database
                AzureMediaServicesContext db = new AzureMediaServicesContext();
                Video video = new Video();
                video.Id       = 0;
                video.AssetId  = encodedAsset.Id;
                video.VideoURI = GetWithoutHttp(smoothStreamingUri);
                db.Videos.Add(video);
                db.SaveChanges();

                return(Json(new
                {
                    error = false,
                    message = "Encoding scheduled with Job Id " + job.Id + ". Encoded output Asset Id: " + encodedAsset.Id,
                    assetId = encodedAsset.Id,
                    jobId = job.Id
                }));
            }
            catch (Exception)
            {
                return(Json(new
                {
                    error = true,
                    message = "Error occured in encoding."
                }));
            }
        }
        public ActionResult EncodeToAdaptiveBitrateMP4s(string assetId)
        {
            // Note: You need atleast 1 reserve streaming unit for dynamic packaging of encoded media. If you don't have that, you can't see video file playing.

            IAsset inputAsset             = GetAssetById(assetId);
            string token                  = string.Empty;
            string uploadFileOriginalName = string.Empty;

            ////// Without preset (say default preset), works very well
            //IJob job = context.Jobs.CreateWithSingleTask(MediaProcessorNames.AzureMediaEncoder,
            //    MediaEncoderTaskPresetStrings.H264AdaptiveBitrateMP4Set720p,
            //    inputAsset,
            //    "UploadedVideo-" + Guid.NewGuid().ToString().ToLower() + "-Adaptive-Bitrate-MP4",
            //    AssetCreationOptions.None);
            //job.Submit();
            //IAsset encodedOutputAsset = job.OutputMediaAssets[0];


            //// XML Preset
            IJob            job           = context.Jobs.Create(inputAsset.Name);
            IMediaProcessor processor     = GetLatestMediaProcessorByName("Media Encoder Standard");
            string          configuration = System.IO.File.ReadAllText(HttpContext.Server.MapPath("~/MediaServicesCustomPreset.xml"));
            ITask           task          = job.Tasks.AddNew(inputAsset.Name + "- encoding task", processor, configuration, TaskOptions.None);

            task.InputAssets.Add(inputAsset);
            task.OutputAssets.AddNew(inputAsset.Name + "-Adaptive-Bitrate-MP4", AssetCreationOptions.None);
            job.Submit();
            IAsset encodedAsset = job.OutputMediaAssets[0];

            // process policy & encryption
            ProcessPolicyAndEncryption(encodedAsset);

            // Get file name
            string fileSession = "CurrentFile";

            if (Session[fileSession] != null)
            {
                CloudFile model = (CloudFile)Session[fileSession];
                uploadFileOriginalName = model.FileName;
            }

            // Generate Streaming URL
            string smoothStreamingUri = GetStreamingOriginLocator(encodedAsset, uploadFileOriginalName);

            // add jobid and output asset id in database
            AzureMediaServicesContext db = new AzureMediaServicesContext();
            var video = new Video();

            video.RowAssetId     = assetId;
            video.EncodingJobId  = job.Id;
            video.EncodedAssetId = encodedAsset.Id;
            video.LocatorUri     = smoothStreamingUri;
            video.IsEncrypted    = useEncryption;
            db.Videos.Add(video);
            db.SaveChanges();

            if (useEncryption)
            {
                token = AzureMediaAsset.GetTestToken(encodedAsset.Id, encodedAsset);
            }

            // Remove session
            Session.Remove("CurrentFile");

            // return success response
            return(Json(new
            {
                error = false,
                message = "Congratulations! Video is uploaded and pipelined for encoding, check console log for after encoding playback details.",
                assetId = assetId,
                jobId = job.Id,
                locator = smoothStreamingUri,
                encrypted = useEncryption,
                token = token
            }));
        }