radek-k / FFMediaToolkit

FFMediaToolkit is a cross-platform video decoder/encoder library for .NET that uses FFmpeg native libraries. It supports video frames extraction, reading stream metadata and creating videos from bitmaps in any format supported by FFmpeg.
MIT License
352 stars 56 forks source link

ImageData.Data Stride Estimation #134

Open Scubaboy opened 8 months ago

Scubaboy commented 8 months ago

Hi, I'm using FFMediaToolKit in a Unity 3D project to create videos of Unity's 3D scene. I'm capturing Unity's camera view as follows:

 _screenShot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
 _captureCamera.targetTexture = _renderTexture;
 RenderTexture.active = _renderTexture;
 _screenShot.ReadPixels(new Rect(0,0,Screen.width, Screen.height),0,0);
 _screenShot.Apply();
var pixelData = _screenShot.GetRawTextureData();

The above code provides a byte array of raw pixel data. When I take the byte array and try to construct an 'ImageData' object

ImageData.FromArray(pixelData , ImagePixelFormat.Rgb24, Screen.width, Screen.height);

I get the following exception:

Pixel buffer size doesn't match size required by this image format.

You use the following formula to calculate the expected stride:

4 * (((GetBitsPerPixel(format) * width) + 31) / 32);

However the byte array generated by Unity is

Screen.Width x Screen.height x 3

3 bytes per pixel corresponding to the 'TextureFormat.RGB24'

Is there any course of action I can take to resolve this difference?