public static Bitmap GetThumbnailFromVideo(Uri uri, long timeMs) { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.SetDataSource(uri.ToString()); return(retriever.GetFrameAtTime(timeMs * 1000)); }
/// <summary> /// /// </summary> /// <param name="dirPath"></param> /// <param name="fileName"></param> /// <param name="url">视频相对路径</param> /// <param name="usecond"></param> public void SaveThumbImage(string dirPath, string fileName, string url, long usecond) { if (string.IsNullOrWhiteSpace(dirPath) || string.IsNullOrWhiteSpace(url)) { return; } try { Java.IO.FileInputStream input = new FileInputStream(dirPath + url); MediaMetadataRetriever retriever = new MediaMetadataRetriever(); //retriever.SetDataSource(url, new Dictionary<string, string>()); retriever.SetDataSource(input.FD); Bitmap bitmap = retriever.GetFrameAtTime(usecond <= 0 ? 2 * 1000 * 1000 : usecond, Option.ClosestSync); if (bitmap != null) { MemoryStream stream = new MemoryStream(); bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream); byte[] bitmapData = stream.ToArray(); System.IO.File.WriteAllBytes(dirPath + fileName, bitmapData); } retriever.Release(); bitmap.Recycle(); } catch (Exception e) { System.Console.WriteLine("===error:" + e); } }
public async Task <IMediaModel> GenerateThumbImageFromVideo(DirectoryInfo argCurrentDataFolder, MediaModel argExistingMediaModel, IMediaModel argNewMediaModel) { IMediaModel returnValue = new MediaModel(); await Task.Run(() => { string outFilePath = System.IO.Path.Combine(argCurrentDataFolder.FullName, argNewMediaModel.OriginalFilePath); MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.SetDataSource(argExistingMediaModel.MediaStorageFilePath); Bitmap bitmap = retriever.GetFrameAtTime(1000); // try at 1 second as this is often a more flattering image if (bitmap != null) { //Save the bitmap var outStream = new FileStream(outFilePath, FileMode.Create); bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, outStream); outStream.Close(); returnValue = argNewMediaModel; } }); return(returnValue); }
private void GetVideoFrames(int viewWidth) { MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever(); try { mediaMetadataRetriever.SetDataSource(mVideoUri.ToString(), new Dictionary <string, string>()); // Retrieve media data long videoLengthInMs = Convert.ToInt64(mediaMetadataRetriever.ExtractMetadata(MetadataKey.Duration)) * 1000; // Set thumbnail properties (Thumbs are squares) int thumbWidth = mHeightView; int thumbHeight = mHeightView; int numThumbs = (int)Math.Ceiling(((float)viewWidth) / thumbWidth); long interval = videoLengthInMs / numThumbs; for (int i = 0; i < numThumbs; ++i) { Bitmap bitmap = mediaMetadataRetriever.GetFrameAtTime(i * interval, Android.Media.Option.ClosestSync); bitmap = Bitmap.CreateScaledBitmap(bitmap, thumbWidth, thumbHeight, false); mBitmapList.Add(bitmap); } } catch (Exception ex) { Log.Error("Error", ex.ToString()); } finally { mediaMetadataRetriever.Release(); } }
private byte[] GetImageSource(MediaMetadataRetriever retriever) { var bitmap = retriever.GetFrameAtTime(0); if (bitmap != null) { var stream = new MemoryStream(); bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream); byte[] bitmapData = stream.ToArray(); return(bitmapData); } return(null); }
public MemoryStream GenerateThumbImage(string url, long usecond) { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.SetDataSource(url); Bitmap bitmap = retriever.GetFrameAtTime(usecond); if (bitmap != null) { MemoryStream stream = new MemoryStream(); bitmap.Compress(Bitmap.CompressFormat.Jpeg, 10, stream); byte[] bitmapData = stream.ToArray(); return(stream); // return ImageSource.FromStream(() => new MemoryStream(bitmapData)); } return(null); }
public ImageSource GenerateThumbImage(string url, long usecond) { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.SetDataSource(url, new Dictionary <string, string>()); Bitmap bitmap = retriever.GetFrameAtTime(usecond); if (bitmap != null) { MemoryStream stream = new MemoryStream(); bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream); byte[] bitmapData = stream.ToArray(); return(ImageSource.FromStream(() => new MemoryStream(bitmapData))); } else { return(null); } }
public ImageSource GenerateThumbnailImageSource(string url, long usecond) { // var bitmap = ThumbnailUtils.CreateVideoThumbnail(url, Android.Provider.ThumbnailKind.MicroKind); // above implementation seems slower MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.SetDataSource(url); Bitmap bitmap = retriever.GetFrameAtTime(usecond); if (bitmap != null) { MemoryStream stream = new MemoryStream(); bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream); byte[] bitmapData = stream.ToArray(); return(ImageSource.FromStream(() => new MemoryStream(bitmapData))); } return(null); }
/// <summary> /// 初始化数据 /// </summary> private void InitData() { // 第一帧图片 Bitmap bitmap = null; // 获取视频第一帧 MediaMetadataRetriever retriever = new MediaMetadataRetriever(); try { Android.Net.Uri uri = Android.Net.Uri.Parse(url); if (Build.VERSION.SdkInt >= Build.VERSION_CODES.IceCreamSandwich) { retriever.SetDataSource(uri.ToString(), new Dictionary <string, string>()); } else { retriever.SetDataSource(uri.ToString()); } // 获取第一帧图片 bitmap = retriever.GetFrameAtTime(0, MediaMetadataRetriever.OptionClosest); imageView_VideoThumbnail.SetImageBitmap(bitmap); progressBar_VideoProgressBar.Visibility = ViewStates.Gone; imageView_VideoPlay.Visibility = ViewStates.Visible; imageView_VideoPause.Visibility = ViewStates.Gone; // 进度条 seekBar_VideoTotal = Convert.ToInt32(retriever.ExtractMetadata((int)MetadataKey.Duration)) / 1000; seekBar_VideoSeekBar.Max = Convert.ToInt32(retriever.ExtractMetadata((int)MetadataKey.Duration)) / 1000; seekBar_VideoSeekBar.Enabled = true; textView_VideoTotalTime.Text = (seekBar_VideoTotal / 60).ToString("00") + " : " + (seekBar_VideoTotal % 60).ToString("00"); retriever.Release(); } catch (Exception) { retriever.Release(); } }
public byte[] GenerateThumbImage(string url, long second) { using (MediaMetadataRetriever retriever = new MediaMetadataRetriever()) using (FileInputStream inputStream = new FileInputStream(url)) using (Bitmap bitmap = retriever.GetFrameAtTime(second)) { retriever.SetDataSource(inputStream.FD); if (bitmap != null) { using (var ms = new MemoryStream()) { bitmap.Compress(Bitmap.CompressFormat.Png, 0, ms); byte[] bitmapData = ms.ToArray(); return(bitmapData); } } } return(null); }
void HandleVideo(MediaFile file) { if (file == null) { return; } MediaMetadataRetriever m = new MediaMetadataRetriever(); m.SetDataSource(file.Path); var bitmap = m.GetFrameAtTime(1000); var documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal); var imageFile = Path.Combine(documentsDirectory, Guid.NewGuid().ToString() + ".jpg"); var outputStream = new FileStream(imageFile, FileMode.CreateNew); bitmap.Compress(Bitmap.CompressFormat.Jpeg, 80, outputStream); outputStream.Close(); bitmap.Recycle(); VideoPicked(file.Path, imageFile); }
void UpdateUI() { var uri = ((VideoImagePreview)Element).VideoUrl; var mediaMetadataRetriever = new MediaMetadataRetriever(); try { mediaMetadataRetriever.SetDataSource(uri, new Dictionary <string, string>()); using (var bitmap = mediaMetadataRetriever.GetFrameAtTime(0)) (Forms.Context as Activity).RunOnUiThread(() => ((ImageView)Control).SetImageBitmap(bitmap)); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); } finally { mediaMetadataRetriever?.Release(); } }
public ImageSource GenerateThumbImage(string savePath, string url, long usecond) { try { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.SetDataSource(url, new Dictionary <string, string>()); Bitmap bitmap = retriever.GetFrameAtTime(usecond <= 0 ? 2 * 1000 * 1000 : usecond, Option.ClosestSync); retriever.Release(); if (bitmap != null) { MemoryStream stream = new MemoryStream(); bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream); byte[] bitmapData = stream.ToArray(); return(ImageSource.FromStream(() => new MemoryStream(bitmapData))); } } catch (Exception) { return(null); } return(null); }
public string GenerateThumbnailAsPath(string url, long usecond, string appCachePath) { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.SetDataSource(url); Bitmap bitmap = retriever.GetFrameAtTime(usecond); if (bitmap != null) { int slashLastIndex = url.LastIndexOf('/'); int dotLastIndex = url.LastIndexOf('.'); int fileNameLength = url.Length - (url.Length - dotLastIndex) - slashLastIndex - 1; string fileName = url.Substring(slashLastIndex + 1, fileNameLength); string outputPath = System.IO.Path.Combine(appCachePath, fileName + ".png"); FileStream stream = new FileStream(outputPath, FileMode.CreateNew); bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream); stream.Flush(); stream.Close(); return(outputPath); } return(null); }