Generate video from a Bitmap, Canvas, or resource drawable in Android.
Create mp4 video from Bitmaps or anything you can draw to a hardware accelerated Canvas. Pure, simple Android MediaCodec implementation. Requires no third party libs or NDK.
Currently supports the MP4 container and both AVC/H264 and HEVC/H265. Easily extensible to other supported formats.
Run the sample app or check out and MainActivity for an example.
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add to your app dependencies:
dependencies {
implementation 'com.github.israel-fl:bitmap2video:2.0.0'
}
Simply create a Muxer
object
val muxer = Muxer(this@MainActivity, "/files/video.mp4")
// and mux
muxer.mux(imageArray)
Use callbacks to listen for video completion:
muxer.setOnMuxingCompletedListener(object : MuxingCompletionListener {
override fun onVideoSuccessful(file: File) {
Log.d(TAG, "Video muxed - file path: ${file.absolutePath}")
}
override fun onVideoError(error: Throwable) {
Log.e(TAG, "There was an error muxing the video")
}
})
Thread(Runnable {
muxer.mux(imageArray, R.raw.bensound_happyrock)
}).start()
Or use a co-routine by calling muxAsync
:
scope.launch {
when (val result = muxer.muxAsync(imageArray, R.raw.bensound_happyrock)) {
is MuxingSuccess -> {
Log.i(TAG, "Video muxed - file path: ${result.file.absolutePath}")
onMuxerCompleted()
}
is MuxingError -> {
Log.e(TAG, "There was an error muxing the video")
bt_make.isEnabled = true
}
}
}
val muxerConfig = MuxerConfig(this, 600, 600, 'video/avc', 3, 1F, 1500000)
val muxer = Muxer(this@MainActivity, muxerConfig!!)
// or
muxer.setMuxerConfig(muxerConfig)
- File object
- video width
- video height
- Mimetype
- Frames per image (how many seconds to display each image)
- Frames per second
- Bitrate
- FrameMuxer (only MP4 included currently)
- IFrame Interval
The library currently supports Bitmap
, Canvas
, and drawable resources (R.drawable.image1
)
In order to add audio, pass in an audio track to the mux
method.
muxer.mux(imageArray, R.raw.bensound_happyrock)
We provide a few functions to simplify a couple of tasks. These can be
found as static methods under FileUtils
getFileDescriptor(Context context, int R.raw.sound_file)
getVideoFile(final Context context, final String fileName)
shareVideo(Context context, File file, String mimeType)
These utility functions use the library's ContentProvider
with
declared authorities and specified paths.
You can change which provider is being used and specify your paths if you register your own against the manifest and create your own resources. You must then pass in the appropriate paths to the functions. See the library's AndroidManifest for an example.
getVideoFile(Context context, String fileDir, String fileName)
and
shareVideo(Context context, File file, String mimeType, String fileAuthority)