video processing: pick smaller file after transcoding. do not transcode very small files

This commit is contained in:
Daniel Gultsch 2017-10-27 11:34:50 +02:00
parent 396ad3361f
commit ad6f6183b8
3 changed files with 24 additions and 6 deletions

View File

@ -66,7 +66,6 @@ public final class Config {
public static final int IMAGE_SIZE = 1920; public static final int IMAGE_SIZE = 1920;
public static final Bitmap.CompressFormat IMAGE_FORMAT = Bitmap.CompressFormat.JPEG; public static final Bitmap.CompressFormat IMAGE_FORMAT = Bitmap.CompressFormat.JPEG;
public static final int IMAGE_QUALITY = 75; public static final int IMAGE_QUALITY = 75;
public static final int IMAGE_MAX_SIZE = 524288; //512KiB
public static final int MESSAGE_MERGE_WINDOW = 20; public static final int MESSAGE_MERGE_WINDOW = 20;

View File

@ -137,7 +137,7 @@ public class FileBackend {
} }
} }
private static long getFileSize(Context context, Uri uri) { public static long getFileSize(Context context, Uri uri) {
try { try {
final Cursor cursor = context.getContentResolver().query(uri, null, null, null, null); final Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
@ -235,14 +235,14 @@ public class FileBackend {
} }
File file = new File(path); File file = new File(path);
long size = file.length(); long size = file.length();
if (size == 0 || size >= Config.IMAGE_MAX_SIZE ) { if (size == 0 || size >= mXmppConnectionService.getResources().getInteger(R.integer.auto_accept_filesize)) {
return false; return false;
} }
BitmapFactory.Options options = new BitmapFactory.Options(); BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; options.inJustDecodeBounds = true;
try { try {
BitmapFactory.decodeStream(mXmppConnectionService.getContentResolver().openInputStream(uri), null, options); BitmapFactory.decodeStream(mXmppConnectionService.getContentResolver().openInputStream(uri), null, options);
if (options == null || options.outMimeType == null || options.outHeight <= 0 || options.outWidth <= 0) { if (options.outMimeType == null || options.outHeight <= 0 || options.outWidth <= 0) {
return false; return false;
} }
return (options.outWidth <= Config.IMAGE_SIZE && options.outHeight <= Config.IMAGE_SIZE && options.outMimeType.contains(Config.IMAGE_FORMAT.name().toLowerCase())); return (options.outWidth <= Config.IMAGE_SIZE && options.outHeight <= Config.IMAGE_SIZE && options.outMimeType.contains(Config.IMAGE_FORMAT.name().toLowerCase()));
@ -348,6 +348,7 @@ public class FileBackend {
scaledBitmap = rotate(scaledBitmap, rotation); scaledBitmap = rotate(scaledBitmap, rotation);
boolean targetSizeReached = false; boolean targetSizeReached = false;
int quality = Config.IMAGE_QUALITY; int quality = Config.IMAGE_QUALITY;
final int imageMaxSize = mXmppConnectionService.getResources().getInteger(R.integer.auto_accept_filesize);
while(!targetSizeReached) { while(!targetSizeReached) {
os = new FileOutputStream(file); os = new FileOutputStream(file);
boolean success = scaledBitmap.compress(Config.IMAGE_FORMAT, quality, os); boolean success = scaledBitmap.compress(Config.IMAGE_FORMAT, quality, os);
@ -355,7 +356,7 @@ public class FileBackend {
throw new FileCopyException(R.string.error_compressing_image); throw new FileCopyException(R.string.error_compressing_image);
} }
os.flush(); os.flush();
targetSizeReached = file.length() <= Config.IMAGE_MAX_SIZE || quality <= 50; targetSizeReached = file.length() <= imageMaxSize|| quality <= 50;
quality -= 5; quality -= 5;
} }
scaledBitmap.recycle(); scaledBitmap.recycle();

View File

@ -9,6 +9,7 @@ import net.ypresto.androidtranscoder.MediaTranscoder;
import net.ypresto.androidtranscoder.format.MediaFormatStrategy; import net.ypresto.androidtranscoder.format.MediaFormatStrategy;
import net.ypresto.androidtranscoder.format.MediaFormatStrategyPresets; import net.ypresto.androidtranscoder.format.MediaFormatStrategyPresets;
import java.io.File;
import java.io.FileDescriptor; import java.io.FileDescriptor;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@ -31,6 +32,7 @@ public class AttachFileToConversationRunnable implements Runnable, MediaTranscod
private final Uri uri; private final Uri uri;
private final UiCallback<Message> callback; private final UiCallback<Message> callback;
private final boolean isVideoMessage; private final boolean isVideoMessage;
private final long originalFileSize;
private int currentProgress = -1; private int currentProgress = -1;
public AttachFileToConversationRunnable(XmppConnectionService xmppConnectionService, Uri uri, Message message, UiCallback<Message> callback) { public AttachFileToConversationRunnable(XmppConnectionService xmppConnectionService, Uri uri, Message message, UiCallback<Message> callback) {
@ -39,7 +41,11 @@ public class AttachFileToConversationRunnable implements Runnable, MediaTranscod
this.message = message; this.message = message;
this.callback = callback; this.callback = callback;
final String mimeType = MimeUtils.guessMimeTypeFromUri(mXmppConnectionService, uri); final String mimeType = MimeUtils.guessMimeTypeFromUri(mXmppConnectionService, uri);
this.isVideoMessage = (mimeType != null && mimeType.startsWith("video/") && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2); final int autoAcceptFileSize = mXmppConnectionService.getResources().getInteger(R.integer.auto_accept_filesize);
this.originalFileSize = FileBackend.getFileSize(mXmppConnectionService,uri);
this.isVideoMessage = (mimeType != null && mimeType.startsWith("video/")
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
&& originalFileSize > autoAcceptFileSize;
} }
public boolean isVideoMessage() { public boolean isVideoMessage() {
@ -111,6 +117,18 @@ public class AttachFileToConversationRunnable implements Runnable, MediaTranscod
@Override @Override
public void onTranscodeCompleted() { public void onTranscodeCompleted() {
mXmppConnectionService.stopForcingForegroundNotification(); mXmppConnectionService.stopForcingForegroundNotification();
final File file = mXmppConnectionService.getFileBackend().getFile(message);
long convertedFileSize = mXmppConnectionService.getFileBackend().getFile(message).getSize();
Log.d(Config.LOGTAG,"originalFileSize="+originalFileSize+" convertedFileSize="+convertedFileSize);
if (originalFileSize != 0 && convertedFileSize >= originalFileSize) {
if (file.delete()) {
Log.d(Config.LOGTAG,"original file size was smaller. deleting and processing as file");
processAsFile();
return;
} else {
Log.d(Config.LOGTAG,"unable to delete converted file");
}
}
mXmppConnectionService.getFileBackend().updateFileParams(message); mXmppConnectionService.getFileBackend().updateFileParams(message);
if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
mXmppConnectionService.getPgpEngine().encrypt(message, callback); mXmppConnectionService.getPgpEngine().encrypt(message, callback);