Can you get a video thumbnail using MediaToolkit without reading/writing to disk?

Viewed 291

I have a video file that's already loaded in memory (an IFormFile that's converted to a byte[]).

I've been trying to figure out how to take that video and get a thumbnail image from it, without having to read/write to disk.

I found use cases for MediaToolkit and FFmpeg here, and Movie Thumbnailer here, but from what I can find, those require the video already be saved to disk and have write access to output the thumbnail to a file.

Is there any way I can take either an IFormFile or byte[] and do something similar to what MediaToolkit is doing while being able to keep the result in memory?

I know a lot of folks are saying byte[] isn't the way to go. In that case, I'm more than happy to convert from IFormFile to a Stream, but I still need a way to do that and keep it in memory.

1 Answers

please try this with Windows.Media.Editing should work, not fully tested..

    int frameHeight; // you can set the height
    int frameWidth; // ...
    TimeSpan getFrameInTime = new TimeSpan(0, 0, 1);


    //Using Windows.Media.Editing get your ImageStream
    var yourClip = await MediaClip.CreateFromFileAsync(someFile);
    var composition = new MediaComposition();
    // add the section of the video as needed
    composition.Clips.Add(yourClip);

    // Answer - now that your have your imageStream, get the thumbnail
    var yourImageStream = await composition.GetThumbnailAsync(
       getFrameInTime, 
       Convert.ToInt32(frameWidth), 
       Convert.ToInt32(frameHeight), 
       VideoFramePrecision.NearestFrame);
    

    //now create your thumbnail bitmap 
    var yourThumbnailBitmap = new WriteableBitmap(
       Convert.ToInt32(frameWidth),  
       Convert.ToInt32(frameHeight)
    );

    yourThumbnailBitmap.SetSource(yourImageStream);
Related