是否可以在通用Windows应用程序中加载视频并从中提取单帧(如图像)?
答案 0 :(得分:3)
是否可以在通用Windows应用程序中加载视频并从中提取单帧(如图像)?
您可以使用MediaComposition.GetThumbnailAsync从视频中获取图片流。然后,您可以使用RandomAccessStream.CopyAsync将IInputStream转换为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)
有两个名称空间允许您获取帧: