AydinAdn/MediaToolkit

Stream-to-Stream operations

Opened this issue · 2 comments

Hello,

Thanks for such a great wrapper.

Is there any way to implement Stream-to-Stream operations? There are some cases where exporting/importing stuff to/from files is overkill.

For example, this could process data directly from request buffer and serves the thumbnail as it generated.

[HttpPut("video")]
public void video(CancellationToken ct) {
    Request.EnableRewind();

    Response.Headers["Content-Type"] = "image/png"; 

    using(var engine = new Engine()) {
        engine.GetThumbnail(Request.Body, Response.Body, ...);
    }
}

I m interested to know too.

In my case, I want to save the new generated file on Azure. Stream is what I m looking for.

@ranouf I was planning to use it in a that way, too.

I really wonder that why .net developers give Streams such a low interest. There is no Stream multiplexer proxy class in general, Amazon S3 file upload gets the size from Stream - no manual specifying size method, .NET restricts Stream classes specify size if seeking is not supported etc.

Doing these kind of things would be pretty cool. Without using Files, MemoryStreams, buffering, duplicating etc. We are living at 2018 and still wasting resources for a simple things.

The code bellow, would change the world, if the .NET developers believe in a such a thing.

//using(var source = new MultiplexerStream(Request.Body))
//using(var source1 = source.copyRead())
//using(var source2 = source.copyRead())
using(var stream1 = new MultiplexerStream())
using(var stream2 = stream1.copyRead())
using(var stream3 = stream1.copyRead())
 {
    using(var engine = new Engine()) {
       var thumbnailTask = engine.GetThumbnail(Request.Body, stream1, ...);
      // var thumbnailTask = engine.GetThumbnail(source1, stream1, ...);
      // var encode720pTask = engine.Encode...(source2, stream4, ...);

       var azureUploadTask = blob.UploadStream(stream2, ...);
       var amazonUploadTask = s3Client.PutObject(stream3, ...);

    // upload 720p version (stream4) to Azure and Amazon as well

       await Task.WhenAll(thumbnailTask, /* encode720pTask */, azureUploadTask, amazonUploadTask);

    }
}