public async Task <MediaComposition> TimeBasedReconstruction(Stream aedatFile, CameraParameters cam, EventColor onColor, EventColor offColor, int frameTime, int maxFrames, float playback_frametime)
        {
            byte[]           aedatBytes  = new byte[5 * Convert.ToInt32(Math.Pow(10, 8))];  // Read 0.5 GB at a time
            MediaComposition composition = new MediaComposition();
            int    lastTime = -999999;
            int    timeStamp;
            int    frameCount  = 0;
            Stream pixelStream = InitBitMap(cam);

            byte[] currentFrame = new byte[pixelStream.Length];

            int bytesRead = aedatFile.Read(aedatBytes, 0, aedatBytes.Length);

            while (bytesRead != 0 && frameCount < maxFrames)
            {
                // Read through AEDAT file
                for (int i = 0, length = bytesRead; i < length; i += AedatUtilities.dataEntrySize)                    // iterate through file, 8 bytes at a time.
                {
                    AEDATEvent currentEvent = new AEDATEvent(aedatBytes, i, cam);

                    AedatUtilities.SetPixel(ref currentFrame, currentEvent.x, currentEvent.y, (currentEvent.onOff ? onColor.Color : offColor.Color), cam.cameraX);
                    timeStamp = currentEvent.time;

                    if (lastTime == -999999)
                    {
                        lastTime = timeStamp;
                    }
                    else
                    {
                        if (lastTime + frameTime <= timeStamp)                         // Collected events within specified timeframe, add frame to video
                        {
                            WriteableBitmap b = new WriteableBitmap(cam.cameraX, cam.cameraY);
                            using (Stream stream = b.PixelBuffer.AsStream())
                            {
                                await stream.WriteAsync(currentFrame, 0, currentFrame.Length);
                            }

                            SoftwareBitmap outputBitmap = SoftwareBitmap.CreateCopyFromBuffer(b.PixelBuffer, BitmapPixelFormat.Bgra8, b.PixelWidth, b.PixelHeight, BitmapAlphaMode.Ignore);
                            CanvasBitmap   bitmap2      = CanvasBitmap.CreateFromSoftwareBitmap(CanvasDevice.GetSharedDevice(), outputBitmap);

                            // Set playback framerate
                            MediaClip mediaClip = MediaClip.CreateFromSurface(bitmap2, TimeSpan.FromSeconds(playback_frametime));

                            composition.Clips.Add(mediaClip);
                            frameCount++;

                            // Stop adding frames to video if max frames has been reached
                            if (frameCount >= maxFrames)
                            {
                                break;
                            }
                            currentFrame = new byte[pixelStream.Length];
                            lastTime     = timeStamp;
                        }
                    }
                }
                bytesRead = aedatFile.Read(aedatBytes, 0, aedatBytes.Length);
            }
            return(composition);
        }
Пример #2
0
        /// <summary>
        /// TODO: need to dispose CanvasRenderTarget.
        /// </summary>
        /// <param name="canvasDevice"></param>
        /// <param name="canvasBitmap"></param>
        private void CreateClipInMemory(CanvasDevice canvasDevice, CanvasBitmap canvasBitmap)
        {
            CanvasRenderTarget rendertarget = null;

            QueryPerformanceCounter(out long counter);
            var currentTime = TimeSpan.FromMilliseconds(1000f * counter / performanceFrequency);

            try
            {
                rendertarget = new CanvasRenderTarget(canvasDevice, canvasBitmap.SizeInPixels.Width, canvasBitmap.SizeInPixels.Height, canvasBitmap.Dpi, canvasBitmap.Format, canvasBitmap.AlphaMode);
                using (CanvasDrawingSession ds = rendertarget.CreateDrawingSession())
                {
                    ds.Clear(Colors.Transparent);
                    ds.DrawImage(canvasBitmap);
                }
                mediaComposition.Clips.Add(MediaClip.CreateFromSurface(rendertarget, currentTime - _timeSpan));
            }
            catch
            {
            }
            finally
            {
                _timeSpan = currentTime;
            }
        }
Пример #3
0
        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);
        }
        //This works
        private async void CreateVideoFromIDX3D()
        {
            for (int i = 0; i < 5; i++)
            {
                try
                {
                    var folder = ApplicationData.Current.LocalFolder;
                    RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
                    await renderTargetBitmap.RenderAsync(RenderGrid);

                    IBuffer pixels = await renderTargetBitmap.GetPixelsAsync();

                    CanvasBitmap bitmap    = null;
                    var          videoClip = new MediaComposition();
                    //SoftwareBitmap softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, renderTargetBitmap.PixelWidth,
                    //    renderTargetBitmap.PixelHeight);
                    //bitmap = CanvasBitmap.CreateFromSoftwareBitmap(CanvasDevice.GetSharedDevice(), softwareBitmap);

                    bitmap = CanvasBitmap.CreateFromBytes(CanvasDevice.GetSharedDevice(), pixels,
                                                          renderTargetBitmap.PixelWidth, renderTargetBitmap.PixelHeight, DirectXPixelFormat.B8G8R8A8UIntNormalized);
                    StorageFile video2 = await folder.CreateFileAsync("video2" + ".mp4", CreationCollisionOption.ReplaceExisting);

                    MediaClip d = MediaClip.CreateFromSurface(bitmap, TimeSpan.FromSeconds(3));
                    videoClip.Clips.Add(d);

                    //Use these code to work
                    await videoClip.SaveAsync(video2);

                    videoClip = await MediaComposition.LoadAsync(video2);

                    var result = await videoClip.RenderToFileAsync(video2);

                    Debug.WriteLine(result.ToString());
                    break;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
        }
        private async void CreateVideoFromWritableBitmapAsync(List <WriteableBitmap> WBs)
        {
            var mediaComposition = new MediaComposition();

            foreach (WriteableBitmap WB in WBs)
            {
                var          pixels = WB.PixelBuffer.ToArray();
                CanvasBitmap bitmap = CanvasBitmap.CreateFromBytes(CanvasDevice.GetSharedDevice(), pixels,
                                                                   800, 600, DirectXPixelFormat.B8G8R8A8UIntNormalized);
                MediaClip mediaClip = MediaClip.CreateFromSurface(bitmap, TimeSpan.FromSeconds(1));
                mediaComposition.Clips.Add(mediaClip);
            }
            //Save the video
            var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("WBVideo.mp4");

            await mediaComposition.SaveAsync(file);

            mediaComposition = await MediaComposition.LoadAsync(file);

            await mediaComposition.RenderToFileAsync(file);
        }
Пример #6
0
 private void PrepareVideo(WriteableBitmap bmp, bool EN)
 {
     if (EN)
     {
         SoftwareBitmap     frame        = SoftwareBitmap.CreateCopyFromBuffer(bmp.PixelBuffer, BitmapPixelFormat.Bgra8, bmp.PixelWidth, bmp.PixelHeight, BitmapAlphaMode.Premultiplied);
         CanvasRenderTarget rendertarget = null;
         using (CanvasBitmap canvas = CanvasBitmap.CreateFromSoftwareBitmap(CanvasDevice.GetSharedDevice(), frame))
         {
             rendertarget = new CanvasRenderTarget(CanvasDevice.GetSharedDevice(), canvas.SizeInPixels.Width, canvas.SizeInPixels.Height, 96);
             using (CanvasDrawingSession ds = rendertarget.CreateDrawingSession())
             {
                 ds.Clear(Windows.UI.Colors.Black);
                 ds.DrawImage(canvas);
             }
         }
         thisframe = DateTime.Now;
         MediaClip m = MediaClip.CreateFromSurface(rendertarget, thisframe - lastframe);
         lastframe = thisframe;
         composition.Clips.Add(m);
     }
 }
        private async Task <MediaClip> CreateMediaClip(TimeSpan originalDuration)
        {
            var rendertargetBitmap = new RenderTargetBitmap();
            await rendertargetBitmap.RenderAsync(control);

            var bfr = await rendertargetBitmap.GetPixelsAsync();

            CanvasRenderTarget rendertarget;

            using (var canvas = CanvasBitmap.CreateFromBytes(CanvasDevice.GetSharedDevice(), bfr,
                                                             rendertargetBitmap.PixelWidth, rendertargetBitmap.PixelHeight,
                                                             DirectXPixelFormat.B8G8R8A8UIntNormalized))
            {
                rendertarget = new CanvasRenderTarget(CanvasDevice.GetSharedDevice(), canvas.SizeInPixels.Width,
                                                      canvas.SizeInPixels.Height, 96);
                using (var ds = rendertarget.CreateDrawingSession())
                {
                    ds.DrawImage(canvas);
                }
            }

            return(MediaClip.CreateFromSurface(rendertarget, originalDuration));
        }
Пример #8
0
        public async Task UnpackToMp4()
        {
            MediaComposition mediacomposition = new MediaComposition();

            StorageFolder pictureFolder = KnownFolders.SavedPictures;

            {
                int len = simpleRecorder.unpackList.Count;
                for (int i = 0; i < len; i++)
                {
                    UnpackItem unpackItem = simpleRecorder.unpackList[i];
                    Windows.Storage.Streams.Buffer buffer = unpackItem.compressedBuffer;

                    InMemoryRandomAccessStream inMemoryRandomAccessStream = null;
                    using (inMemoryRandomAccessStream = new InMemoryRandomAccessStream())
                    {
                        await inMemoryRandomAccessStream.WriteAsync(buffer);

                        await inMemoryRandomAccessStream.FlushAsync();

                        inMemoryRandomAccessStream.Seek(0);
                        CanvasBitmap tempBitmap = await CanvasBitmap.LoadAsync(CanvasDevice.GetSharedDevice(), inMemoryRandomAccessStream);

                        if (tempBitmap != null)
                        {
                            CanvasRenderTarget canvasRenderTarget = new CanvasRenderTarget(CanvasDevice.GetSharedDevice(), tempBitmap.SizeInPixels.Width, tempBitmap.SizeInPixels.Height, 96);
                            using (CanvasDrawingSession session = canvasRenderTarget.CreateDrawingSession())
                            {
                                session.Clear(Colors.Black);
                                session.DrawImage(tempBitmap);

                                TimeSpan frameTime30Mil = TimeSpan.FromMilliseconds(30f);
                                TimeSpan frameTime      = unpackItem.frameTime;
                                //if (frameTime < frameTime30Mil)
                                //    frameTime = frameTime30Mil;

                                MediaClip mediaclip = MediaClip.CreateFromSurface(canvasRenderTarget, frameTime);
                                mediacomposition.Clips.Add(mediaclip);
                            }

                            string str = "Adding Clips " + (i + 1).ToString() + " / " + len.ToString();
                            if (i == len - 1)
                            {
                                str += "  ...  Please wait for file rendering  ...";
                            }
                            TextOutput.Text = str;
                        }
                    }

                    //free up memory recources as each frame is unpacked
                    if (unpackItem.compressedBuffer != null)
                    {
                        unpackItem.compressedBuffer = null;
                    }
                } //for
            }

            StorageFile mp4file = null;

            if (simpleRecorder.savefile != null)
            {
                mp4file = simpleRecorder.savefile;
            }
            else
            {
                string mp4filename = "SavedVideo" + ".mp4";
                mp4file = await pictureFolder.CreateFileAsync(
                    mp4filename,
                    CreationCollisionOption.ReplaceExisting);
            }

            //await mediacomposition.RenderToFileAsync(mp4file, MediaTrimmingPreference.Precise, MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD720p));
            var rendering = mediacomposition.RenderToFileAsync(mp4file, MediaTrimmingPreference.Precise, MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD720p));

            rendering.Progress  += ProgressReport;
            rendering.Completed += CompletedReport;
        }
Пример #9
0
        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));
        }
Пример #10
0
        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
                   ));
        }
Пример #11
0
        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));
        }
Пример #12
0
        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())));
            }
        }
Пример #13
0
        private void ProcessFrame(Direct3D11CaptureFrame frame)
        {
            // Resize and device-lost leverage the same function on the
            // Direct3D11CaptureFramePool. Refactoring it this way avoids
            // throwing in the catch block below (device creation could always
            // fail) along with ensuring that resize completes successfully and
            // isn’t vulnerable to device-lost.
            bool needsReset     = false;
            bool recreateDevice = false;

            if ((frame.ContentSize.Width != _lastSize.Width) ||
                (frame.ContentSize.Height != _lastSize.Height))
            {
                needsReset = true;
                _lastSize  = frame.ContentSize;
                _swapChain.ResizeBuffers(_lastSize.Width, _lastSize.Height);
            }
            Direct3D11CaptureFrame direct = frame;

            try
            {
                // Take the D3D11 surface and draw it into a
                // Composition surface.
                if (direct.SystemRelativeTime - lastFrameTime < TimeSpan.FromSeconds(1))
                {
                    //F**k Microsoft🤬
                    MediaClip mediaClip = MediaClip.CreateFromSurface(direct.Surface, direct.SystemRelativeTime - lastFrameTime);
                    composition.Clips.Add(mediaClip);
                }
                lastFrameTime = direct.SystemRelativeTime;

                // Convert our D3D11 surface into a Win2D object.
                canvasBitmap = CanvasBitmap.CreateFromDirect3D11Surface(
                    _canvasDevice,
                    direct.Surface);

                using (var drawingSession = _swapChain.CreateDrawingSession(Colors.Transparent))
                {
                    //drawingSession.DrawCircle(400, 300, 100, Colors.Red, 20);
                    ScaleEffect effect = new ScaleEffect()
                    {
                        Source = canvasBitmap,
                        Scale  = new Vector2((float)swapChain.ActualWidth / _item.Size.Width)
                    };

                    drawingSession.DrawImage(effect);
                }

                _swapChain.Present();

                //canvasControl.Invalidate();
                // Helper that handles the drawing for us, not shown.
            }
            // This is the device-lost convention for Win2D.
            catch (Exception e) when(_canvasDevice.IsDeviceLost(e.HResult))
            {
                // We lost our graphics device. Recreate it and reset
                // our Direct3D11CaptureFramePool.
                needsReset     = true;
                recreateDevice = true;
            }
            if (needsReset)
            {
                ResetFramePool(direct.ContentSize, recreateDevice);
            }
        }