Efficient video recording method

I am trying to record the screen using FFMPEG. I made it work by adding a USceneCaptureComponent2D and calling the USceneCaptureComponent2D::CaptureScene() method. I get 60fps at 720p but I want to try higher resolution video at higher fps.

I think the performance issue now is that I am basically rendering the screen twice. I only want to copy the memory where the screen pixels are stored, copy them to the CPU memory, and call the pipe method all in the rendering thread. But I could not find such a method in the API. How can I achieve this? How can I get screen pixel values?

I manage to reach this code

struct FReadSurfaceContext
{
	FRenderTarget* SrcRenderTarget;
	TArray<FColor>* OutData;
	FIntRect Rect;
	FReadSurfaceDataFlags Flags;
};

FReadSurfaceContext Context =
{
	rtr,
	&OutImageData,
	InRect,
	InFlags
};

ENQUEUE_RENDER_COMMAND(ReadSurfaceCommand)(
	[Context, pipe](FRHICommandListImmediate& RHICmdList)
	{
		RHICmdList.ReadSurfaceData(
			Context.SrcRenderTarget->GetRenderTargetTexture(),
			Context.Rect,
			*Context.OutData,
			Context.Flags
			);
		fwrite(Context.OutData->GetData(), Context.Rect.Area() * 4 * sizeof(uint8), 1, pipe);
	});

where rtr is GetWorld()->GetGameViewport()->Viewport.

I get nice video (but slower than I expected) in the editor but I only get black pixels in the shipping build.

I found an answer here

It is not actually faster. I guess the bottleneck was somewhere else.