Reputation: 151
I am facing an issue in android, that is compressing a video after selecting it from gallery, because when i select a video from gallery and converts it to Base64 string the string becomes too long like 3.5mb of just string.What can be the solution to this. Its ok to degrade the video result. Here is the code snippet.
Uri selectedVideoUri = data.getData();
String selectedPath = getPath(selectedVideoUri,"Video");
encodedVideo=convertFileToString(selectedPath);
public String convertFileToString(String pathOnSdCard){
String strFile=null;
File file=new File(pathOnSdCard);
try {
byte[] data = FileUtils.readFileToByteArray(file);//Convert any file, image or video into byte array
strFile = Base64.encodeToString(data, Base64.NO_WRAP);//Convert byte array into string
} catch (IOException e) {
e.printStackTrace();
}
try {
File myFile = new File("/sdcard/mysdfile.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(strFile);
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Done writing SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
return strFile;
} private String getPath(Uri uri,String str) {
String[] projection = { MediaStore.Video.Media.DATA, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION};
Cursor cursor = managedQuery(uri, projection, null, null, null);
cursor.moveToFirst();
String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
// int fileSize = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
// long duration = Time.MILLISECONDS.toSeconds(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)));
//some extra potentially useful data to help with filtering if necessary
//System.out.println("size: " + fileSize);
System.out.println("path: " + filePath);
// System.out.println("duration: " + duration);
return filePath;
}
Upvotes: 3
Views: 2911
Reputation: 488
You can use FFmepg. I achieved by implementing JDK to compress the video. You need to download the library file and run it. For more information, Refer this
And compress the video in Background Task, which will be more efficient.
class VideoCompressing extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = ProgressDialog.show(Sentinel.this, "", "Compressing, Please Wait...", true);
dialog.setMax(100);
dialog.show();
}
@Override
protected String doInBackground(String... key) {
String newText = key[0];
LoadJNI vk = new LoadJNI();
try {
File sdCardDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + "DTP_Video");
try {
if (!sdCardDirectory.exists()) {
sdCardDirectory.mkdirs();
}
dateofpic=currentDateFormat();
String videoDestiPath = "VID_"+dateofpic+".mp4";
File image = new File(sdCardDirectory, videoDestiPath);
afterCompressPath = image.getAbsolutePath();
}
catch (Exception e)
{
e.printStackTrace();
}
String workFolder = getApplicationContext().getFilesDir().getAbsolutePath();
String[] complexCommand = {"ffmpeg","-y" ,"-i",newText,
"-strict","experimental","-s", "460x320","-r","25", "-vcodec", "mpeg4", "-b", "1500k",
"-ab","48000", "-ac", "2", "-ar", "22050", afterCompressPath};
vk.run(complexCommand, workFolder, getApplicationContext());
Log.i("test", "ffmpeg4android finished successfully");
}
catch (Throwable e) {
Log.e("test", "vk run exception.", e);
}
runOnUiThread(new Runnable() {
public void run() {
}
});
return null;
}
Upvotes: 1