public OperationResult <Stream> GetFirstVideoFrame(string filePath) { var url = NSUrl.CreateFileUrl(filePath, false, null); var asset = new AVUrlAsset(url); var imageGenerator = new AVAssetImageGenerator(asset); imageGenerator.AppliesPreferredTrackTransform = true; CMTime actualTime; NSError error; var cgImage = imageGenerator.CopyCGImageAtTime(new CMTime(0, 1), out actualTime, out error); if (error != null) { return(OperationResult <Stream> .AsFailure(error.ToString())); } if (cgImage != null) { return(OperationResult <Stream> .AsSuccess(new UIImage(cgImage).AsJPEG().AsStream())); } else { return(OperationResult <Stream> .AsFailure("Image generation failed")); } }
public static bool TryGenerateVideoThumbnail2(string localFile, CGSize size, out UIImage image, string extensionHint = ".mov") { image = null; try { const int secondToGet = 1; const int timeScale = 60; var asset = AVAsset.FromUrl(NSUrl.FromFilename(localFile)); var generator = new AVAssetImageGenerator(asset); var time = new CMTime(secondToGet, timeScale); CMTime actualTime; NSError error; var cgImage = generator.CopyCGImageAtTime(time, out actualTime, out error); if (error == null) { image = new UIImage(cgImage); image = UIImageExtensions.ResizeAndDispose(image, size, ResizeMethod.AspectFill, ResizeAlignment.CenterCenter ); return(true); } } catch { } return(false); }
internal static async Task <StorageItemThumbnail> CreateVideoThumbnailAsync(StorageFile file) { AVAsset asset = AVUrlAsset.FromUrl(NSUrl.FromFilename(file.Path)); AVAssetImageGenerator generator = AVAssetImageGenerator.FromAsset(asset); generator.AppliesPreferredTrackTransform = true; NSError error; CMTime actualTime; CMTime time = CMTime.FromSeconds(asset.Duration.Seconds / 2, asset.Duration.TimeScale); CGImage image = generator.CopyCGImageAtTime(time, out actualTime, out error); #if __MAC__ NSMutableData buffer = new NSMutableData(); CGImageDestination dest = CGImageDestination.Create(buffer, UTType.JPEG, 1, null); dest.AddImage(image); return(new StorageItemThumbnail(buffer.AsStream())); #else UIImage image2 = UIImage.FromImage(image); image.Dispose(); UIImage image3 = image2.Scale(new CGSize(240, 240)); image2.Dispose(); return(new StorageItemThumbnail(image3.AsJPEG().AsStream())); #endif }
private void syncedChanged() { if (media == null || media.URL == null) { return; } if (media.URL.Substring(media.URL.Length - 3, 3) == "png") { using (Stream stream = AppDelegate.MomentsManager.FileSystem.getFileStream(media.URL)) { NSData data = NSData.FromStream(stream); this.imageView.Image = UIImage.LoadFromData(data); AppDelegate.MomentsManager.FileSystem.CloseFileStream(stream); } } else { var nsurl = NSUrl.FromFilename((Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/" + media.URL)); AVAsset asset = AVAsset.FromUrl(nsurl); AVAssetImageGenerator generator = new AVAssetImageGenerator(asset); generator.AppliesPreferredTrackTransform = true; NSError err = null; CMTime outTime = new CMTime(); CMTime requestedTime = new CMTime(2, 1); // To create thumbnail image using (var imgRef = generator.CopyCGImageAtTime(requestedTime, out outTime, out err)){ this.imageView.Image = UIImage.FromImage(imgRef); } } }
public byte[] GenerateThumbImage(string url, long second) { AVAssetImageGenerator imageGenerator = new AVAssetImageGenerator(AVAsset.FromUrl((new Foundation.NSUrl(url)))); imageGenerator.AppliesPreferredTrackTransform = true; CMTime actualTime; NSError error; CGImage cgImage = imageGenerator.CopyCGImageAtTime(new CMTime(second, 1000000), out actualTime, out error); return(new UIImage(cgImage).AsPNG().ToArray()); }
public ImageSource GenerateThumbImage(string url, long usecond) { AVAssetImageGenerator imageGenerator = new AVAssetImageGenerator(AVAsset.FromUrl((new Foundation.NSUrl(url)))); imageGenerator.AppliesPreferredTrackTransform = true; CMTime actualTime; NSError error; CGImage cgImage = imageGenerator.CopyCGImageAtTime(new CMTime(usecond, 1000000), out actualTime, out error); return(ImageSource.FromStream(() => new UIImage(cgImage).AsPNG().AsStream())); //ImageSource.FromStream(() => new UIImage(cgImage).AsPNG().AsStream()); }
UIImage ImageFor(AVAsset avAsset, double time) { AVAssetImageGenerator imageGenerator = AVAssetImageGenerator.FromAsset(avAsset); imageGenerator.AppliesPreferredTrackTransform = true; CMTime actualTime; NSError error = null; var requestedTime = new CMTime((long)time, 100); using (CGImage posterImage = imageGenerator.CopyCGImageAtTime(requestedTime, out actualTime, out error)) return(UIImage.FromImage(posterImage)); }
public void CopyCGImageAtTime_Invalid() { // Mov file is not supported by CopCGImageAtTime so we can test out error param using (NSUrl video_url = NSUrl.FromFilename(does_not_exists_asset_path)) using (AVAsset video_asset = AVAsset.FromUrl(video_url)) using (AVAssetImageGenerator aig = new AVAssetImageGenerator(video_asset)) { // signature errors see https://bugzilla.xamarin.com/show_bug.cgi?id=5218 CMTime actual; NSError error; var img = aig.CopyCGImageAtTime(CMTime.Zero, out actual, out error); Assert.Null(img, "missing"); Assert.True(actual.IsInvalid, "actual"); Assert.NotNull(error, "error"); } }
public static UIImage GetVideoThumbnail(this AVAsset asset) { return(CoreUtility.ExecuteFunction("GetVideoThumbnail", delegate() { AVAssetImageGenerator generator = new AVAssetImageGenerator(asset); generator.AppliesPreferredTrackTransform = true; CMTime ignoreTime = new CMTime(); NSError error = null; CGImage image = generator.CopyCGImageAtTime(new CMTime(1, 1), out ignoreTime, out error); if (error != null) { Container.Track.LogError(error.ConvertToException(), "GetVideoThumbnail"); } return new UIImage(image); })); }
// Video thumbnail code: // https://forums.xamarin.com/discussion/61903/code-snippet-how-to-generate-video-thumbnail public void UpdateContent(string filePath) { this.filePath = filePath; string cache = Common.LocalData.Storage.GetCacheFolder(); string extension = Path.GetExtension(filePath); ImageView.BackgroundColor = UIColor.White; ImageView.ContentMode = UIViewContentMode.ScaleAspectFill; if (extension == ".mov") { // Get thumbnail AVAsset asset = AVAsset.FromUrl(NSUrl.FromFilename(Path.Combine(cache, filePath))); CMTime time = new CMTime(1, 1); using (AVAssetImageGenerator imageGenerator = AVAssetImageGenerator.FromAsset(asset)) { imageGenerator.AppliesPreferredTrackTransform = true; using (CGImage imageRef = imageGenerator.CopyCGImageAtTime(time, out CMTime actualTime, out NSError err)) { if (err != null || imageRef == null) { Console.WriteLine("Error loading thumbnail: " + err?.LocalizedDescription); return; } var thumbnail = UIImage.FromImage(imageRef); ImageView.Image = thumbnail; } } } else if ((new List <string> { ".m4a", ".mp3", ".wav" }).Contains(extension)) { ImageView.Image = UIImage.FromBundle("AudioIcon"); ImageView.BackgroundColor = UIColor.FromRGB(76, 175, 80); ImageView.ContentMode = UIViewContentMode.Center; } else { ImageService.Instance.LoadFile(Path.Combine(cache, filePath)).Into(ImageView); } }
private static UIImage GetVideoThumbnail(AVAssetImageGenerator generator, double seconds, CGSize cellSize) { var time = new CMTime(Clamp(1, 0, (int)seconds), 1); CMTime actual; NSError error; using (var cgImage = generator.CopyCGImageAtTime(time, out actual, out error)) using (var uiImage = new UIImage(cgImage)) { var scaledImage = uiImage.ScaleImage(cellSize, UiKitExtensions.UIImageScaleMode.AspectFill, UiKitExtensions.UIImageAlignment.Center, true); return(scaledImage); } }
public Stream GetThumbnail(string pathMediaPath) { CMTime actualTime; NSError outError; try { using (var asset = AVAsset.FromUrl(NSUrl.FromFilename(pathMediaPath))) using (var imageGen = new AVAssetImageGenerator(asset)) { imageGen.AppliesPreferredTrackTransform = true; using (var imageRef = imageGen.CopyCGImageAtTime(new CMTime(1, 1), out actualTime, out outError)) { return(UIImage.FromImage(imageRef).AsPNG().AsStream()); } } } catch (Exception exc) { return(null); } }
/// <summary> /// Creates a thumbnail image from a local video /// </summary> /// <param name="localVideoPath">Full path of the video file, that should exist in the local storage</param> /// <returns>A stream containing the thumbnail bitmap</returns> public Stream CreateVideoThumbnail(string localFilePath) { CoreMedia.CMTime actualTime; NSError outError; using (var asset = AVAsset.FromUrl(NSUrl.FromFilename(localFilePath))) using (var imageGen = new AVAssetImageGenerator(asset)) using (var imageRef = imageGen.CopyCGImageAtTime(new CoreMedia.CMTime(1, 1), out actualTime, out outError)) { if (imageRef == null) { return(null); } var image = UIImage.FromImage(imageRef); return(image.AsPNG().AsStream()); } }
private UIImage GetVideoThumbnailIOS(string path) { try { CMTime actualTime; NSError outError; using (var asset = AVAsset.FromUrl(NSUrl.FromFilename(path))) using (var imageGen = new AVAssetImageGenerator(asset)) using (var imageRef = imageGen.CopyCGImageAtTime(new CMTime(1, 1), out actualTime, out outError)) { return(UIImage.FromImage(imageRef)); } } catch { return(null); } }
private byte[] GetImageSource(AVAsset asset) { using (var imageGenerator = new AVAssetImageGenerator(asset)) using (var imageRef = imageGenerator.CopyCGImageAtTime(new CMTime(1, 1), out var actualTime, out var error)) { if (imageRef == null) { return(null); } var image = UIImage.FromImage(imageRef); using (NSData imageData = image.AsPNG()) { var myByteArray = new byte[imageData.Length]; System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, myByteArray, 0, Convert.ToInt32(imageData.Length)); return(myByteArray); } } }
public byte[] GetVideoThumbnail(string path) { try { CMTime actualTime; NSError outError; using (var asset = AVAsset.FromUrl(NSUrl.FromString(path))) using (var imageGen = new AVAssetImageGenerator(asset)) using (var imageRef = imageGen.CopyCGImageAtTime(new CMTime(1, 1), out actualTime, out outError)) { return(RotateImage(UIImage.FromImage(imageRef))); //return UIImage.FromImage(imageRef).AsPNG().ToArray(); } } catch { return(null); } }
public UIImage Generate(CGSize size) { var asset = new AVUrlAsset(Url); var generator = new AVAssetImageGenerator(asset) { AppliesPreferredTrackTransform = true, MaximumSize = new CGSize(size.Width * UIScreen.MainScreen.Scale, size.Height * UIScreen.MainScreen.Scale) }; var actualTime = new CMTime(0, 1000); NSError error = null; var cgImage = generator.CopyCGImageAtTime(new CMTime(1, 1000), out actualTime, out error); if (error == null) { return(null); } return(new UIImage(cgImage)); }
public void Config(Moment moment) { this.moment = moment; this.TextLabel.Text = moment.Comment; this.DetailTextLabel.Text = moment.Title; if (moment.Media.Count > 0) { this.icon.Hidden = true; NSData data = null; this.ImageView.Layer.MasksToBounds = true; if (moment.Media [0].Type == "Image") { try { using (Stream imageStream = AppDelegate.MomentsManager.FileSystem.getFileStream(moment.Media [0].URL)) { data = NSData.FromStream(imageStream); AppDelegate.MomentsManager.FileSystem.CloseFileStream(imageStream); } this.ImageView.Image = UIImage.LoadFromData(data); } catch (Exception ex) { Console.WriteLine(ex); } } else { var nsurl = NSUrl.FromFilename((Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/" + moment.Media [0].URL)); AVAsset asset = AVAsset.FromUrl(nsurl); AVAssetImageGenerator generator = new AVAssetImageGenerator(asset); generator.AppliesPreferredTrackTransform = true; NSError err = null; CMTime outTime = new CMTime(); CMTime requestedTime = new CMTime(2, 1); // To create thumbnail image using (var imgRef = generator.CopyCGImageAtTime(requestedTime, out outTime, out err)){ this.ImageView.Image = UIImage.FromImage(imgRef); } } } else { this.ImageView.Image = null; } }
// Create a video thumbnail preview for the video public static async Task createVideoThumbnailAsync(string videoFilePath) { #if __IOS__ // Extract name of video file, use same name for video thumbnail file string videoFileName = System.IO.Path.GetFileNameWithoutExtension(videoFilePath); string videoThumbnailFilePath = System.IO.Path.Combine( FilePaths.allVideoThumbnailsPath, videoFileName + ".jpg"); await Task.Run(() => { // Adapted from : https://forums.xamarin.com/discussion/61903/code-snippet-how-to-generate-video-thumbnail CMTime actualTime; NSError outError; using (var asset = AVAsset.FromUrl(NSUrl.FromFilename(videoFilePath))) using (var imageGen = new AVAssetImageGenerator(asset)) using (var imageRef = imageGen.CopyCGImageAtTime(new CMTime(1, 1), out actualTime, out outError)) { UIImage videoThumbnail = UIImage.FromImage(imageRef); NSData imgData = videoThumbnail.AsJPEG(); imgData.Save(videoThumbnailFilePath, false); } } ); #endif #if __ANDROID__ // Generate bitmap Bitmap videoBitmap = await createVideoBitmapAsync(100, 100, videoFilePath); // Extract name of video file, use same name for video thumbnail file string videoFileName = System.IO.Path.GetFileNameWithoutExtension(videoFilePath); string videoThumbnailFilePath = System.IO.Path.Combine( FilePaths.allVideoThumbnailsPath, videoFileName + ".jpg"); // Save to file await exportBitmapAsPNGAsync(videoThumbnailFilePath, videoBitmap); #endif }
public override void ViewDidLoad() { base.ViewDidLoad(); this.View.Add(this.tableView); this.View.Add(this.mapView); this.tableView.Source = this.momentsTableViewSource; this.mapView.GetViewForAnnotation += (mapView, annotation) => { if (annotation is MKUserLocation) { return(null); } // create pin annotation view MKAnnotationView pinView = (MKPinAnnotationView)mapView.DequeueReusableAnnotation("MomentPin"); if (pinView == null) { pinView = new MKPinAnnotationView(annotation, "MomentPin"); } if (annotation is MomentAnnotation) { Moment moment = ((MomentAnnotation)annotation).Moment; var button = UIButton.FromType(UIButtonType.DetailDisclosure); button.TouchDown += (sender, e) => { EditDialog dialog = new EditDialog(); dialog.Config(moment); this.NavigationController.PushViewController(dialog, true); }; pinView.RightCalloutAccessoryView = button; if (moment.Media.Count > 0) { NSData data = null; if (moment.Media[0].URL != null) { if (moment.Media[0].Type == "Image") { using (Stream imageStream = AppDelegate.MomentsManager.FileSystem.getFileStream(moment.Media[0].URL)) { data = NSData.FromStream(imageStream); AppDelegate.MomentsManager.FileSystem.CloseFileStream(imageStream); } pinView.LeftCalloutAccessoryView = new UIImageView() { Bounds = new CGRect(0, 0, 40, 40), Image = UIImage.LoadFromData(data) }; } else { var nsurl = NSUrl.FromFilename((Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/" + moment.Media [0].URL)); AVAsset asset = AVAsset.FromUrl(nsurl); AVAssetImageGenerator generator = new AVAssetImageGenerator(asset); generator.AppliesPreferredTrackTransform = true; NSError err = null; CMTime outTime = new CMTime(); CMTime requestedTime = new CMTime(2, 1); // To create thumbnail image using (var imgRef = generator.CopyCGImageAtTime(requestedTime, out outTime, out err)){ pinView.LeftCalloutAccessoryView = new UIImageView() { Bounds = new CGRect(0, 0, 40, 40), Image = UIImage.FromImage(imgRef) }; } } pinView.LeftCalloutAccessoryView.Layer.CornerRadius = 20; pinView.LeftCalloutAccessoryView.Layer.MasksToBounds = true; } } else { pinView.LeftCalloutAccessoryView = null; } if (moment.State == MomentState.Finished) { ((MKPinAnnotationView)pinView).PinColor = MKPinAnnotationColor.Green; } else if (moment.State == MomentState.InProgress) { ((MKPinAnnotationView)pinView).PinColor = MKPinAnnotationColor.Purple; } else { ((MKPinAnnotationView)pinView).PinColor = MKPinAnnotationColor.Red; } pinView.CanShowCallout = true; } return(pinView); }; //this.TabBarItem = new UITabBarItem ("My Moments", UIImage.FromFile ("images/moments.png"), 1); this.tableView.SeparatorInset = new UIEdgeInsets(0, 0, 0, 0); this.tableView.TintColor = this.NavigationController.NavigationBar.TintColor; AppDelegate.MomentsManager.Synced += () => { this.tableView.ReloadData(); updateMap(); }; }
private static void Save_Data(queue Finsh_queue) { DataSql _DataSql = new DataSql(); UIImage image; LibraryWords library = new LibraryWords(); var myByteArray = File.ReadAllBytes(Finsh_queue.Filename); byte[] myByteThumbnail = null; if (Finsh_queue._Type == "Video") { // byte[] myByteArray; CoreMedia.CMTime actualTime; NSError outError; using (var asset = AVAsset.FromUrl(NSUrl.FromFilename(Finsh_queue.Filename))) using (var imageGen = new AVAssetImageGenerator(asset)) using (var imageRef = imageGen.CopyCGImageAtTime(new CoreMedia.CMTime(1, 1), out actualTime, out outError)) { if (imageRef == null) { // return null; } image = UIImage.FromImage(imageRef); } using (NSData imageData = image.AsPNG()) { myByteThumbnail = new Byte[imageData.Length]; System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, myByteThumbnail, 0, Convert.ToInt32(imageData.Length)); } if (save.Savegerlly) { if (File.Exists(Finsh_queue.Filename)) { savedowloaning file = new savedowloaning(); // AMvideo.SaveToAlbum(Finsh_queue.Filename); file.Get_clipfile(Finsh_queue.ID.ToString(), myByteArray); // ALAssetsLibrary lib = new ALAssetsLibrary(); // lib.WriteVideoToSavedPhotosAlbum(NSUrl.FromFilename(Finsh_queue.Filename), (t, u) => { }); } } } else { myByteThumbnail = myByteArray; if (save.Savegerlly) { savedowloaning file = new savedowloaning(); file.Get_imagefile(Finsh_queue.ID.ToString(), myByteArray); } } _DataSql.process(library.File(2), library.INSERT(2), DataSql_Parameters(Path.GetFileName(Finsh_queue.Filename), myByteArray, myByteThumbnail, Finsh_queue._Type)); }
private UIImage GetVideoThumbnail(string path) { try { CMTime actualTime; NSError outError; using (var asset = AVAsset.FromUrl (NSUrl.FromFilename (path))) using (var imageGen = new AVAssetImageGenerator (asset)) { imageGen.AppliesPreferredTrackTransform = true; using (var imageRef = imageGen.CopyCGImageAtTime (new CMTime (1, 1), out actualTime, out outError)) { return UIImage.FromImage (imageRef); } } } catch { return null; } }
FileData GetMovieMediaFile(NSDictionary info) { var url = info[UIImagePickerController.MediaURL] as NSUrl; if (url == null) { return(null); } AVUrlAsset asset = new AVUrlAsset(url); // 文件描述 var track = asset.GetTracks(AVMediaTypes.Video)[0]; TimeSpan ts = TimeSpan.FromSeconds(track.TimeRange.Duration.Seconds); string desc = $"{ts.Minutes.ToString("D2")}:{ts.Seconds.ToString("D2")}"; // 生成缩略图 string thumbPath = null; var segs = track.Segments; if (segs != null && segs.Length > 0) { CMTime startTime = CMTime.Invalid; for (int i = 0; i < segs.Length; i++) { if (!segs[i].Empty) { startTime = segs[i].TimeMapping.Target.Start; break; } } if (!startTime.IsInvalid) { var assetGen = new AVAssetImageGenerator(asset); assetGen.AppliesPreferredTrackTransform = true; using (var firstImg = assetGen.CopyCGImageAtTime(startTime, out var r, out var error)) { if (error == null) { UIImage img = new UIImage(firstImg); desc += $" ({img.CGImage.Width} x {img.CGImage.Height})"; thumbPath = CreateThumbnail(img, true); } } } } var path = System.IO.Path.Combine(Kit.CachePath, Kit.NewGuid + ".mp4"); try { File.Move(url.Path, path); } catch (Exception ex) { Debug.WriteLine($"Unable to move file, trying to copy. {ex.Message}"); try { File.Copy(url.Path, path); File.Delete(url.Path); } catch (Exception) { Debug.WriteLine($"Unable to copy/delete file, will be left around :( {ex.Message}"); } } var fd = new FileData(path, System.IO.Path.GetFileName(path), (ulong)new FileInfo(path).Length); fd.Desc = desc; fd.ThumbPath = thumbPath; return(fd); }