unosquare / ffmediaelement

FFME: The Advanced WPF MediaElement (based on FFmpeg)
https://unosquare.github.io/ffmediaelement/
Other
1.17k stars 238 forks source link

Question: How to enable GPU for decoding (when not shown on screen #556

Closed jtorjo closed 3 years ago

jtorjo commented 3 years ago

Question: How to enable GPU for decoding (when not shown on screen)

I want to use ffmpeg to capture frames, and save them to disk. I did a small test, and it's not using the GPU.

Issue Categories

Steps to Reproduce

public static async Task test_go_forward() {
    await Library.LoadFFmpegAsync();

    Unosquare.FFME.MediaElement me = new MediaElement {
        LoadedBehavior = MediaPlaybackState.Manual
    };

    await me.Open(new Uri("D:\\john\\buff\\__cinematic\\_4k\\4k-04.mp4"));
    await Task.Delay(1000);
    Debug.Assert(me.IsOpen);
    await me.Seek(TimeSpan.FromMilliseconds(220));

    var watch = Stopwatch.StartNew();
    for (int i = 0; i < 100; ++i) {
        await me.StepForward();
        await me.CaptureBitmapAsync();
    }

    MessageBox.Show("capture " + watch.ElapsedMilliseconds);
}

The above is a simple test (ignore the fact that I'm not really using the returned bitmap). I've looked at the settings for MediaElement, but I don't really see anything that would allow this.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

mariodivece commented 3 years ago

The GPU in this WPF control can only be used to DECODE frames. Meaning, the encoded packets get to the GPU, the GPU puts them together and stores an image internally, and that image is then converted to a WPF-compatible bitmap. If you want to enable GPU decoding please look at the sample project (MainWindow.MediaEvents.cs) and search for the following comment: // Hardware device selection. Please keep in mind this creates-round trips i.e. CPU->GPU->CPU and so on. The reason why the image cannot be shown directly off of the GPU is because of how Windows does window composition. Displaying images straight from the GPU would cause the control to lose layout, transparency, etc. capabilities.

jtorjo commented 3 years ago

Thanks, will look into it