Exemple #1
0
 public Microsoft.WindowsAzure.MediaServices.Client.IMediaProcessor GetMediaProcessorByName(string name)
 {
     // Query for a media processor to get a reference.
     // Get the latest in version
     Microsoft.WindowsAzure.MediaServices.Client.IMediaProcessor processor =
         (from p in this.MediaContext.MediaProcessors where p.Name == name select p)
         .ToList()
         .OrderBy(wame => new Version(wame.Version))
         .LastOrDefault();
     if (processor == null)
     {
         throw new ArgumentException(string.Format(System.Globalization.CultureInfo.CurrentCulture,
                                                   "Unknown processor",
                                                   name));
     }
     return(processor);
 }
Exemple #2
0
        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;
            string fileName               = 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);

            if (inputAsset.AssetType.ToString() == "Video")
            {
                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);
            }

            else if (true)
            {
                IMediaProcessor processor     = GetLatestMediaProcessorByName("Media Encoder Standard");
                string          configuration = System.IO.File.ReadAllText(HttpContext.Server.MapPath("~/MediaServicesCustomPreset.xml"));
                ITask           task          = job.Tasks.AddNew("Media Encoder Standard encoding task", processor, "Adaptive Streaming", TaskOptions.None);

                //ITask task = job.Tasks.AddNew("My encoding task", processor, configuration, TaskOptions.None);
                task.InputAssets.Add(inputAsset);
                task.OutputAssets.AddNew(inputAsset.Name + "-Adaptive-Bitrate-MP4", AssetCreationOptions.None);
            }
            job.Submit();

            //Task progressJobTask = job.GetExecutionProgressTask(CancellationToken.None);

            //progressJobTask.Wait();
            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.OriginalName;
                fileName = model.FileName;
            }
            // Generate Streaming URL
            string smoothStreamingUri = GetStreamingOriginLocator(encodedAsset, fileName);

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

            video.AssetId        = assetId;
            video.EncodingJobId  = job.Id;
            video.EncodedAssetId = encodedAsset.Id;
            video.LocatorUri     = smoothStreamingUri;
            video.IsEncrypted    = useAESRestriction;
            video.IndexedAssetId = ((IAsset)Session["indexingMediaAssets"]).Id;
            video.AssetFileName  = uploadFileOriginalName;
            video.text           = Session["textParsed"].ToString();
            var videoformatlist = new List <string> {
                ".mp4", ".avi", ".mkv", ".flv", ".wmv"
            };
            var audioformatlist = new List <string> {
                ".wav", ".mp3", ".aac"
            };
            var format = uploadFileOriginalName.Substring(uploadFileOriginalName.Length - 4);

            if (videoformatlist.Contains(format))
            {
                video.filetype = "video";
            }
            else if (audioformatlist.Contains(format))
            {
                video.filetype = "audio";
            }
            else
            {
                video.filetype = "other";
            }

            //  Repository dal = new Repository();
            //var result = dal.SaveVideo(video);
            SaveVideoListtoJson(video);
            if (useAESRestriction)
            {
                token = AzureMediaAsset.GetTestToken(encodedAsset.Id, encodedAsset);
            }

            // Remove session
            Session.Remove("CurrentFile");
            Session.Clear();
            // 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 = useAESRestriction,
                token = token
            }));
        }