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

Converting Frame to byte[] #52

Closed MastalerzKamil closed 3 years ago

MastalerzKamil commented 3 years ago

What I need to do is getting frame and convert that to byte[] format to send it to another microservice. Is that conversion possible? I know that there is property Span<byte> and then use ToArray() but it didn't work and RestSharp throw an exception.

Toumash commented 3 years ago

What code is exactly throwing an exception? Show us the stack trace with the message. Isn't that a serialization issue - from the serialization library that RestSharp uses? How big is the frame in kilobytes?

MastalerzKamil commented 3 years ago

Alright, my problem has been solved. @Toumash was right, RestSharp might be problematic. The more simple way was using HttpClient.

Here is a method which do that:

private async void SendPicture(string frameName, byte[] frameData)
{
    var client = new HttpClient();
    var formDataContent = new MultipartFormDataContent();
    var fileContent = new ByteArrayContent(frameData);
    fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/png");
    var fileName = $"{frameName}.png";
    formDataContent.Add(fileContent, "image", fileName);

    var namePair = new KeyValuePair<string, string>("name", frameName);
    formDataContent.Add(new StringContent(namePair.Value), namePair.Key);

    var response = await client.PostAsync("http://127.0.0.1:5002/predict", formDataContent);

}

The issue has been also connected with sending pixels instead of bytes array converted to PNG format. I've converted Image<Bgr24> into bytes[] of PNG image by using SixLabors library.

var framePixels = file.Video.ReadFrame(i).ToBitmap();
var ms = new MemoryStream();
framePixels.SaveAsPng(ms);
var frameAsPng = ms.ToArray();