public Task <string> CreateThumbnailAsync(string videoPath)
        {
            return(Task.Run(() =>
            {
                var url = NSUrl.FromFilename(videoPath);
                var asset = AVAsset.FromUrl(url);
                var assetImageGenerator = AVAssetImageGenerator.FromAsset(asset);
                assetImageGenerator.AppliesPreferredTrackTransform = true;

                var time = CMTime.FromSeconds(1, 1);

                using (var img = assetImageGenerator.CopyCGImageAtTime(time, out CMTime _, out NSError __))
                {
                    if (img == null)
                    {
                        Debug.WriteLine($"Could not find file at url: {videoPath}");
                        return string.Empty;
                    }

                    var outputPath = Path.ChangeExtension(videoPath, ".png");
                    var fileUrl = NSUrl.FromFilename(outputPath);

                    using (var imageDestination = CGImageDestination.Create(fileUrl, UTType.PNG, 1))
                    {
                        imageDestination.AddImage(img);
                        imageDestination.Close();
                    }

                    return outputPath;
                }
            }));
        }
Exemplo n.º 2
0
        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
        }
Exemplo n.º 3
0
        ImageSource IVideoUtils.GetVideoThumbnail(string url)
        {
            NSUrl videoUrl = NSUrl.CreateFileUrl(url, null);

            var asset          = AVAsset.FromUrl(videoUrl);
            var imageGenerator = AVAssetImageGenerator.FromAsset(asset);

            imageGenerator.AppliesPreferredTrackTransform = true;
            var seconds = asset.Duration.Seconds;

            CoreMedia.CMTime actualTime;
            CoreMedia.CMTime cmTime = asset.Duration;
            var timeScale           = asset.Duration.TimeScale;

            cmTime.Value = (seconds > 5) ? timeScale * 5 : timeScale * 1;
            NSError error;
            var     imageRef = imageGenerator.CopyCGImageAtTime(cmTime, out actualTime, out error);

            if (imageRef == null)
            {
                return(null);
            }

            var uiImage = UIImage.FromImage(imageRef);



            var img = Xamarin.Forms.ImageSource.FromStream(() => ((uiImage.AsPNG().AsStream())));


            return(img);
        }
Exemplo n.º 4
0
        void UpdateUI()
        {
            var formsControl = Element as VideoImagePreview;

            if (formsControl == null)
            {
                return;
            }

            if (!string.IsNullOrWhiteSpace(formsControl.VideoUrl))
            {
                var outputTime = new CMTime();
                var error      = new NSError();

                using (var asset = AVAsset.FromUrl(new NSUrl(formsControl.VideoUrl)))
                    using (var assetGenerator = AVAssetImageGenerator.FromAsset(asset))
                    {
                        var image = assetGenerator.CopyCGImageAtTime(new CMTime(0, 1), out outputTime, out error);

                        if (image != null)
                        {
                            Control?.InvokeOnMainThread(() => ((UIImageView)Control).Image = new UIImage(image));
                        }
                    }
            }
            else
            {
                formsControl.Source = "camera.png";
            }
        }
Exemplo n.º 5
0
        public MemoryStream GenerateThumbImage(string url, long usecond)
        {
            var asset          = AVAsset.FromUrl(NSUrl.FromFilename(url));
            var imageGenerator = AVAssetImageGenerator.FromAsset(asset);

            imageGenerator.AppliesPreferredTrackTransform = true;
            var actualTime = asset.Duration;

            CoreMedia.CMTime cmTime = new CoreMedia.CMTime(usecond, 2000000);
            NSError          error;
            var cgImage = imageGenerator.CopyCGImageAtTime(cmTime, out actualTime, out error);

            //AVAssetImageGenerator imageGenerator = new AVAssetImageGenerator(AVAsset.FromUrl((new Foundation.NSUrl(url))));
            //imageGenerator.AppliesPreferredTrackTransform = true;
            //var actualTime = asset.Duration;
            //CoreMedia.CMTime cmTime = new CoreMedia.CMTime(usecond, 1000000);
            //NSError error;
            //var cgImage = imageGenerator.CopyCGImageAtTime(cmTime, out actualTime, out error);
            //CMTime actualTime;
            //NSError error;
            //CGImage cgImage = imageGenerator.CopyCGImageAtTime(new CMTime(usecond, 1000000), out actualTime, out error);

            var zz = new UIImage(cgImage).AsJPEG().AsStream();
            var a  = new MemoryStream();

            zz.CopyTo(a);

            return(a);
            //return ImageSource.FromStream(() => new UIImage(cgImage).AsJPEG().AsStream()); //mageSource.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));
        }
Exemplo n.º 7
0
        // 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);
            }
        }
Exemplo n.º 8
0
        public static UIImage GetThumbnailFromVideoURL(NSUrl videoURL)
        {
            UIImage theImage  = null;
            var     asset     = AVAsset.FromUrl(videoURL);
            var     generator = AVAssetImageGenerator.FromAsset(asset);

            generator.AppliesPreferredTrackTransform = true;
            NSError error = null;
            var     time  = new CMTime(1, 60);
            CMTime  actTime;
            var     img = generator.CopyCGImageAtTime(time, out actTime, out error);

            if (error != null)
            {
                Console.WriteLine(error.ToString());
                return(UIImage.FromBundle("videoimage.png"));
            }
            Console.WriteLine(img.ToString());
            theImage = new UIImage(img);
            var     path        = videoURL.RelativePath;
            var     imgData     = SuperimposeImage(theImage, UIImage.FromBundle("btn_play.png")).AsJPEG(0.5f);
            NSError err         = null;
            var     jpgFilename = path.Replace(Path.GetExtension(path), "-thumb.jpg");

            if (imgData.Save(jpgFilename, false, out err))
            {
                if (err == null)
                {
                    Console.WriteLine("Thumbnail saved");
                }
                else
                {
                    Console.WriteLine("error in saving " + err);
                }
            }
            else
            {
                Console.WriteLine("Thumbnail not saved");
            }

            return(theImage);
        }
Exemplo n.º 9
0
        private void SetVideoCell(AlbumViewCell cell, MediaItem item, int row)
        {
            DispatchQueue.DefaultGlobalQueue.DispatchAsync(() =>
            {
                using (var asset = AVAsset.FromUrl(new NSUrl(item.Path)))
                    using (var generator = AVAssetImageGenerator.FromAsset(asset))
                    {
                        generator.AppliesPreferredTrackTransform = true;

                        var duration    = asset.Duration.Seconds;
                        var scaledImage = GetVideoThumbnail(generator, duration, _cellSize);

                        DispatchQueue.MainQueue.DispatchAsync(() =>
                        {
                            if (cell.Tag != row)
                            {
                                return;
                            }
                            cell.Duration = duration;
                            cell.Image    = scaledImage;
                        });
                    }
            });
        }