UWP:如何录制屏幕并保存为mp4文件?

标签 uwp video-capture

我已经弄清楚如何使用此处的示例捕获屏幕并将其输出回预览器:Microsoft Screen Capture Documentation

就目前而言,这很好。

我无法弄清楚,也找不到任何文档是获取这些帧并将它们写入视频文件。

理想情况下,我想将它们直接流式传输到 mp4 或类似文件中,然后我可以稍后使用 MediaComposition 系统进行编辑。

我找到了 VideoFrame.CreateWithDirect3D11Surface,但我不知道如何将视频帧添加到现有视频文件。该文档告诉您如何创建视频帧以及属性如何工作,但它没有告诉您如何在视频文件中使用视频帧,我也找不到有关如何在没有相机或其他捕获设备。

GitHub 文档中也有一些引用资料,其中有人问同样的问题,他们说 MediaStreamSample 是关键,但也没有任何代码,当然也没有允许保存文件的代码。 ( Here's the issue )

人们会认为使用此 API 录制屏幕并将这些帧转储为原始视频文件会很容易,然后您可以将其导入并使用 MediaComposition API 进行编辑。

请帮忙!

最佳答案

假设您正在使用 Microsoft 的屏幕捕获示例。以下是您需要执行的操作:

此处提供了完整的源代码(代码确实需要重构,但它可以工作🤷‍♂️):https://gitlab.com/colinkiama/screenrecordertest/-/tree/master/screenRecorderTest

代码是使用屏幕捕获示例和部分 SimpleRecorder 的源代码:https://github.com/robmikh/SimpleRecorder/tree/master/SimpleRecorder

  1. 创建一个包含输入详细信息的 MediaStreamSource
private void CreateMediaObjects()
 {
            // Describe our input: uncompressed BGRA8 buffers
            var videoProperties = VideoEncodingProperties.CreateUncompressed(MediaEncodingSubtypes.Bgra8, _sourceWidth, _sourceHeight);
            _videoDescriptor = new VideoStreamDescriptor(videoProperties);


            // Create our MediaStreamSource
            _mediaStreamSource = new MediaStreamSource(_videoDescriptor);
            _mediaStreamSource.BufferTime = TimeSpan.FromSeconds(0);
            _mediaStreamSource.Starting += OnMediaStreamSourceStarting;
            _mediaStreamSource.SampleRequested += OnMediaStreamSourceSampleRequested;

            // Create our transcoder
            _transcoder = new MediaTranscoder();
            _transcoder.HardwareAccelerationEnabled = true;

        }

  1. 设置编码,开始截屏并开始转码(注:stream是指从StorageFile对象打开的stream)
private async Task EncodeInternalAsync(IRandomAccessStream stream, uint width, uint height, uint bitrateInBps, uint frameRate)
        {
            if (!_isRecording)
            {
                _isRecording = true;

                var encodingProfile = new MediaEncodingProfile();
                encodingProfile.Container.Subtype = "MPEG4";
                encodingProfile.Video.Subtype = "H264";
                encodingProfile.Video.Width = width;
                encodingProfile.Video.Height = height;
                encodingProfile.Video.Bitrate = bitrateInBps;
                encodingProfile.Video.FrameRate.Numerator = frameRate;
                encodingProfile.Video.FrameRate.Denominator = 1;
                encodingProfile.Video.PixelAspectRatio.Numerator = 1;
                encodingProfile.Video.PixelAspectRatio.Denominator = 1;

                StartFrameCapture();

                var transcode = await _transcoder.PrepareMediaStreamSourceTranscodeAsync(_mediaStreamSource, stream, encodingProfile);
                await transcode.TranscodeAsync();
            }
        }
  1. 对于从屏幕捕获到达的每个帧,创建一个 MediaStream 样本
using (var frame = _framePool.TryGetNextFrame())
                {
                    MediaStreamSample sampleToUseLater = MediaStreamSample.CreateFromDirect3D11Surface(frame.Surface, frame.SystemRelativeTime);

                    lock (doubleBufferingPool)
                    {
                        while (doubleBufferingPool.Count >= 2)
                        {
                            // Stops too many samples from being saved
                            doubleBufferingPool.Dequeue();
                        }

                        doubleBufferingPool.Enqueue(sampleToUseLater);
                    }
                }
  1. 设置流的实际开始时间:
 private void OnMediaStreamSourceStarting(MediaStreamSource sender, MediaStreamSourceStartingEventArgs args)
        {

            while (doubleBufferingPool.Count == 0)
            {

            }
            var sample = doubleBufferingPool.Dequeue();
            TimeSpan timeStamp = sample.Timestamp;
            args.Request.SetActualStartPosition(timeStamp);
        }
  1. 向 MediaStreamSource 提供样本
private void OnMediaStreamSourceSampleRequested(MediaStreamSource sender, MediaStreamSourceSampleRequestedEventArgs args)
        {
            if (_isRecording && !_closed)
            {
                while (doubleBufferingPool.Count == 0)
                {

                }

                lock (doubleBufferingPool)
                {
                    args.Request.Sample = doubleBufferingPool.Dequeue();
                }
            }
            else
            {
                args.Request.Sample = null;
                StopCapture();
            }
        }

如果您为 MediaStreamSource 提供空样本,它将停止请求更多样本。

然后可以从您从中打开流的文件中查看视频。

关于UWP:如何录制屏幕并保存为mp4文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53070359/

相关文章:

c# - 启动 MvvmCross Uwp 应用程序时出现异常 : "System.AccessViolationException: Attempted to read or write protected memory"

opencv - OpenCV 中的 GStreamer API : autovideosink vs appsink

android - 捕获运行 android 应用程序的视频,然后播放它

android - 视频录制开启时,每 5 秒间隔保存一次视频(Android 操作系统)

python - cv2.VideoWriter 不会使用 fourcc h.264 写入文件(使用 logitech c920、python 2.7、windows 8)

opencv - 使用 javacv 和 ffmpeg 或备用库创建 1080p 视频

azure - Multi-Tenancy 应用程序 - OneDrive Business API

c++ - VS 2017 & 2019 找不到 WinRT 头文件,即使是在新的解决方案上

uwp - 在 UWP 中旋转、缩放和平移元素

uwp - 不断收到 BLE UWP Unreachable 错误