javaandroidhttp-live-streamingandroid-mediacodecandroid-media3

How to create fragmented MPEG-4 (FMP4) file from a .mp4 on Android using Kotlin or Java (without using FFmpeg)


Is there a way to take a .mp4 video file on Android and create a fragmented MPEG-4 (FMP4) video container file (with the accompanying playlist .m3u8 file)? Either Java or Kotlin is fine, but I don't want to use FFmpeg.

I want to use the FMP4 file for video on demand streaming (not live streaming) via HTTP Live Streaming (HLS) protocol. The codecs are basic H.264/AAC. Android supports playing FMP4 containers with HLS as they describe here. FMP4 spec for HLS is here.

There is a video from Apple on how to do this on iOS but I can't find anything on Android.

There is the Android Media3 Transformer API but it doesn't look like it can do this. It has a FragmentedMp4Extractor class but is only used to extract data from the FMP4 container format.

JCodec can't seem to do this either. There is an open issue in the project requesting fragmented MP4.


Solution

  • You can produce fragmented mp4 video using transformer API:

        val transformerBuilder = Transformer.Builder( /* context= */this)
        val muxerFactory = InAppMuxer.Factory.Builder()
                .setOutputFragmentedMp4(true)
                .build()
        transformerBuilder.setMuxerFactory(muxerFactory)