我有一个UWP项目想在播放视频时从Mediaelement拍摄快照。
有没有人知道任何有用的链接或如何处理这项任务?
答案 0 :(得分:2)
根据您的要求,您可以通过Custom video effects
实现这一目标。因为您可以使用ProcessFrame
方法获得每帧。您可以使用静态属性来存储当前帧并将其传递给图像控件。以下是RExampleVidoEffect
类。
public sealed class RExampleVidoEffect : IBasicVideoEffect
{
private static SoftwareBitmap Snap;
public void SetEncodingProperties(VideoEncodingProperties encodingProperties, IDirect3DDevice device)
{
}
public void ProcessFrame(ProcessVideoFrameContext context)
{
var inputFrameBitmap = context.InputFrame.SoftwareBitmap;
Snap = inputFrameBitmap;
}
public static SoftwareBitmap GetSnapShot()
{
return Snap;
}
public void Close(MediaEffectClosedReason reason)
{
}
public void DiscardQueuedFrames()
{
}
public bool IsReadOnly
{
get
{
return true;
}
}
public IReadOnlyList<VideoEncodingProperties> SupportedEncodingProperties
{
get { return new List<VideoEncodingProperties>(); }
}
public MediaMemoryTypes SupportedMemoryTypes
{
get { return MediaMemoryTypes.Cpu; }
}
public bool TimeIndependent
{
get { return true; }
}
public void SetProperties(IPropertySet configuration)
{
}
}
<强>用法强>
private async void VideoPlayer_Loaded(object sender, RoutedEventArgs e)
{
var videoFile = await Package.Current.InstalledLocation.GetFileAsync("big_buck_bunny.mp4");
MediaClip clip = await MediaClip.CreateFromFileAsync(videoFile);
var videoEffectDefinition = new VideoEffectDefinition(typeof(RExampleVidoEffect).FullName);
clip.VideoEffectDefinitions.Add(videoEffectDefinition);
MediaComposition compositor = new MediaComposition();
compositor.Clips.Add(clip);
this.VideoPlayer.SetMediaStreamSource(compositor.GenerateMediaStreamSource());
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
var bitmap = RExampleVidoEffect.GetSnapShot();
if (bitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 ||
bitmap.BitmapAlphaMode == BitmapAlphaMode.Straight)
{
bitmap = SoftwareBitmap.Convert(bitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
}
var source = new SoftwareBitmapSource();
await source.SetBitmapAsync(bitmap);
img.Source = source;
}
<强>效果强>