如何将视频中的帧读取为UWP中的位图

时间:2016-05-18 13:34:42

标签: .net video ffmpeg uwp

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

2 个答案:

答案 0 :(得分:3)

  

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

您可以使用MediaComposition.GetThumbnailAsync从视频中获取图片流。然后,您可以使用RandomAccessStream.CopyAsyncIInputStream转换为InMemoryRandomAccessStream。我们可以添加IRandomAccessStream来设置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" };
}

答案 1 :(得分:3)

有两个名称空间允许您获取帧:

  1. &#34; Windows.Media.Capture&#34;命名空间。如果你想从相机中捕获视频,请使用它然后阅读&#34;使用MediaFrameReader处理媒体帧&#34; https://docs.microsoft.com/en-us/windows/uwp/audio-video-camera/process-media-frames-with-mediaframereader
  2. &#34; Windows.Media.Playback&#34;命名空间。如果您想从文件或流视频中获取帧,请使用它。滚动到段落&#34;在帧服务器模式下使用MediaPlayer&#34;在https://docs.microsoft.com/en-us/windows/uwp/audio-video-camera/play-audio-and-video-with-mediaplayer