private async void CreateOverlays() { var baseVideoClip = await MediaClip.CreateFromFileAsync(baseVideoFile); composition = new MediaComposition(); composition.Clips.Add(baseVideoClip); var overlayVideoClip = await MediaClip.CreateFromFileAsync(overlayVideoFile); // Overlay video in upper left corner, retain its native aspect ratio Rect videoOverlayPosition; var encodingProperties = overlayVideoClip.GetVideoEncodingProperties(); videoOverlayPosition.Height = mediaElement.ActualHeight / 3; videoOverlayPosition.Width = (double)encodingProperties.Width / (double)encodingProperties.Height * videoOverlayPosition.Height; videoOverlayPosition.X = 0; videoOverlayPosition.Y = 0; var videoOverlay = new MediaOverlay(overlayVideoClip); videoOverlay.Position = videoOverlayPosition; videoOverlay.Opacity = 0.75; var overlayLayer = new MediaOverlayLayer(); overlayLayer.Overlays.Add(videoOverlay); composition.OverlayLayers.Add(overlayLayer); // Render to MediaElement mediaElement.Position = TimeSpan.Zero; mediaStreamSource = composition.GeneratePreviewMediaStreamSource((int)mediaElement.ActualWidth, (int)mediaElement.ActualHeight); mediaElement.SetMediaStreamSource(mediaStreamSource); rootPage.NotifyUser("Overlays created", NotifyType.StatusMessage); }
protected async override void OnNavigatedTo(NavigationEventArgs e) { _composition = new MediaComposition(); _overlayLayer = new MediaOverlayLayer(new VideoCompositorDefinition(typeof(SimpleCompositor).FullName)); _composition.OverlayLayers.Add(_overlayLayer); }
//覆盖层 private void LayerButton_Tapped(object sender, TappedRoutedEventArgs e) { //新建媒体覆盖层 MediaOverlayLayer OverlayLayer = new MediaOverlayLayer(); App.Model.MediaComposition.OverlayLayers.Add(OverlayLayer); }
protected async override void OnNavigatedTo(NavigationEventArgs e) { _composition = new MediaComposition(); _overlayLayer = new MediaOverlayLayer(new VideoCompositorDefinition(typeof(SimpleCompositor).FullName)); _composition.OverlayLayers.Add(_overlayLayer); }
private async Task <MediaOverlayLayer> CreateOverlay2(Interval videoInterval, Flight flight, TimeSpan period, Action <Status> onNewStatus, Rect position) { var count = videoInterval.Duration.ToTimeSpan().Divide(period); var mediaLayer = new MediaOverlayLayer(); for (var i = 0; i < count; i++) { var offset = i * period; var videoTime = videoInterval.Start.Plus(offset.ToDuration()); var flightTime = videoTime.Minus(flight.Date.ToInstant()); var status = flight.Statuses.FirstOrDefault(x => x.TimeElapsed >= flightTime.ToTimeSpan()); if (status != null) { onNewStatus(status); } var clip = await CreateMediaClip(period); var mediaOverlay = new MediaOverlay(clip) { Position = position, Delay = offset, }; mediaLayer.Overlays.Add(mediaOverlay); } return(mediaLayer); }
private void AddOverlay_Click(object sender, RoutedEventArgs e) { var colorClip = MediaClip.CreateFromColor(Windows.UI.Color.FromArgb(255, 125, 0, 0), TimeSpan.FromSeconds(10)); var colorOverlay = new MediaOverlay(colorClip, new Rect(10, 10, 50, 50), 1); MediaOverlayLayer colorLayer = new MediaOverlayLayer(); colorLayer.Overlays.Add(colorOverlay); mediaComposition.OverlayLayers.Add(colorLayer); UpdateSource(); }
private async Task <MediaOverlayLayer> GenerateHorizonLayer(int timeInSeconds, uint height, uint width) { CanvasBitmap cb = await CanvasBitmap.LoadAsync(CanvasDevice.GetSharedDevice(), new Uri("ms-appx:///Assets/horizon3840x2160.png")); //cb.AlphaMode = CanvasAlphaMode.Straight; //img.Source = horizonFile; MediaOverlayLayer horizonOverlay = new MediaOverlayLayer(); MediaOverlay mediaOverlay = new MediaOverlay(MediaClip.CreateFromSurface(cb, new TimeSpan(0, 0, timeInSeconds))); //generowanie horyzontu mediaOverlay.Position = new Rect(0, 0, width, height); mediaOverlay.Opacity = 1; horizonOverlay.Overlays.Add(mediaOverlay); return(horizonOverlay); }
// <SnippetAddOverlay> private void AddOverlay(MediaClip overlayMediaClip, double scale, double left, double top, double opacity) { Windows.Media.MediaProperties.VideoEncodingProperties encodingProperties = overlayMediaClip.GetVideoEncodingProperties(); Rect overlayPosition; overlayPosition.Width = (double)encodingProperties.Width * scale; overlayPosition.Height = (double)encodingProperties.Height * scale; overlayPosition.X = left; overlayPosition.Y = top; MediaOverlay mediaOverlay = new MediaOverlay(overlayMediaClip); mediaOverlay.Position = overlayPosition; mediaOverlay.Opacity = opacity; MediaOverlayLayer mediaOverlayLayer = new MediaOverlayLayer(); mediaOverlayLayer.Overlays.Add(mediaOverlay); composition.OverlayLayers.Add(mediaOverlayLayer); }
public VideoLayer(MediaComposition composition) { mediaOverlayLayer = new MediaOverlayLayer(); composition.OverlayLayers.Add(mediaOverlayLayer); }
public async Task MergeAudioVideoWebcam(StorageFile webcamFile, StorageFile videoFile, StorageFile outputFile) { if (webcamFile == null) { if (parent != null) { parent.StartWritingOutput("Webcam File is null", 1); } return; } if (videoFile == null) { if (parent != null) { parent.StartWritingOutput("Video File is null", 1); } return; } if (outputFile == null) { if (parent != null) { parent.StartWritingOutput("Output File is null (not specified)", 1); } return; } if (!webcamFile.IsAvailable) { if (parent != null) { parent.StartWritingOutput("Webcam File is not available", 1); } return; } if (!videoFile.IsAvailable) { if (parent != null) { parent.StartWritingOutput("Video File is not available", 1); } return; } if (parent != null) { parent.StartWritingOutput("Merging temporary video and webcam files..", 1); } try { MediaClip mediaClip = await MediaClip.CreateFromFileAsync(videoFile); MediaClip webcamClip = await MediaClip.CreateFromFileAsync(webcamFile); //the BorderVideoEffects need to be in a separate Windows Component Runtime webcamClip.VideoEffectDefinitions.Add(new VideoEffectDefinition(typeof(BorderVideoEffect).FullName)); int xoffset = 20; int yoffset = 30; double overlayOpacity = 1.0; double overlayHeight = 150; Rect overlayRect; VideoEncodingProperties webcamEncodingProperties = webcamClip.GetVideoEncodingProperties(); overlayRect.Height = overlayHeight; double widthToHeightAspectRatio = ((double)webcamEncodingProperties.Width / (double)webcamEncodingProperties.Height); overlayRect.Width = widthToHeightAspectRatio * overlayHeight; overlayRect.X = xoffset; overlayRect.Y = yoffset; MediaOverlay mediaOverlay = new MediaOverlay(webcamClip); mediaOverlay.Position = overlayRect; mediaOverlay.Opacity = overlayOpacity; mediaOverlay.AudioEnabled = true; MediaOverlayLayer mediaOverlayLayer = new MediaOverlayLayer(); mediaOverlayLayer.Overlays.Add(mediaOverlay); MediaComposition mediaComposition = new MediaComposition(); mediaComposition.Clips.Add(mediaClip); mediaComposition.OverlayLayers.Add(mediaOverlayLayer); if (outputFile != null) { var mergeOperation = mediaComposition.RenderToFileAsync(outputFile, MediaTrimmingPreference.Precise); mergeOperation.Progress += mergeWebcamProgress; mergeOperation.Completed += mergeWebcamCompleted; } } catch (Exception e) { if (parent != null) { parent.StartWritingOutput("Merge Error :" + e.Message, 1); } } }
private async void CreateOverlays() { var baseVideoClip = await MediaClip.CreateFromFileAsync(baseVideoFile); composition = new MediaComposition(); composition.Clips.Add(baseVideoClip); var overlayVideoClip = await MediaClip.CreateFromFileAsync(overlayVideoFile); // Overlay video in upper left corner, retain its native aspect ratio Rect videoOverlayPosition; var encodingProperties = overlayVideoClip.GetVideoEncodingProperties(); videoOverlayPosition.Height = mediaElement.ActualHeight / 3; videoOverlayPosition.Width = (double)encodingProperties.Width / (double)encodingProperties.Height * videoOverlayPosition.Height; videoOverlayPosition.X = 0; videoOverlayPosition.Y = 0; var videoOverlay = new MediaOverlay(overlayVideoClip); videoOverlay.Position = videoOverlayPosition; videoOverlay.Opacity = 0.75; var overlayLayer = new MediaOverlayLayer(); overlayLayer.Overlays.Add(videoOverlay); composition.OverlayLayers.Add(overlayLayer); // Render to MediaElement mediaElement.Position = TimeSpan.Zero; mediaStreamSource = composition.GeneratePreviewMediaStreamSource((int)mediaElement.ActualWidth, (int)mediaElement.ActualHeight); mediaElement.SetMediaStreamSource(mediaStreamSource); rootPage.NotifyUser("Overlays created", NotifyType.StatusMessage); }
private Task CreateExportTask(object obj, CancellationToken cancellationToken) { return(new Task(() => { var para = obj as Tuple <StorageFolder, object, string, Size>; var folder = para?.Item1; var player = para?.Item2; var fileName = para?.Item3; var mediaEncodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD720p); if (para == null || folder == null || //player == null || string.IsNullOrEmpty(fileName) || Math.Abs(para.Item4.Width) < 1 || Math.Abs(para.Item4.Height) < 1 || mediaEncodingProfile?.Video == null) { UpdateExportStatus(false); return; } var exportSize = para.Item4; mediaEncodingProfile.Video.Width = (uint)exportSize.Width; mediaEncodingProfile.Video.Height = (uint)exportSize.Height; var cacheFolder = GetCacheFolder().GetAwaiter().GetResult(); var device = CanvasDevice.GetSharedDevice(); long total = 10000; int fps = 25; var mediafileList = new List <StorageFile>(); var tmpFrameImgs = new List <CanvasBitmap>(); var layerTmp = new MediaOverlayLayer(); int start = 0; int loopGap = 1000 / fps; var span = TimeSpan.FromMilliseconds(loopGap); var backgroud = new CanvasRenderTarget(device, (float)exportSize.Width, (float)exportSize.Height, 96f); using (var session = backgroud.CreateDrawingSession()) { session.Clear(Colors.White); } for (int s = 0; s <= total; s += loopGap) { if (Canceled) { return; } var frame = new CanvasRenderTarget(device, (float)exportSize.Width, (float)exportSize.Height, 96f); using (var session = frame.CreateDrawingSession()) { session.DrawText($"frame:{s / 100}", new System.Numerics.Vector2(100, 100), Colors.Red); } tmpFrameImgs.Add(frame); var clip = MediaClip.CreateFromSurface(frame, span); layerTmp.Overlays.Add(CreateMediaOverlay(clip, exportSize, s - start)); if (s - start >= 2000 || total - s < loopGap) { var progress = (int)(s / (float)total * 100 * 0.5); UpdateProgress(progress); var composition = new MediaComposition(); composition.Clips.Add(MediaClip.CreateFromSurface(backgroud, TimeSpan.FromMilliseconds(s - start + loopGap))); composition.OverlayLayers.Add(layerTmp); var mediaPartFile = cacheFolder.CreateFileAsync( $"part_{mediafileList.Count}.mp4", CreationCollisionOption.ReplaceExisting).GetAwaiter() .GetResult(); composition.RenderToFileAsync(mediaPartFile, MediaTrimmingPreference.Fast, mediaEncodingProfile).GetAwaiter().GetResult(); mediafileList.Add(mediaPartFile); layerTmp = new MediaOverlayLayer(); foreach (var item in tmpFrameImgs) { item.Dispose(); } tmpFrameImgs.Clear(); start = s + loopGap; } } var mediaComposition = new MediaComposition(); foreach (var mediaPartFile in mediafileList) { if (Canceled) { return; } var mediaPartClip = MediaClip.CreateFromFileAsync(mediaPartFile).GetAwaiter().GetResult(); mediaComposition.Clips.Add(mediaPartClip); } lock (BackgroundAudioTracks) { foreach (var bgm in BackgroundAudioTracks) { mediaComposition.BackgroundAudioTracks.Add(bgm); } } var exportFile = folder.CreateFileAsync($"{fileName}.mp4", CreationCollisionOption.ReplaceExisting) .GetAwaiter().GetResult(); if (Canceled) { return; } var saveOperation = mediaComposition.RenderToFileAsync(exportFile, MediaTrimmingPreference.Fast, mediaEncodingProfile); saveOperation.Progress = (info, progress) => { UpdateProgress((int)(50 + progress * 0.5)); if (Canceled) { saveOperation.Cancel(); } }; saveOperation.Completed = (info, status) => { if (!Canceled) { var results = info.GetResults(); if (results != TranscodeFailureReason.None || status != AsyncStatus.Completed) { UpdateExportStatus(false); } else { UpdateExportStatus(true); } } ClearCacheAsync(); }; }, cancellationToken)); }
public async Task <MediaStreamSource> GenerateHeatmap(bool scaleFovFlag, int scaleInPercentage, bool forceFov, int forcedFov, bool horizonFlag, SessionCollection sessions, Rect overlayPosition, Windows.UI.Color colorPickerColor, double heatmapOpacity, double startTime, double stopTime, MediaClip video) { CheckHistoryErrors(sessions); List <Heatmap.Coord> inputList = await Task.Factory.StartNew(() => TrimStaticHeatmap(sessions, startTime, stopTime, video) ); MediaOverlayLayer mediaOverlayLayer = new MediaOverlayLayer(); WriteableBitmap wb = await GenerateHeatmap(inputList, forceFov, forcedFov, scaleFovFlag, scaleInPercentage); CanvasDevice device = CanvasDevice.GetSharedDevice(); SoftwareBitmap swb = SoftwareBitmap.CreateCopyFromBuffer(wb.PixelBuffer, BitmapPixelFormat.Bgra8, wb.PixelWidth, wb.PixelHeight); swb = SoftwareBitmap.Convert(swb, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore); CanvasBitmap canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap(device, swb); var clip = MediaClip.CreateFromSurface(canvasBitmap, new TimeSpan(0, 0, 0, 0, 1)); MediaOverlay mediaOverlay = new MediaOverlay(clip) { Position = overlayPosition, Opacity = heatmapOpacity }; mediaOverlayLayer.Overlays.Add(mediaOverlay); if (horizonFlag) { CanvasBitmap cb = await CanvasBitmap.LoadAsync(CanvasDevice.GetSharedDevice(), new Uri("ms-appx:///Assets/horizon3840x2160.png")); MediaOverlay horizonOverlay = new MediaOverlay(MediaClip.CreateFromSurface(cb, new TimeSpan(0, 0, 0, 0, 1))) { Position = overlayPosition, Opacity = 1 }; mediaOverlayLayer.Overlays.Add(horizonOverlay); } MediaComposition mediaComposition = new MediaComposition(); mediaComposition.Clips.Add(MediaClip.CreateFromColor(colorPickerColor, new TimeSpan(0, 0, 0, 0, 1))); mediaComposition.OverlayLayers.Add(mediaOverlayLayer); return(mediaComposition.GeneratePreviewMediaStreamSource ( (int)overlayPosition.Width, (int)overlayPosition.Height )); }
private Task CreateExportTask(CancellationToken cancellationToken) { return(new Task(() => { var provider = this.ExportProvider; var mediaEncodingProfile = MediaEncodingProfile.CreateMp4(this.ExportVideoEncodingQuality); if (provider?.ExportPartConfigs == null || this.DestinationFolder == null || provider.ExportPartConfigs.Count < 1 || string.IsNullOrEmpty(this.FileName) || Math.Abs(provider.ExportSize.Width) < 1 || Math.Abs(provider.ExportSize.Height) < 1 || mediaEncodingProfile?.Video == null) { UpdateExportStatus(false); return; } var exportSize = provider.ExportSize; mediaEncodingProfile.Video.Width = (uint)exportSize.Width; mediaEncodingProfile.Video.Height = (uint)exportSize.Height; mediaEncodingProfile.Video.FrameRate.Numerator = this.ExportFrameRate; var cacheFolder = GetCacheFolder().GetAwaiter().GetResult(); var device = CanvasDevice.GetSharedDevice(); var mediafileList = new List <StorageFile>(); var defaultBackgroud = new CanvasRenderTarget(device, (float)exportSize.Width, (float)exportSize.Height, 96f); using (var session = defaultBackgroud.CreateDrawingSession()) { session.Clear(Colors.White); } var scale = exportSize.Width / provider.ExportArea.Width; var transform = Matrix3x2.CreateScale((float)scale); var offsetPoint = Vector2.Transform(new Point(-provider.ExportArea.X, -provider.ExportArea.Y).ToVector2(), transform); transform.Translation = offsetPoint; var framePosition = new Rect(new Point(), exportSize); var timeGap = TimeSpan.FromSeconds(1) / provider.FrameRate; foreach (var videoPartConfig in provider.ExportPartConfigs) { var composition = new MediaComposition(); var layerTmp = new MediaOverlayLayer(); var tmpFrameImgs = new List <CanvasBitmap>(); if (videoPartConfig.BackgroundVideoClips?.Count > 0) { if (videoPartConfig.Delay.TotalSeconds > 0) { var blankClip = MediaClip.CreateFromColor(Colors.White, videoPartConfig.Delay); composition.Clips.Add(blankClip); } foreach (var videoClip in videoPartConfig.BackgroundVideoClips) { composition.Clips.Add(videoClip); } if (composition.Duration < videoPartConfig.Duration) { var blankClip = MediaClip.CreateFromColor(Colors.White, videoPartConfig.Duration - composition.Duration); composition.Clips.Add(blankClip); } } else { composition.Clips.Add(MediaClip.CreateFromSurface(defaultBackgroud, videoPartConfig.Duration)); } if (videoPartConfig.BackgroundAudioTracks?.Count > 0) { foreach (var track in videoPartConfig.BackgroundAudioTracks) { track.Delay -= videoPartConfig.Start; composition.BackgroundAudioTracks.Add(track); } } for (var currentPosition = videoPartConfig.Start; currentPosition < videoPartConfig.Start + videoPartConfig.Duration; currentPosition += timeGap) { if (Canceled) { return; } var progress = (int)(currentPosition / provider.Duration * 100 * 0.5); UpdateProgress(progress); var frame = new CanvasRenderTarget(device, (float)exportSize.Width, (float)exportSize.Height, 96f); using (var session = frame.CreateDrawingSession()) { session.Clear(Colors.Transparent); session.Transform = transform; provider.DrawFrame(session, currentPosition); } tmpFrameImgs.Add(frame); var clip = MediaClip.CreateFromSurface(frame, timeGap); var tmpLayer = new MediaOverlay(clip) { Position = framePosition, Opacity = 1f, Delay = currentPosition - videoPartConfig.Start }; layerTmp.Overlays.Add(tmpLayer); } composition.OverlayLayers.Add(layerTmp); var mediaPartFile = cacheFolder.CreateFileAsync( $"part_{mediafileList.Count}.mp4", CreationCollisionOption.ReplaceExisting).GetAwaiter() .GetResult(); composition.RenderToFileAsync(mediaPartFile, MediaTrimmingPreference.Fast, mediaEncodingProfile).GetAwaiter().GetResult(); mediafileList.Add(mediaPartFile); foreach (var item in tmpFrameImgs) { item.Dispose(); } tmpFrameImgs.Clear(); } defaultBackgroud?.Dispose(); var mediaComposition = new MediaComposition(); #region connect video foreach (var mediaPartFile in mediafileList) { if (Canceled) { return; } var mediaPartClip = MediaClip.CreateFromFileAsync(mediaPartFile).GetAwaiter().GetResult(); mediaComposition.Clips.Add(mediaPartClip); } #endregion #region add global BackgroundAudioTrack if (provider.GlobalBackgroundAudioTracks != null) { foreach (var bgm in provider.GlobalBackgroundAudioTracks) { mediaComposition.BackgroundAudioTracks.Add(bgm); } } #endregion #region add watermark var watermarkLayer = provider.CreateWatermarkLayer(); if (watermarkLayer != null) { mediaComposition.OverlayLayers.Add(watermarkLayer); } #endregion var exportFile = this.DestinationFolder.CreateFileAsync($"{this.FileName}.mp4", CreationCollisionOption.ReplaceExisting) .GetAwaiter().GetResult(); if (Canceled) { return; } var saveOperation = mediaComposition.RenderToFileAsync(exportFile, MediaTrimmingPreference.Fast, mediaEncodingProfile); saveOperation.Progress = (info, progress) => { UpdateProgress((int)(50 + progress * 0.5)); if (Canceled) { saveOperation.Cancel(); } }; saveOperation.Completed = (info, status) => { if (!Canceled) { var results = info.GetResults(); if (results != TranscodeFailureReason.None || status != AsyncStatus.Completed) { UpdateExportStatus(false); } else { UpdateExportStatus(true); } } ClearCacheAsync(); }; }, cancellationToken)); }
private async Task ChatPhotoAsync(UpdateFileGenerationStart update, string[] args) { try { var conversion = JsonConvert.DeserializeObject <ChatPhotoConversion>(args[2]); var sticker = await _protoService.SendAsync(new GetFile(conversion.StickerFileId)) as Telegram.Td.Api.File; if (sticker == null || !sticker.Local.IsFileExisting()) { _protoService.Send(new FinishFileGeneration(update.GenerationId, new Error(500, "FILE_GENERATE_LOCATION_INVALID No sticker found"))); return; } Background background = null; var backgroundLink = await _protoService.SendAsync(new GetInternalLinkType(conversion.BackgroundUrl ?? string.Empty)) as InternalLinkTypeBackground; if (backgroundLink != null) { background = await _protoService.SendAsync(new SearchBackground(backgroundLink.BackgroundName)) as Background; } else { var freeform = new[] { 0xDBDDBB, 0x6BA587, 0xD5D88D, 0x88B884 }; background = new Background(0, true, false, string.Empty, new Document(string.Empty, "application/x-tgwallpattern", null, null, TdExtensions.GetLocalFile("Assets\\Background.tgv", "Background")), new BackgroundTypePattern(new BackgroundFillFreeformGradient(freeform), 50, false, false)); } if (background == null || (background.Document != null && !background.Document.DocumentValue.Local.IsFileExisting())) { _protoService.Send(new FinishFileGeneration(update.GenerationId, new Error(500, "FILE_GENERATE_LOCATION_INVALID No background found"))); return; } var device = CanvasDevice.GetSharedDevice(); var bitmaps = new List <CanvasBitmap>(); var sfondo = new CanvasRenderTarget(device, 640, 640, 96, DirectXPixelFormat.B8G8R8A8UIntNormalized, CanvasAlphaMode.Premultiplied); using (var session = sfondo.CreateDrawingSession()) { if (background.Type is BackgroundTypePattern pattern) { if (pattern.Fill is BackgroundFillFreeformGradient freeform) { var colors = freeform.GetColors(); var positions = new Vector2[] { new Vector2(0.80f, 0.10f), new Vector2(0.35f, 0.25f), new Vector2(0.20f, 0.90f), new Vector2(0.65f, 0.75f), }; using (var gradient = CanvasBitmap.CreateFromBytes(device, ChatBackgroundFreeform.GenerateGradientData(50, 50, colors, positions), 50, 50, DirectXPixelFormat.B8G8R8A8UIntNormalized)) using (var cache = await PlaceholderHelper.GetPatternBitmapAsync(device, null, background.Document.DocumentValue)) { using (var scale = new ScaleEffect { Source = gradient, BorderMode = EffectBorderMode.Hard, Scale = new Vector2(640f / 50f, 640f / 50f) }) using (var colorize = new TintEffect { Source = cache, Color = Color.FromArgb(0x76, 00, 00, 00) }) using (var tile = new BorderEffect { Source = colorize, ExtendX = CanvasEdgeBehavior.Wrap, ExtendY = CanvasEdgeBehavior.Wrap }) using (var effect = new BlendEffect { Foreground = tile, Background = scale, Mode = BlendEffectMode.Overlay }) { session.DrawImage(effect, new Rect(0, 0, 640, 640), new Rect(0, 0, 640, 640)); } } } } } bitmaps.Add(sfondo); var width = (int)(512d * conversion.Scale); var height = (int)(512d * conversion.Scale); var animation = await Task.Run(() => LottieAnimation.LoadFromFile(sticker.Local.Path, new Windows.Graphics.SizeInt32 { Width = width, Height = height }, false, null)); if (animation == null) { _protoService.Send(new FinishFileGeneration(update.GenerationId, new Error(500, "FILE_GENERATE_LOCATION_INVALID Can't load Lottie animation"))); return; } var composition = new MediaComposition(); var layer = new MediaOverlayLayer(); var buffer = ArrayPool <byte> .Shared.Rent(width *height * 4); var framesPerUpdate = animation.FrameRate < 60 ? 1 : 2; var duration = TimeSpan.Zero; for (int i = 0; i < animation.TotalFrame; i += framesPerUpdate) { var bitmap = CanvasBitmap.CreateFromBytes(device, buffer, width, height, DirectXPixelFormat.B8G8R8A8UIntNormalized); animation.RenderSync(bitmap, i); var clip = MediaClip.CreateFromSurface(bitmap, TimeSpan.FromMilliseconds(1000d / 30d)); var overlay = new MediaOverlay(clip, new Rect(320 - (width / 2d), 320 - (height / 2d), width, height), 1); overlay.Delay = duration; layer.Overlays.Add(overlay); duration += clip.OriginalDuration; bitmaps.Add(bitmap); } composition.OverlayLayers.Add(layer); composition.Clips.Add(MediaClip.CreateFromSurface(sfondo, duration)); var temp = await _protoService.GetFileAsync(update.DestinationPath); var profile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto); profile.Audio = null; profile.Video.Bitrate = 1800000; profile.Video.Width = 640; profile.Video.Height = 640; profile.Video.FrameRate.Numerator = 30; profile.Video.FrameRate.Denominator = 1; var progress = composition.RenderToFileAsync(temp, MediaTrimmingPreference.Precise, profile); progress.Progress = (result, delta) => { _protoService.Send(new SetFileGenerationProgress(update.GenerationId, 100, (int)delta)); }; var result = await progress; if (result == TranscodeFailureReason.None) { _protoService.Send(new FinishFileGeneration(update.GenerationId, null)); } else { _protoService.Send(new FinishFileGeneration(update.GenerationId, new Error(500, result.ToString()))); } ArrayPool <byte> .Shared.Return(buffer); foreach (var bitmap in bitmaps) { bitmap.Dispose(); } } catch (Exception ex) { _protoService.Send(new FinishFileGeneration(update.GenerationId, new Error(500, "FILE_GENERATE_LOCATION_INVALID " + ex.ToString()))); } }
private async void VideoGenTest2(object sender, RoutedEventArgs e) { var ep = SetVideoPlayer(); saveCompositionButton.IsEnabled = false; _composition = new MediaComposition(); mediaPlayerElement.Source = null; SessionCollection sessionCollection = new SessionCollection(); sessionCollection.sessions = new List <Session>(); var listItems = heatmapSessionsListView.SelectedItems.ToList(); foreach (Session s in listItems) { sessionCollection.sessions.Add(s); } HeatmapGenerator generator = new HeatmapGenerator(); generator.CheckHistoryErrors(sessionCollection); FillEffectPropertySet(sessionCollection); if (_mediaPlayer == null) { _mediaPlayer = new MediaPlayer(); } var video = await MediaClip.CreateFromFileAsync(_videoFile); MediaOverlayLayer videoOverlayLayer = new MediaOverlayLayer(); TrimVideo(ref video); ValuePairs.Add("offset", video.TrimTimeFromStart.Ticks); var enc = video.GetVideoEncodingProperties(); ValuePairs.Add("frameLength", (1 / ((double)enc.FrameRate.Numerator / enc.FrameRate.Denominator)) * 1000); _composition.Clips.Add(video); if (_horizonFlag) { _composition.OverlayLayers.Add(await GenerateHorizonLayer((int)video.TrimmedDuration.TotalSeconds, ep.Video.Height, ep.Video.Width)); } var videoEffectDefinition = new VideoEffectDefinition("VideoEffectComponent.HeatmapAddVideoEffect", ValuePairs); video.VideoEffectDefinitions.Add(videoEffectDefinition); MediaStreamSource res; try { ValuePairs.Remove("height"); ValuePairs.Remove("width"); ValuePairs.Add("height", ep.Video.Height); ValuePairs.Add("width", ep.Video.Width); res = _composition.GenerateMediaStreamSource(ep); var md = MediaSource.CreateFromMediaStreamSource(res); mediaPlayerElement.Source = md; } catch (Exception f) { Debug.WriteLine(f.Message); } _mediaPlayer = mediaPlayerElement.MediaPlayer; mediaPlayerElement.AreTransportControlsEnabled = true; saveCompositionButton.IsEnabled = true; }