Home » Android » I want to attach Pre-Rolls to videos taken on android devices

I want to attach Pre-Rolls to videos taken on android devices

Posted by: admin May 14, 2020 Leave a comment


I’m using mp4parser and the videos need to be of the same kind.
I was thinking of using android’s media codec to decode & encode the preroll video to fit the same encoding output of the cameras (front & back)
any suggestion on how this can be done (how to get specific device encoding params)?

How to&Answers:

If you want to find out what encoding your Android camera is using, try using this: https://developer.android.com/reference/android/media/CamcorderProfile

This should suffice to answer your questions for detecting the video encoding including : The file output format, Video codec format, Video bit rate in bits per second, Video frame rate in frames per second, Video frame width and height, Audio codec format, Audio bit rate in bits per second, Audio sample rate Number of audio channels for recording.

Pulled a lot of the above information from here as well: https://developer.android.com/guide/topics/media/camera#capture-video

As for transcoding videos that are already in the users roll, I found this useful transcoder that was written in pure java using the Android MediaCodec API and can be found here: https://github.com/ypresto/android-transcoder

Also, as rupps mentioned below, you can use ffmeg which is proven to work countless times on Android. However, the reasoning for me linking the other transcoder first is because, as the author states, “FFmpeg is the most famous solution for transcoding. But using FFmpeg binary on Android can cause GPL and/or patent issues. Also using native code for Android development can be troublesome because of cross-compiling, architecture compatibility, build time and binary size.” So use whichever one you believe better suits you. Here is the link for ffmeg for Android:

If you don’t want to use the transcoder that someone else made then I reccomend making your own transcoder using the MediaCodec API that can be found here: https://developer.android.com/reference/android/media/MediaCodec


If you want magic, try this library.


Take a look at the MediaComposer class.

Here’s also a code snippet on how it’s done.

org.m4m.MediaComposer mediaComposer = new org.m4m.MediaComposer(factory, progressListener);

int orientation = mediaFileInfo.getRotation();

mediaComposer.setTargetFile(dstMediaPath, orientation);

// set video encoder
VideoFormatAndroid videoFormat = new VideoFormatAndroid(videoMimeType, width, height);

// set audio encoder
AudioFormatAndroid aFormat = new AudioFormatAndroid(audioMimeType, audioFormat.getAudioSampleRateInHz(), audioFormat.getAudioChannelCount());

mediaComposer.setTargetFile(dstMediaPath, orientation);