Exemplo n.º 1
0
        /*
         * Windows.Media.Effects.VideoTransformEffectDefinition videoEffect =
         *      new Windows.Media.Effects.VideoTransformEffectDefinition();
         *
         * videoEffect.Mirror = Windows.Media.MediaProperties.MediaMirroringOptions.Vertical;
         *  videoEffect.OutputSize = new Size(mediaElement.ActualWidth, mediaElement.ActualHeight);
         */

        // <SnippetSaveComposition>
        private async Task SaveComposition()
        {
            var picker = new Windows.Storage.Pickers.FileSavePicker();

            picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.VideosLibrary;
            picker.FileTypeChoices.Add("Composition files", new List <string>()
            {
                ".cmp"
            });
            picker.SuggestedFileName = "SavedComposition";

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

            if (compositionFile == null)
            {
                ShowErrorMessage("User cancelled the file selection");
            }
            else
            {
                var action = composition.SaveAsync(compositionFile);
                action.Completed = (info, status) =>
                {
                    if (status != AsyncStatus.Completed)
                    {
                        ShowErrorMessage("Error saving composition");
                    }
                };
            }
        }
Exemplo n.º 2
0
        private async Task SetComp(StorageFile backgroundAudio, Dictionary <StorageFile, TimeSpan> imageFiles, CancellationToken cancellationToken)
        {
            StorageFile temporaryFile = await CreateFile("Media Composition", "temp (1)", "cmp", cancellationToken);

            BackgroundAudioTrack backgroundAudioTrack = await BackgroundAudioTrack.CreateFromFileAsync(backgroundAudio);

            AudioEncodingProperties audioEncodingProperties = backgroundAudioTrack.GetAudioEncodingProperties();
            TimeSpan         duration    = backgroundAudioTrack.OriginalDuration;
            MediaComposition composition = new MediaComposition();

            composition.BackgroundAudioTracks.Add(backgroundAudioTrack);

            CancelTask(cancellationToken);

            MediaClip clip;

            foreach (var item in imageFiles)
            {
                CancelTask(cancellationToken);
                clip = await MediaClip.CreateFromImageFileAsync(item.Key, item.Value);

                composition.Clips.Add(clip);
            }
            composition.CreateDefaultEncodingProfile();
            await composition.SaveAsync(temporaryFile);

            ReportProgress(Stage5, Stage4ProgressBar, TextProgress4, 100);

            CancelTask(cancellationToken);
        }
        private async void SaveCompositionToFile(MediaComposition composition, string suggestedFileName, uint vidX, uint vidY)
        {
            var savePicker = new FileSavePicker
            {
                SuggestedStartLocation = PickerLocationId.DocumentsLibrary
            };

            // Dropdown of file types the user can save the file as
            savePicker.FileTypeChoices.Add("MP4", new List <string>()
            {
                ".mp4"
            });
            // Default file name if the user does not type one in or select a file to replace
            savePicker.SuggestedFileName = suggestedFileName;

            // Get name and location for saved video file
            StorageFile sampleFile = await savePicker.PickSaveFileAsync();

            if (sampleFile == null)
            {
                backgroundTint.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                showLoading.IsActive      = false;
                return;
            }

            await composition.SaveAsync(sampleFile);

            composition = await MediaComposition.LoadAsync(sampleFile);

            // Get a generic encoding profile and match the width and height to the camera's width and height
            MediaEncodingProfile _MediaEncodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD720p);

            _MediaEncodingProfile.Video.Width  = vidX;
            _MediaEncodingProfile.Video.Height = vidY;

            var saveOperation = composition.RenderToFileAsync(sampleFile, MediaTrimmingPreference.Precise, _MediaEncodingProfile);

            //mediaSimple.Source = new Uri("ms-appx:///WBVideo.mp4");
            saveOperation.Progress = new AsyncOperationProgressHandler <TranscodeFailureReason, double>(async(info, progress) =>
            {
                await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(() =>
                {
                    Debug.WriteLine(progress);
                    //ShowErrorMessage(string.Format("Saving file... Progress: {0:F0}%", progress));
                }));
            });
            saveOperation.Completed = new AsyncOperationWithProgressCompletedHandler <TranscodeFailureReason, double>(async(info, status) =>
            {
                await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(async() =>
                {
                    backgroundTint.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    showLoading.IsActive      = false;
                    await videoExportCompleteDialog.ShowAsync();
                }));
            });
        }
        //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);
        }
Exemplo n.º 6
0
        public async Task MergeMedias(List<Media> medias, MediaGroup currentMediaGroup = null)
        {

            // Use Profile for output vid/cmp? file 


            try
            {
                List<StorageFile> mediaFiles = new List<StorageFile>();
                MediaGroup mediaGroup = new MediaGroup();
                StorageFolder localFolder = ApplicationData.Current.LocalFolder;
                MediaComposition composition = new MediaComposition();
                List<MediaClip> mediaClips = new List<MediaClip>();
                StorageFile timelineVidOutputFile;
                StorageFile timelineCMPOutputFile;
                bool deleteExistingFile = (currentMediaGroup != null) ? false : true;
                #region EmotionsMeanTemp

                double tmpAngerScoreMean = 0;
                double tmpContemptScoreMean = 0;
                double tmpDisgustScoreMean = 0;
                double tmpFearScoreMean = 0;
                double tmpHappinessScoreMean = 0;
                double tmpNeutralScoreMean = 0;
                double tmpSadnessScoreMean = 0;
                double tmpSurpriseScoreMean = 0;

                string tmpHighestEmotionMean = string.Empty;

                #endregion

                foreach (var media in medias)
                {

                    mediaFiles.Add(await StorageFile.GetFileFromApplicationUriAsync(new Uri(
                            string.Format("ms-appdata:///local/{0}", media.MediaName))));

                    tmpAngerScoreMean += media.AngerScore;
                    tmpContemptScoreMean += media.ContemptScore;
                    tmpDisgustScoreMean += media.DisgustScore;
                    tmpFearScoreMean += media.FearScore;
                    tmpHappinessScoreMean += media.HappinessScore;
                    tmpNeutralScoreMean += media.NeutralScore;
                    tmpSadnessScoreMean += media.SadnessScore;
                    tmpSurpriseScoreMean += media.SurpriseScore;
                }
                tmpAngerScoreMean = tmpAngerScoreMean / medias.Count;
                tmpContemptScoreMean = tmpContemptScoreMean / medias.Count;
                tmpDisgustScoreMean = tmpDisgustScoreMean / medias.Count;
                tmpFearScoreMean = tmpFearScoreMean / medias.Count;
                tmpHappinessScoreMean = tmpHappinessScoreMean / medias.Count;
                tmpNeutralScoreMean = tmpNeutralScoreMean / medias.Count;
                tmpSadnessScoreMean = tmpSadnessScoreMean / medias.Count;
                tmpSurpriseScoreMean = tmpSurpriseScoreMean / medias.Count;

                SortedList<string, double> tempForSortingScores = new SortedList<string, double>();
                tempForSortingScores.Add("AngerScoreMean", tmpAngerScoreMean);
                tempForSortingScores.Add("ContemptScoreMean", tmpContemptScoreMean);
                tempForSortingScores.Add("DisgustScoreMean", tmpDisgustScoreMean);
                tempForSortingScores.Add("FearScoreMean", tmpFearScoreMean);
                tempForSortingScores.Add("HappinessScoreMean", tmpHappinessScoreMean);
                tempForSortingScores.Add("NeutralScoreMean", tmpNeutralScoreMean);
                tempForSortingScores.Add("SadnessScoreMean", tmpSadnessScoreMean);
                tempForSortingScores.Add("SurpriseScoreMean", tmpSurpriseScoreMean);
                // TODO: check this.
                tmpHighestEmotionMean = tempForSortingScores.OrderByDescending(x => x.Value).FirstOrDefault().Key;

                if (currentMediaGroup == null)
                {
                    timelineVidOutputFile = await localFolder.CreateFileAsync(
                   "timelineMeOutput.mp4", CreationCollisionOption.GenerateUniqueName);

                    string CMPFileName = string.Format(timelineVidOutputFile.DisplayName + ".cmp");
                    timelineCMPOutputFile = await localFolder.CreateFileAsync(
                   CMPFileName, CreationCollisionOption.OpenIfExists);

                    for (int i = 0; i < mediaFiles.Count; i++)
                    {
                        mediaClips.Add(await MediaClip.CreateFromImageFileAsync(mediaFiles[i], TimeSpan.FromSeconds(int.Parse(localSettings.Values["DurationInSecForEachImage"].ToString()))));
                        composition.Clips.Add(mediaClips[i]);
                    }


                    using (var db = new MediaContext())
                    {
                        mediaGroup = new MediaGroup()
                        {
                            CompostionFileName = timelineVidOutputFile.DisplayName,
                            LastEditDate = DateTime.Now,
                            AngerScoreMean = tmpAngerScoreMean,
                            ContemptScoreMean = tmpContemptScoreMean,
                            DisgustScoreMean = tmpDisgustScoreMean,
                            FearScoreMean = tmpFearScoreMean,
                            HappinessScoreMean = tmpHappinessScoreMean,
                            NeutralScoreMean = tmpNeutralScoreMean,
                            SadnessScoreMean = tmpSadnessScoreMean,
                            SurpriseScoreMean = tmpSurpriseScoreMean,
                            HighestEmotionMean = tmpHighestEmotionMean

                        };

                        db.MediaGroups.Add(mediaGroup);
                        db.SaveChanges();

                    }

                    var action = composition.SaveAsync(timelineCMPOutputFile);
                    action.Completed = (info, status) =>
                    {
                        if (status != AsyncStatus.Completed)
                        {
                        //ShowErrorMessage("Error saving composition");
                    }

                    };



                }
                else
                {
                    using (var db = new MediaContext())
                    {
                        mediaGroup =
                            db.MediaGroups.Where(
                                x => x.CompostionFileName == currentMediaGroup.CompostionFileName).
                                FirstOrDefault();
                        // TODO Check this
                        mediaGroup.LastEditDate = DateTime.Now;
                        mediaGroup.AngerScoreMean = mediaGroup.AngerScoreMean + tmpAngerScoreMean / 2;
                        mediaGroup.ContemptScoreMean = mediaGroup.ContemptScoreMean + tmpContemptScoreMean / 2;
                        mediaGroup.DisgustScoreMean = mediaGroup.DisgustScoreMean + tmpDisgustScoreMean / 2;
                        mediaGroup.FearScoreMean = mediaGroup.FearScoreMean + tmpFearScoreMean / 2;
                        mediaGroup.HappinessScoreMean = mediaGroup.HappinessScoreMean + tmpHappinessScoreMean / 2;
                        mediaGroup.NeutralScoreMean = mediaGroup.NeutralScoreMean + tmpNeutralScoreMean / 2;
                        mediaGroup.SadnessScoreMean = mediaGroup.SadnessScoreMean + tmpSadnessScoreMean / 2;
                        mediaGroup.SurpriseScoreMean = mediaGroup.SurpriseScoreMean + tmpSurpriseScoreMean / 2;

                        SortedList<string, double> temptempForSortingScores = new SortedList<string, double>();
                        temptempForSortingScores.Add("AngerScoreMean", mediaGroup.AngerScoreMean);
                        temptempForSortingScores.Add("ContemptScoreMean", mediaGroup.ContemptScoreMean);
                        temptempForSortingScores.Add("DisgustScoreMean", mediaGroup.DisgustScoreMean);
                        temptempForSortingScores.Add("FearScoreMean", mediaGroup.FearScoreMean);
                        temptempForSortingScores.Add("HappinessScoreMean", mediaGroup.HappinessScoreMean);
                        temptempForSortingScores.Add("NeutralScoreMean", mediaGroup.NeutralScoreMean);
                        temptempForSortingScores.Add("SadnessScoreMean", mediaGroup.SadnessScoreMean);
                        temptempForSortingScores.Add("SurpriseScoreMean", mediaGroup.SurpriseScoreMean);
                        // TODO: check this.
                        mediaGroup.HighestEmotionMean = temptempForSortingScores.OrderByDescending(x => x.Value).FirstOrDefault().Key;

                        db.MediaGroups.Update(mediaGroup);
                        db.SaveChanges();
                    }

                    timelineVidOutputFile = await localFolder.GetFileAsync(currentMediaGroup.CompostionFileName + ".mp4");
                    timelineCMPOutputFile = await localFolder.GetFileAsync(currentMediaGroup.CompostionFileName + ".cmp");
                    composition = await MediaComposition.LoadAsync(timelineCMPOutputFile);
                    //TODO: make sure this works.
                    for (int i = 0; i < mediaFiles.Count; i++)
                    {
                        // TODO Fix this
                        mediaClips.Add(await MediaClip.CreateFromImageFileAsync(mediaFiles[i], TimeSpan.FromSeconds(int.Parse(localSettings.Values["DurationInSecForEachImage"].ToString()))));
                        composition.Clips.Add(mediaClips[i]);
                    }

                    var action = composition.SaveAsync(timelineCMPOutputFile);
                    action.Completed = (info, status) =>
                    {
                        if (status != AsyncStatus.Completed)
                        {
                        //ShowErrorMessage("Error saving composition");
                    }

                    };
                }
            }
            catch (Exception)
            {

                
            }

        }