We know uploading a video on server is a time consuming task but by using third party libraries we can compress video file faster.
There are many libraries available but this one is good, you don't have to add any ndk with this lib :
FFmpeg4Android
Download this library from here :
https://drive.google.com/file/d/0B9qAjo6wKhk9bVBpM0lWYXBSeVk/view
and add this library as a project module in your app and then use the following code :
Send your package name, input file name and output file name in this method :
Here LoadJNI is a class of this library that is used to run this complex command to compress video file.
In complex command you can also reduce the quality of video by replacing 480X320 to 160x120 and you can also reduce the bitrate by replacing -b 209k to -b 100k etc.
public static String[] compressVideo(Activity act,String packageName,String inputFile, String outputFile) {
PowerManager powerManager = (PowerManager)act.getSystemService(Activity.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "VK_LOCK");
Log.d(Prefs.TAG, "Acquire wake lock");
wakeLock.acquire();
File directoryFile=new File("/data/data/"+packageName+"/ffmpeg");
if(directoryFile.exists()){
System.out.println("FFMPEG exists");
}
String[] complexCommand = {"/data/data/"+packageName+"/ffmpeg", "-y", "-i", inputFile, "-strict", "experimental",
"-s", "480x320", "-r", "30","-aspect", "4: 3", "-vcodec", "mpeg4", "-b", "150k", "-ab", "48000",
"-ac", "2", "-ar", "22050", "-b", "209k",outputFile};
LoadJNI vk = new LoadJNI();
try {
vk.run(complexCommand, "/data/data/"+packageName,act);
} catch (Throwable e) {
Log.e(Prefs.TAG, "vk run exeption.", e);
}
finally {
if (wakeLock.isHeld())
wakeLock.release();
else{
Log.i(Prefs.TAG, "Wake lock is already released, doing nothing");
}
}
Log.i(Prefs.TAG, "doInBackground finished");
return complexCommand;
}
This method will generate a new compressed video.
0 Comment(s)