Пример #1
0
 private bool SetMetadata(EncodedMediaRecord encodedMediaRecord, XmlDocument metadata)
 {
     try {
         var sizeNode = metadata.SelectNodes("MediaItems/MediaItem/DisplayVideoSize").Item(0);
         encodedMediaRecord.Width = Convert.ToInt32(sizeNode.Attributes["Width"].Value);
         encodedMediaRecord.Height = Convert.ToInt32(sizeNode.Attributes["Height"].Value);
         var frameRate = metadata.SelectNodes("MediaItems/MediaItem/FrameRate").Item(0);
         encodedMediaRecord.Framerate = Convert.ToDecimal(frameRate.InnerText, CultureInfo.InvariantCulture);
         var aspectRatio = metadata.SelectNodes("MediaItems/MediaItem/AspectRatio").Item(0);
         encodedMediaRecord.AspectRatio = Convert.ToDecimal(aspectRatio.InnerText, CultureInfo.InvariantCulture);
         return true;
     }
     catch (Exception ex) {
         _logger.Warning(ex, "Problem setting metadata for id=" + encodedMediaRecord.Id);
         return false;
     }
 }
Пример #2
0
        public IJob BeginEncodingJob(string assetId, int encodingId, string username)
        {
            lock (_locker)
            {
                using (var scope = _workContextAccessor.CreateWorkContextScope()) {
                    var transactionManager = scope.Resolve<ITransactionManager>();
                    transactionManager.Demand();
                    var mediaItemRep = scope.Resolve<IRepository<MediaItemRecord>>();
                    var encodingPresetRep = scope.Resolve<IRepository<EncodingPresetRecord>>();
                    var encodedMediaRep = scope.Resolve<IRepository<EncodedMediaRecord>>();
                    var config = scope.Resolve<IConfig>();
                    var cloudServiceContext = new CloudMediaContext(config.MediaServiceAccount, config.MediaServiceKey);
                    try
                    {
                        var mediaItem = mediaItemRep.Table.FirstOrDefault(mi => mi.AssetId == assetId);
                        if (mediaItem == null)
                        {
                            throw new Exception("MediaItem with AssetId '" + assetId + "' not found.");
                        }

                        var encodingPreset = encodingPresetRep.Get(encodingId);
                        if (encodingPreset == null)
                        {
                            throw new Exception("Encoding preset with id:" + encodingId + "not found.");
                        }

                        var mediaEncodedList = encodedMediaRep.Table.Where(e => e.EncodingPreset.Id == encodingId && e.MediaItem.Id == mediaItem.Id);
                        foreach (var mediaEncoded in mediaEncodedList)
                        {
                            encodedMediaRep.Delete(mediaEncoded);
                            DeleteJob(mediaEncoded.JobId, cloudServiceContext);
                            DeleteAsset(GetAsset(mediaEncoded.AssetId, cloudServiceContext));
                        }

                        var encodedMediaRecord = new EncodedMediaRecord
                        {
                            MediaItem = mediaItem,
                            EncodingPreset = encodingPreset,
                            Status = "Initializing",
                            CreatedUtc = DateTime.UtcNow,
                            ModifiedUtc = DateTime.UtcNow,
                            Owner = username
                        };

                        encodedMediaRep.Create(encodedMediaRecord);
                        encodedMediaRep.Flush();

                        var asset = cloudServiceContext.Assets.Where(a => a.Id == assetId).ToList().FirstOrDefault();
                        if (asset == null)
                        {
                            throw new Exception("Asset with id '" + assetId + "' not found.");
                        }
                        var job = cloudServiceContext.Jobs.Create(String.Format("Job for '{0}' using '{1}'", asset.Name, encodingPreset.ShortDescription));
                        encodedMediaRecord.Status = job.State.ToString();
                        var processor = GetLatestMediaProcessorByName("Windows Azure Media Encoder", cloudServiceContext);
                        var file = asset.AssetFiles.ToList().FirstOrDefault();
                        if (file == null)
                        {
                            throw new Exception("File for asset '" + asset.Name + "' not found.");
                        }
                        var task = job.Tasks.AddNew(String.Format("Encoding task from file {0} to '{1}'", file.Name, encodingPreset.Name), processor, encodingPreset.Name, TaskOptions.ProtectedConfiguration);
                        task.InputAssets.Add(asset);
                        task.OutputAssets.AddNew(file.Name.Replace('.', '-') + "-Output-" + encodingPreset.ShortDescription, AssetCreationOptions.None);
                        job.Submit();
                        if (job.State == JobState.Error)
                        {
                            encodedMediaRecord.Status = job.State.ToString();
                            encodedMediaRecord.JobErrorMessage = "";
                            foreach (var message in job.Tasks.Where(t => t.ErrorDetails.Any()).SelectMany(t => t.ErrorDetails).Select(ed => ed.Message))
                            {
                                encodedMediaRecord.JobErrorMessage += message + "; ";
                            }

                        }
                        else
                        {
                            encodedMediaRecord.Status = job.State.ToString();
                        }
                        encodedMediaRecord.JobId = job.Id;
                        encodedMediaRep.Update(encodedMediaRecord);
                        encodedMediaRep.Flush();
                        return job;

                    }
                    catch (Exception e)
                    {
                        transactionManager.Cancel();
                        _logger.Error(e, "Error while processing background task");
                        return null;
                    }
                }
            }
        }