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 ).

Sign In or Register to comment.