如何从视频中读取帧作为UWP中的位图 [英] How to read frames from a video as bitmaps in UWP

查看:380
本文介绍了如何从视频中读取帧作为UWP中的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在通用Windows应用程序中加载视频并从中提取单个帧(如图像)?

Is it possible to load a video and extract single frames from it (as images) in Universal Windows Applications?

推荐答案


可以在通用Windows应用程序中加载视频并从中提取单个帧(如图像)?

Is it possible to load a video and extract single frames from it (as images) in Universal Windows Applications?

您可以使用 MediaComposition.GetThumbnailAsync 从视频中获取图像流。然后,您可以使用 RandomAccessStream.CopyAsync 进行转换 IInputStream InMemoryRandomAccessStream 。我们可以添加IRandomAccessStream来设置 BitmapSource.SetSource

You can use MediaComposition.GetThumbnailAsync to get an image stream from the video. Then you can use RandomAccessStream.CopyAsync to convert the IInputStream to InMemoryRandomAccessStream. We can add the IRandomAccessStream to set BitmapSource.SetSource.

例如:

private async void Button_Click(object sender, RoutedEventArgs e)
{
    FileOpenPicker openPicker = new FileOpenPicker();
    foreach (string extension in FileExtensions.Video)
    {
        openPicker.FileTypeFilter.Add(extension);
    }
    StorageFile file = await openPicker.PickSingleFileAsync();
    var thumbnail = await GetThumbnailAsync(file);
    BitmapImage bitmapImage = new BitmapImage();
    InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
    await RandomAccessStream.CopyAsync(thumbnail, randomAccessStream);
    randomAccessStream.Seek(0);
    bitmapImage.SetSource(randomAccessStream);
    MyImage.Source = bitmapImage;
}

public async Task<IInputStream> GetThumbnailAsync(StorageFile file)
{
    var mediaClip = await MediaClip.CreateFromFileAsync(file);
    var mediaComposition = new MediaComposition();
    mediaComposition.Clips.Add(mediaClip);
    return await mediaComposition.GetThumbnailAsync(
        TimeSpan.FromMilliseconds(5000), 0, 0, VideoFramePrecision.NearestFrame);
}

internal class FileExtensions
{
    public static readonly string[] Video = new string[] { ".mp4", ".wmv" };
}

这篇关于如何从视频中读取帧作为UWP中的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆