Cross-Platform Desktop Capture

Captures the entire display at the specified framerate. It works the best with SetInputTime to limit recording to a limited time. That method adds new stream to your conversion with you entire screen. Please note that if you have multiple screens result can be strange and additional parameters has to be set up.

string output = Path.Combine("C:", "Temp", "desktop_capture.mp4");

IConversionResult conversionResult = await FFmpeg.Conversions.New()
    .AddDesktopStream()
    .SetInputTime(TimeSpan.FromSeconds(3))
    .SetOutput(output)
    .Start();

CancellationToken is another option to stop capturing.

string output = Path.Combine("C:", "Temp", "desktop_capture.mp4");

CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
FFmpeg.Conversions.New()
    .AddDesktopStream()
    .SetOutput(output)
    .Start(cancellationTokenSource.Token);

await Task.Delay(5000);
cancellationTokenSource.Cancel();

For more advanced usage it is possible to modify desktop stream. Example below shows how to rotate desktop stream. Of course a lot of others methods can be used instead of rotate.

string output = Path.Combine("C:", "Temp", "desktop_capture.mp4");

var conversion = FFmpeg.Conversions.New()
    .AddDesktopStream()
    .SetInputTime(TimeSpan.FromSeconds(3))
    .SetOutput(output);
var desktopStream = conversion.Streams.First() as IVideoStream;
desktopStream.Rotate(RotateDegrees.Clockwise);

await conversion.Start();

AddDesktopStream method has few optional parameters. They are useful if you have multiple screens.

/// <summary>
///     Capture desktop to output file
/// </summary>
/// <param name="xOffset">X Offset</param>
/// <param name="yOffset">Y Offset</param>
/// <param name="videoSize">Input video size</param>
/// <param name="framerate">The desired framerate of the output</param>
/// <returns>IConversion object</returns>
IConversion AddDesktopStream(string videoSize = null, double framerate = 30, int xOffset = 0, int yOffset = 0);
IConversion AddDesktopStream(VideoSize videoSize, double framerate = 30, int xOffset = 0, int yOffset = 0);

Underneath AddDesktopStream method add additional stream that depends on Operating System. On Windows it uses gdigrab, on Linux x11grab and on OSX - avfoundation.