我已经弄清楚了如何使用以下示例捕获屏幕并将其输出回预览器:Microsoft Screen Capture Documentation
就目前而言还可以。
我不知道,也找不到任何文档是将这些帧取下来并将它们写入视频文件。
理想情况下,我想将它们直接流到mp4或类似文件中,然后我可以在以后使用MediaComposition系统进行编辑。
我找到了VideoFrame.CreateWithDirect3D11Surface,但是我不知道如何将视频帧添加到现有的视频文件中。该文档介绍了如何创建视频帧以及属性如何工作,但没有告诉您如何在视频文件中使用视频帧,也没有找到关于如何在没有照相机或摄像机的情况下创建视频文件的任何信息。其他捕获设备。
从GitHub文档中也有一些参考,有人问他们相同的地方,他们说MediaStreamSample是关键,但是也没有任何代码,当然也没有任何允许保存文件的代码。 (Here's the issue)
人们会认为,使用此api录制屏幕并将这些帧转储到原始视频文件会很容易,然后您可以使用MediaComposition api引入这些视频并进行编辑。
请帮助!
答案 0 :(得分:1)
假设您正在使用Microsoft的Screen Capture示例。这是您需要做的:
此处提供了完整的源代码(该代码确实需要重构,但可以使用?♂️):https://gitlab.com/colinkiama/screenrecordertest/-/tree/master/screenRecorderTest
该代码是使用截屏示例以及SimpleRecorder的部分源代码:https://github.com/robmikh/SimpleRecorder/tree/master/SimpleRecorder
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;
}
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();
}
}
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);
}
}
private void OnMediaStreamSourceStarting(MediaStreamSource sender, MediaStreamSourceStartingEventArgs args)
{
while (doubleBufferingPool.Count == 0)
{
}
var sample = doubleBufferingPool.Dequeue();
TimeSpan timeStamp = sample.Timestamp;
args.Request.SetActualStartPosition(timeStamp);
}
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提供的样本为空,则它将停止索取更多样本。
然后,您可以从打开流的文件中观看视频。