Пример #1
0
        private async void SaveVideo_Click(object sender, RoutedEventArgs e)
        {
            TokenSource.Dispose();
            TokenSource = new CancellationTokenSource();
            Token       = TokenSource.Token;

            var temp = saveResolutionSelector.SelectedItem as Resolutions;
            var enc  = _video.GetVideoEncodingProperties();

            MediaEncodingProfile mediaEncoding = GetMediaEncoding(temp, enc);

            Debug.WriteLine("Vid type: " + enc.Type);
            Debug.WriteLine("Vid sub: " + enc.Subtype);
            Debug.WriteLine("Vid id: " + enc.ProfileId);

            var picker = new Windows.Storage.Pickers.FileSavePicker();

            picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.VideosLibrary;
            picker.FileTypeChoices.Add("MP4 files", new List <string>()
            {
                ".mp4"
            });
            picker.SuggestedFileName = "RenderedVideo.mp4";
            SaveProgressCallback saveProgress = ShowErrorMessage;

            Windows.Storage.StorageFile file = await picker.PickSaveFileAsync();

            if (file != null)
            {
                _mediaPlayer.Pause();

                ValuePairs.Remove("height");
                ValuePairs.Remove("width");

                ValuePairs.Add("height", temp.Resolution.Height);
                ValuePairs.Add("width", temp.Resolution.Width);

                if (_dotsFlag)
                {
                    ValuePairs.Remove("dotsRadius");
                    ValuePairs.Add("dotsRadius", (float)temp.Resolution.Width / 4096 * 20);
                }

                if (_horizonFlag)
                {
                    _composition.OverlayLayers[0] = await GenerateHorizonLayer((int)_video.TrimmedDuration.TotalSeconds, temp.Resolution.Height, temp.Resolution.Width);
                }

                //buttonLoadingStop.Visibility = Visibility.Visible;
                generateVideoButton.IsEnabled   = false;
                saveCompositionButton.IsEnabled = false;

                HeatmapGenerator generator = new HeatmapGenerator();

                generator.RenderCompositionToFile(file, _composition, saveProgress, Window.Current, mediaEncoding, Token, saveResolutionSelector.SelectedItem);
            }
        }
Пример #2
0
        public void RenderCompositionToFile(StorageFile file, MediaComposition composition, SaveProgressCallback showErrorMessage, Window window, MediaEncodingProfile encodingProfile, CancellationToken token, object selectedResolution)
        {
            var saveOperation = composition.RenderToFileAsync(file, MediaTrimmingPreference.Precise, encodingProfile);

            saveOperation.Progress = async(info, progress) =>
            {
                await window.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    showErrorMessage(progress);
                    try
                    {
                        if (token.IsCancellationRequested)
                        {
                            saveOperation.Cancel();
                            showErrorMessage(100.0);
                        }
                    }
                    catch (OperationCanceledException)
                    {
                    }
                });
            };

            saveOperation.Completed = async(info, status) =>
            {
                await window.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
                {
                    if (saveOperation.Status != AsyncStatus.Canceled)
                    {
                        try
                        {
                            var results = info.GetResults();
                            if (results != TranscodeFailureReason.None || status != AsyncStatus.Completed)
                            {
                                //ShowErrorMessage("Saving was unsuccessful");
                            }
                            else
                            {
                                //ShowErrorMessage("Trimmed clip saved to file");
                                await Launcher.LaunchFileAsync(file);
                            }
                        }
                        catch (Exception e)
                        {
                            Debug.WriteLine("Saving exception: " + e.Message);
                            var dialog   = new MessageDialog("Saving exception: " + e.Message);
                            dialog.Title = "Error";
                            dialog.Commands.Add(new UICommand {
                                Label = "OK", Id = 0
                            });
                            await dialog.ShowAsync();
                            showErrorMessage(100.0);
                        }
                    }
                });
            };
        }