Forum General

Create thumbnail from Video

Mikael_GirouxMikael_Giroux USMember ✭✭

I want to create 5 thumbnails from a video (at 5 different moment in the video) to let the user choose the thumbnail.
I did some search and I didn't find the good way to do that.
Can someone help me?
Thanks

Best Answer

Answers

  • Mikael_GirouxMikael_Giroux USMember ✭✭

    @flchaux thank you it's working

  • @flchaux said:

    On iOS you can do this:

    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());mageSource.FromStream(() => new UIImage(cgImage).AsPNG().AsStream());
    }

    On Android:

    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));
    }
    return null;
    }

    It works for iOS but doesn't work for Android.
    retriever.SetDataSource(url, new Dictionary<string, string>()); makes exception.
    url like this /storage/emulated/0/Android/data/com.ibase.mtwpublicapp/files/Movies/temp/VID_20180706_005641_1.mp4

    Please help me to fix, thanks!

  • @flchaux said:

    On iOS you can do this:

    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());mageSource.FromStream(() => new UIImage(cgImage).AsPNG().AsStream());
    }

    On Android:

    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));
    }
    return null;
    }

    I have tried this solution but in iOS the image didn't appear

  • ArmaganCelikArmaganCelik USMember ✭✭

    For Android:
    if file path is => /storage/emulated/0/Download/Sample.mp4 something like that,
    use => retriever.SetDataSource(url);
    instead of => retriever.SetDataSource(url, new Dictionary<string, string>());

  • MedFehmiMedFehmi Member ✭✭

    @flchaux please can you tell me how to convert ImageSource returned from each method to a string ( as a file path ).

  • abashindeabashinde Member ✭✭
    edited October 2019

    Line: retriever.SetDataSource(url, new Dictionary<string, string>());
    Exception : setDataSource failed: status = 0x80000000'
    can you tell me why this error occurring

  • kishan574kishan574 USMember ✭✭

    @flchaux said:

    On iOS you can do this:

    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());mageSource.FromStream(() => new UIImage(cgImage).AsPNG().AsStream());
    }

    In IOS I am using same code but i want to store the thumbnail in particular location Like
    "Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)" how can i do this .

    please give some suggestions

        UIImage uIImage = UIImage.FromImage(cgImage);
                 var img = uIImage.AsPNG().AsStream();
    
                    Byte[] mybytes = new Byte[img.Length];
                    var videos = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+ "/" + fileName);
                    System.IO.File.WriteAllBytes(videos, mybytes); 
    

    I do like this but it is not working

  • E75E75 NLMember ✭✭

    @flchaux It doesn't work for IOS 13 devices. I'm getting a null object (cgImage) back. Do you know a solution for this problem?

  • junnnjunnn Member
    edited January 28

    iOS here

    public ImageSource ExecuteCommand(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, 1000000);
    NSError error;
    var cgImage= imageGenerator.CopyCGImageAtTime(cmTime, out actualTime, out error);
    return ImageSource.FromStream(() => new UIImage(cgImage).AsPNG().AsStream());
    }

  • hs79hshs79hs AUMember ✭✭

    For iOS, I use PHCachingImageManager

    PHAsset videoAsset;
    ...
    readonly PHCachingImageManager imageManager = new PHCachingImageManager();
    
    var thumbnailFilePath = Path.Combine(cachePath, filename);
                    if (!File.Exists(thumbnailFilePath))
                    {   // if can't find thumbnail, request it
                        //Console.WriteLine("not exist: {0}", thumbnailFilePath);
                        imageManager.RequestImageForAsset(videoAsset, thumbnailSize, PHImageContentMode.AspectFill, null, (image, info) =>
                        {
                            if (image != null)
                            {
                                File.WriteAllBytes(thumbnailFilePath, image.AsPNG().ToArray());
                            }
                        });
                    }
    
Sign In or Register to comment.