Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

.net - How to read frames from a video as bitmaps in UWP

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.

For example:

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" };
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...