Search code examples
javaencodingazure-functions

Is it possible to encode AVI video to MP4 using Azure function?


Since Azure Media Service is being retired next year, I was thinking to try different approach to encode a video to MP4 and this approach includes Azure Function. The flow would be like this

  1. Get BlobInputStream by calling blobClient.openInputStream() on BlobClient.
  2. Get byte[] from BlobInputStream.
  3. Use some library to perform encoding like JAVE2 or FFMPEG.(This is where my confusion is how to do this).
  4. Hopig above would result in byte[], create InputStream from byte[].
  5. Write encoded video to storage account container by calling upload(InputStream data) on BlobClinet.

In theory sounds simple but the problem I am running into here is JAVE2 and other similar libraries accepts java.io.File object for input and output but azure-storage-blob library does not exposes any api to get File object. Now option left is to open inputstream to get byte[] but then how to encode a byte array.

Update: Input file size is max 100MB.


Solution

  • I accomplished this with Java azure function and JAVE2 library. I got the SAS Url of source file, then fed that to MultimediaObject

    Sample Code:

    private File encoded = null;
    
    MultimediaObject multimediaObject = new MultimediaObject(new URL(blobSasUrl));
    
    VideoAttributes video = new VideoAttributes();
    video.setCodec(LIBX264_CODEC);
    video.setFrameRate(FRAME_RATE);
    
    EncodingAttributes attrs = new EncodingAttributes();
    attrs.setOutputFormat(ENCODE_FORMAT);
    attrs.setVideoAttributes(video);
    
    Encoder encoder = new Encoder();
    encoder.encode(multimediaObject, encoded, attrs);