public Task <DataItem> GetData() { // Use this to create design time data DataItem item = new DataItem("Welcome to MVVM Light [design testing]"); item.Clip = MediaClip.CreateFromColor(Color.FromArgb(byte.MaxValue, byte.MinValue, byte.MaxValue, byte.MinValue), TimeSpan.FromSeconds(5)); return(Task.FromResult(item)); }
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 void Page_Loaded(object sender, RoutedEventArgs e) { MediaList.ItemsSource = MediaClipList; //Main:主1 AddColor(Colors.Black, false); AddColor(Colors.White, false); App.Model.MediaComposition.OverlayLayers.Add(new MediaOverlayLayer()); App.Model.MediaComposition.OverlayLayers.Add(new MediaOverlayLayer()); MediaClip clip = MediaClip.CreateFromColor(Colors.White, new TimeSpan(24, 1, 0)); clip.TrimTimeFromEnd = clip.TrimTimeFromStart = new TimeSpan(11, 58, 0); App.Model.MediaComposition.Clips.Add(clip); }
private void AddColor(Color co, bool isAnumal = true) //从颜色创建媒体剪辑 { //新建媒体 Media media = new Media(); //从颜色创建剪辑 MediaClip clip = MediaClip.CreateFromColor(co, new TimeSpan(24, 1, 0)); clip.TrimTimeFromEnd = clip.TrimTimeFromStart = new TimeSpan(12, 0, 0); media.Clip = clip; //从颜色创建笔刷 media.Brush = new SolidColorBrush(co); //从颜色创建名称 media.Name = "#" + 剪片.Library.Method.ColorToString(co); MediaClipList.Add(media); if (isAnumal) { SplitShowMethod(); // SplitView:侧栏 } }
public async Task <MediaComposition> GetMediaComposition(StorageFile source) { MediaClip clip = await MediaClip.CreateFromFileAsync(source); clip.VideoEffectDefinitions.Add( new Windows.Media.Effects.VideoEffectDefinition(typeof(MForge.VideoEffects.BlurEffect).FullName) ); var props = clip.GetVideoEncodingProperties(); var frameMilliseconts = 1000.0f / ((float)props.FrameRate.Numerator / (float)props.FrameRate.Denominator); var frameCount = (int)(clip.OriginalDuration.TotalMilliseconds / frameMilliseconts); var fps = MediaHelper.MillisecondsToFPS((long)frameMilliseconts); MediaComposition composition = new MediaComposition(); MediaClip wait = MediaClip.CreateFromColor(Colors.Black, TimeSpan.FromSeconds(2)); composition.Clips.Add(wait); composition.Clips.Add(clip); return(composition); }
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)); }