How to read frames from video as bitmaps in UWP

Can I upload a video and extract individual frames (like images) from Universal Windows Applications?

+2
source share
2 answers

There are two namespaces that allow you to receive frames:

+2
source

Can I upload a video and extract individual frames (like images) from Universal Windows Applications?

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

For instance:

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" }; } 
+2
source

Source: https://habr.com/ru/post/1213282/


All Articles