【问题标题】:How to read frames from a video as bitmaps in UWP如何在 UWP 中将视频中的帧作为位图读取
【发布时间】:2016-09-15 00:52:53
【问题描述】:

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

【问题讨论】:

    标签: .net video ffmpeg uwp


    【解决方案1】:

    有两个命名空间可让您获取帧:

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

    【讨论】:

      【解决方案2】:

      是否可以在通用 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" };
      }
      

      【讨论】:

        猜你喜欢
        • 2021-08-15
        • 2022-01-26
        • 2014-08-24
        • 2019-04-07
        • 2014-06-12
        • 1970-01-01
        • 2012-11-17
        • 2011-07-11
        • 1970-01-01
        相关资源
        最近更新 更多