You can use ffmpeg, an open source video library, to do the compression.
Be aware that video compression is generally quite compute intense so it is unlikely to take just 'a few seconds' except for small videos.
There are a number of ways to include ffmpeg in your application. Take a look at these 'wrapper' projects for examples to ether use or to help you build your own ffmpeg library:
As an example, the following code will compress an mp4 video file (default compression) once you have selected or built a ffmpeg wrapper (this code uses a custom ffmpeg library but it should give you an overview and you should be able to substitute one of the examples above):
public class VideoCompressionTask extends AsyncTask<String, String, String> {
/* This Class is an AsynchTask to compress a video on a background thread
*
*/
@Override
protected String doInBackground(String... params) {
//Compress the video in the background
//Get the the path of the video to compress
String videoPath;
String videoFileName;
File videoFileToCompress;
if (params.length == 1) {
videoPath = params[0];
videoFileToCompress = new File(videoPath);
videoFileName = videoFileToCompress.getName();
} else {
//One or all of the params are not present - log an error and return
Log.d("VideoCompressionTask","doInBackground wrong number of params");
return null;
}
//Make sure the video to compress actually exists
if(!videoFileToCompress.exists()) {
Log.d("VideoCompressionTask","doInBackground video file to compress does not exist");
return null;
}
//If the compressed file already exists then delete it first and let this task create a new one
File compressedVideoFile = new File(compressedFilePath);
if(compressedVideoFile.exists()) {
compressedVideoFile.delete();
}
String argv[] = {"ffmpeg", "-i", videoPath, "-strict", "experimental", "-acodec", "aac", compressedFilePath};
int ffmpegWrapperreturnCode = FfmpegJNIWrapper.call_ffmpegWrapper(appContext, argv);
Log.d("VideoCompressionTask","doInBackground ffmpegWrapperreturnCode: " + ffmpegWrapperreturnCode);
return(compressedFilePath);
}