在UWP C ++ / CX项目中,我尝试从MediaCapture Camera Stream的每一帧截取屏幕截图,并在最后将视频流保存为视频。我的方法:
/// <summary>
/// Records an MP4 video to a StorageFile and adds rotation metadata to it
/// </summary>
/// <returns></returns>
task<void> CameraControl::StartRecordingAsyncStream()
{
//InMemoryRandomAccessStream^ video_stream;
// Calculate rotation angle, taking mirroring into account if necessary
auto rotationAngle = CameraRotationHelper::ConvertSimpleOrientationToClockwiseDegrees(_rotationHelper->GetCameraCaptureOrientation());
auto encodingProfile = MediaProperties::MediaEncodingProfile::CreateMp4(MediaProperties::VideoEncodingQuality::Auto);
encodingProfile->Video->Properties->Insert(RotationKey, rotationAngle);
return create_task(_mediaCapture->StartRecordToStreamAsync(encodingProfile, this->video_stream))
.then([this]()
{
_isRecording = true;
WriteLine("Started recording");
}).then([this](task<void> previousTask)
{
try
{
previousTask.get();
}
catch (Exception^ ex)
{
// File I/O errors are reported as exceptions
WriteLine(ex->Message);
}
});
}
/// <summary>
/// Stops recording a video
/// </summary>
/// <returns></returns>
task<void> CameraControl::StopRecordingAsyncStream()
{
_isRecording = false;
WriteLine("Stopping recording...");
return create_task(_mediaCapture->StopRecordAsync())
.then([this]()
{
WriteLine("Stopped recording!");
});
}
//Loads all frames as images from the stream
void CameraControl::LoadVideoStream(void) {
//TODO: Read all Frames as Thumbnails from "this->video_stream" and store them as Bitmaps, for example in a vector
}
/// <summary>
/// Stores the recorded stream as a MP4 video
/// </summary>
/// <returns></returns>
task<void> CameraControl::StoreRecordingAsync()
{
// Create storage file for the capture
return create_task(_captureFolder->CreateFileAsync("SimpleVideo.mp4", CreationCollisionOption::GenerateUniqueName))
.then([this](StorageFile^ file)
{
//TODO: Write the "this->video_stream" as a MP4 video to the file
});
}
对于第一个问题,我需要像MP4Decoder那样可以转换流(类似于CapturePhotoToStreamAsync的BitmapDecoder)
关于第二个问题:我知道有一个功能&#34; StartRecordToStorageFileAsync&#34;。但这不是解决方案,因为保存视频可选择在最后。我不想保存每个视频。
谢谢!
Corono
修改
我尝试实现具有FrameArrived事件的Media Frame Reader。
public : IAsyncOperation<MediaFrameReader> CreateFrameReaderAsync(MediaFrameSource inputSource)
在PC上运行良好,但Win10移动设备上没有找到MediaFrameSource。不知道为什么。
答案 0 :(得分:0)
通过MediaCapture
,您可以访问SoftwareBitmap
中<div [innerHTML]="some_string"></div>
的各个帧。我这样做是为了处理C#中的单个帧,但它应该同样适用于C ++。我的代码基于此示例:https://docs.microsoft.com/en-gb/windows/uwp/audio-video-camera/process-media-frames-with-mediaframereader