This is a library which creates a video from a series of bitmaps.
The library uses the surface input capabilities of Android's mediacodec (https://developer.android.com/reference/android/media/MediaCodec) that were introduced in Android 4.3 (API 18), hence 4.3 is the earliest version of Android that supports this library. A lot of the code that I used came from snippets than can be found here: https://bigflake.com/mediacodec/.
There is very little parameter sanity checking so be careful. For example, setting up a video encoding with a weird resolution can lock up your device, though, for me, the device has always restarted itself after a few seconds. Different devices behave differently. My S7 seems to be very forgiving and will create videos at any resolution provided the width and height are divisible by two. Another of my devices only likes the standard resolutions. Speaking of which: https://developer.android.com/guide/topics/media/media-formats. Stick to the standard resolutions and you shouldn't have any problem.
Likewise with the other setup parameters. Going lower than 3FPS sometimes caused playback issues. I haven't played around with the bitrate and IFrame_Interval settings since I found the commonly quoted values of 2,000,000 bps and 1 IFrame per second worked well for my requirements. (Look at the previously referenced web pages for more info.) Finally for the codec (MIME_Type) parameter, I always use "video/avc" (H.264) though you can play around with the other codecs supported by your device. You can get the codec list by calling the encoderCodecs method.
Multiple videos can be generated simultaneously but remember to send the correct state information to each encoder instance.
BitmapToVideoEncoder
Author: CaptKronos
Version: 1.1
The use of the library is straightforward:
The library uses the surface input capabilities of Android's mediacodec (https://developer.android.com/reference/android/media/MediaCodec) that were introduced in Android 4.3 (API 18), hence 4.3 is the earliest version of Android that supports this library. A lot of the code that I used came from snippets than can be found here: https://bigflake.com/mediacodec/.
There is very little parameter sanity checking so be careful. For example, setting up a video encoding with a weird resolution can lock up your device, though, for me, the device has always restarted itself after a few seconds. Different devices behave differently. My S7 seems to be very forgiving and will create videos at any resolution provided the width and height are divisible by two. Another of my devices only likes the standard resolutions. Speaking of which: https://developer.android.com/guide/topics/media/media-formats. Stick to the standard resolutions and you shouldn't have any problem.
Likewise with the other setup parameters. Going lower than 3FPS sometimes caused playback issues. I haven't played around with the bitrate and IFrame_Interval settings since I found the commonly quoted values of 2,000,000 bps and 1 IFrame per second worked well for my requirements. (Look at the previously referenced web pages for more info.) Finally for the codec (MIME_Type) parameter, I always use "video/avc" (H.264) though you can play around with the other codecs supported by your device. You can get the codec list by calling the encoderCodecs method.
Multiple videos can be generated simultaneously but remember to send the correct state information to each encoder instance.
BitmapToVideoEncoder
Author: CaptKronos
Version: 1.1
- BitmapToVideoEncoder
- Functions:
- encoderCodecs As String
Returns a comma separated string of the available encoder codecs together with their supported MIME types.
It's the MIME type that is passed to the setup method.
Example:<code>
Dim codecs As String = aBitmapToVideoEncoder.encoderCodecs</code> - endEncoding (theMediacodecParams As com.cyfer.bitmapstovideo.BitmapToVideoEncoder.mediacodecParams)
Ends the encoding session. Pass the object returned from the call to setup.
Example:<code>
aBitmapToVideoEncoder.endEncoding(state)</code> - setup (Filename As String, Width As Int, Height As Int, MIME_Type As String, Codec_Name As String, Bitrate As Int, Frame_Rate As Int, IFrame_Interval As Int) As com.cyfer.bitmapstovideo.BitmapToVideoEncoder.mediacodecParams
Initialises a new encoding session. Returns an object that holds the encoder state. This object must be passed to the write and end methods.
More than one session can be active simultaneously - make sure to send the correct state object!
Example:<code>
Dim aBitmapToVideoEncoder As BitmapToVideoEncoder
Dim state As JavaObject=aBitmapToVideoEncoder.setup("/sdcard/Movies/test.mp4", 1920, 1080, "video/avc", "OMX.google.h264.encoder", 2000000, 15, 1)</code> - writeBitmap (theMediacodecParams As com.cyfer.bitmapstovideo.BitmapToVideoEncoder.mediacodecParams, bitmap As android.graphics.Bitmap) As Boolean
Sends a bitmap to the encoder. Pass the object returned from the call to setup.
Example:<code>
Dim success As Boolean = aBitmapToVideoEncoder.writeBitmap(state, bitmap)</code> - writeImageFile (theMediacodecParams As com.cyfer.bitmapstovideo.BitmapToVideoEncoder.mediacodecParams, filename As String) As Boolean
Sends an image file (e.g. a jpeg) to the encoder. Pass the object returned from the call to setup.
Example:<code>
Dim success As Boolean = aBitmapToVideoEncoder.writeImageFile(state, "/sdcard/Pictures/test.jpg")</code>
- encoderCodecs As String
- Functions:
The use of the library is straightforward:
B4X:
Sub CreateVideo
Dim state As JavaObject
Dim aBitmapToVideoEncoder As BitmapToVideoEncoder
state = aBitmapToVideoEncoder.setup("/sdcard/Movies/test.mp4", _
1920, 1080, "video/avc", "OMX.google.h264.encoder", 2000000, 15, 1)
Dim bmp As Bitmap=generateVideoFrame(0), ret As Boolean = True
Dim n As Int = 1
Do While bmp.IsInitialized And ret=True
ret=aBitmapToVideoEncoder.writeBitmap(state, bmp)
bmp=generateVideoFrame(n)
n=n+1
Loop
aBitmapToVideoEncoder.endEncoding(state)
End Sub
'return Null when finished
Sub generateVideoFrame(n As Int) As Bitmap
End Sub
Attachments
Last edited: