SceneCapture2D to Avencoder

Haxors123
Sat, 05 Mar 2022 15:27:13 +0000
Rendering

Hello i’m trying to take the texture target from a UTextureRenderTarget2D and use the copytexture to copy it to a AVEncoder instance.

I have the AVEncoder working, because i can get this to work with the OnBackBufferReadyToPresent callback.

However when i replace the reference with the one for the texture target the copy texture simply stops working.

I found somecode on where which seems to do it successfully.

How to encode scene capture textures into h264 video? Asset Creation

I have some scene captures and I want to encode their rendering results into h264 videos, with UE4’s internal AmfVideoEncoder. But I failed to achieve that:( The offical usage of AmfVideoEncoder registers back buffer capture with UE4: FSlateApplication::Get().GetRenderer()->OnBackBufferReadyToPresent().AddRaw(this, &FGameplayMediaEncoder::OnBackBufferReady); The only modification I did was replacing the back buffer with scene capture’s rendering target textures. But the “OnEncodedVideoFrame” …

However this interface seems to have been replaced with the CopyTexture command.

But like the awnser here i’m trying to use the enqueue render command.

UOpencvLibBPLibrary *This = this;
ENQUEUE_RENDER_COMMAND(Readback)([This]
(FRHICommandListImmediate& RHICmdList)
{
AVEncoder::FBufferId BufferId;
FTimespan Now = FTimespan::FromSeconds(FApp::GetCurrentTime()) - This->startingTime;
FRHITexture2D *texture_pointer = This->targetTexture->GetRenderTargetResource()->GetTexture2DRHI();
RHICmdList.Transition(FRHITransitionInfo(texture_pointer, ERHIAccess::Unknown, ERHIAccess::ReadOnlyMask));
FRHIRenderPassInfo RPInfo(texture_pointer, ERenderTargetActions::Load_Store);
RHICmdList.BeginRenderPass(RPInfo, TEXT(“CopyBackbuffer”));
if (!This->VideoEncoder->CopyTexture(texture_pointer,
Now,
Now - This->LastVideoInputTimestamp,
BufferId,
texture_pointer->GetSizeXY()
))
{
UE_LOG(LogTemp, Error, TEXT(“Failed to copy”));
return;
}
RHICmdList.EndRenderPass();
This->VideoEncoder->Encode(BufferId, false, 0, nullptr);
This->LastVideoInputTimestamp = Now;
});

The code example above simply crashes in the render thread, so i’m wonder if this is the right approach or if i need to flush my render command somehow?

The engine version is 4.26.1.

Anyways appreciate the help.

1 post - 1 participant