Example #1
0
        private MediaEncodingProfile SetVideoPlayer()
        {
            MediaEncodingProfile result;

            uint width  = (uint)mediaPlayerElement.ActualWidth;
            uint height = (uint)mediaPlayerElement.ActualHeight;

            if (width % 2 != 0)
            {
                width = width - 1;
            }

            if (height % 2 != 0)
            {
                height = height - 1;
            }

            var enc = _video.GetVideoEncodingProperties();

            if (width / height == enc.Width / enc.Height)
            {
                result = GetMediaEncoding(new Resolutions(new SavingResolutions {
                    Height = height, Width = width
                }, false), enc);
                mediaPlayerElement.Width  = width;
                mediaPlayerElement.Height = height;
            }
            else
            {
                double temp      = (double)enc.Width / enc.Height;
                uint   newHeight = (uint)(width / temp);

                if (newHeight % 2 != 0)
                {
                    newHeight = newHeight - 1;
                }

                result = GetMediaEncoding(new Resolutions(new SavingResolutions {
                    Width = width, Height = newHeight
                }, false), enc);
                mediaPlayerElement.Width  = width;
                mediaPlayerElement.Height = newHeight;
            }

            //mediaPlayerElement.Width = width;
            //result.Video.Height = height;
            //result.Video.Width = width;

            return(result);
        }
Example #2
0
        /**
         * ソース動画ファイルのサイズにあわせて、EncodingProfileに、いい感じのサイズを設定する(HD720用)
         */
        public static MediaEncodingProfile SetFeelGoodSizeToEncodingProfileForHD720(MediaClip clip, MediaEncodingProfile profile)
        {
            // 動画のサイズ情報取得
            var    prop = clip.GetVideoEncodingProperties();
            double width = prop.Width, height = prop.Height;
            double numerator = prop.PixelAspectRatio.Numerator, denominator = prop.PixelAspectRatio.Denominator;

            // PixelAspectRatio の考慮
            if ((numerator != 1 || denominator != 1) && numerator != 0 && denominator != 0)
            {
                width = width * numerator / denominator;
            }

            // - 短いほうの辺の長さが、720以下となり、かつ、
            // - 長いほうの辺の長さが、1280以下となるように、縮小。(拡大はしない)
            double r;

            if (width > height)
            {
                // 横長
                r = Math.Min(1280.0 / width, 720.0 / height);
            }
            else
            {
                // 縦長
                r = Math.Min(720.0 / width, 1280.0 / height);
            }
            if (r > 1)
            {
                r = 1;  // 拡大はしない
            }
            profile.Video.Width  = (uint)Math.Round(width * r);
            profile.Video.Height = (uint)Math.Round(height * r);
            return(profile);
        }
Example #3
0
        private async void SelectedVideo_Drop(object sender, DragEventArgs e)
        {
            if (e.DataView.Contains(StandardDataFormats.StorageItems))
            {
                var items = await e.DataView.GetStorageItemsAsync();

                if (items.Count > 0)
                {
                    var file = items[0] as StorageFile;
                    if (file.ContentType == "video/mp4" || file.ContentType == "video/x-matroska")
                    {
                        _videoFile = file;
                        selectedVideoFileTextBlock.Text = _videoFile.DisplayName;
                        _video = await MediaClip.CreateFromFileAsync(_videoFile);

                        SetTimeSliders(_video.OriginalDuration);
                        GenerateButtonEnable();
                        var enc = _video.GetVideoEncodingProperties();
                        Resolutions = new SavingResolutionsCollection(enc);
                        saveResolutionSelector.ItemsSource   = Resolutions;
                        saveResolutionSelector.SelectedIndex = 0;

                        AllowToWatchVideoBeforeGenerating(true);
                    }
                }
            }
            GenerateButtonEnable();
        }
Example #4
0
        private async void addVideoButton_Click(object sender, RoutedEventArgs e)
        {
            StorageFile file;
            IReadOnlyList <StorageFile> files;

            files = await LoadFile("single", ".mp4");

            if (files.Count != 0)
            {
                file = files[0];
                if (file.ContentType == "video/mp4")
                {
                    _videoFile = file;
                    selectedVideoFileTextBlock.Text = _videoFile.DisplayName;
                    _video = await MediaClip.CreateFromFileAsync(_videoFile);

                    SetTimeSliders(_video.OriginalDuration);
                    GenerateButtonEnable();
                    var enc = _video.GetVideoEncodingProperties();
                    Resolutions = new SavingResolutionsCollection(enc);
                    saveResolutionSelector.ItemsSource   = Resolutions;
                    saveResolutionSelector.SelectedIndex = 0;

                    AllowToWatchVideoBeforeGenerating(true);
                }
            }
        }
Example #5
0
        public void AddClip(MediaClip clip)
        {
            var          aspect  = clip.GetVideoEncodingProperties().PixelAspectRatio;
            MediaOverlay overlay = new MediaOverlay(clip, new Windows.Foundation.Rect(20, 20, 400, 400), 1);

            mediaOverlayLayer.Overlays.Add(overlay);
            AddMediaClip?.Invoke(this);
        }
Example #6
0
        public async Task <IFrameSource> Encode(StorageFile source)
        {
            MediaClip clip = await MediaClip.CreateFromFileAsync(source);

            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);

            var frameSource = new FrameSet(fps, (int)props.Width, (int)props.Height);

            TimeSpan frameDuration  = TimeSpan.FromMilliseconds((int)fps);
            TimeSpan frameStartTime = new TimeSpan();

            MediaComposition composition = new MediaComposition();

            composition.Clips.Add(clip);

            for (int idx = 0; idx < frameCount; idx++)
            {
                var time = TimeSpan.FromMilliseconds(idx * frameMilliseconts);

                var frame = await composition.GetThumbnailAsync(
                    time,
                    (int)props.Width,
                    (int)props.Height,
                    VideoFramePrecision.NearestFrame
                    );

                using (var stream = new InMemoryRandomAccessStream())
                {
                    await RandomAccessStream.CopyAsync(frame, stream);

                    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

                    stream.Seek(0);

                    SoftwareBitmap bitmap = SoftwareBitmap.Convert(
                        await decoder.GetSoftwareBitmapAsync(),
                        BitmapPixelFormat.Rgba16,
                        BitmapAlphaMode.Premultiplied);

                    frameSource.AddFrame(new Frame(bitmap)
                    {
                        Duration = frameDuration, StartTime = frameStartTime
                    });
                    frameStartTime += frameDuration;
                }
            }

            return(frameSource);
        }
Example #7
0
        // <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);
        }
Example #8
0
        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);
        }
Example #9
0
        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);
                }
            }
        }