From bb6b647af8988fea006c123bdff68d1f97eac966 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 28 Sep 2018 18:38:54 +0200 Subject: [PATCH] use stronger video compression (lower bitrate & lower resolution) --- .../AttachFileToConversationRunnable.java | 4 +- .../utils/Android360pFormatStrategy.java | 69 +++++++++++++++++++ .../utils/Android480pFormatStrategy.java | 69 +++++++++++++++++++ 3 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 src/main/java/eu/siacs/conversations/utils/Android360pFormatStrategy.java create mode 100644 src/main/java/eu/siacs/conversations/utils/Android480pFormatStrategy.java diff --git a/src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java b/src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java index 44f88e27e..039f2dcb2 100644 --- a/src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java +++ b/src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java @@ -23,6 +23,8 @@ import eu.siacs.conversations.entities.DownloadableFile; import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.persistance.FileBackend; import eu.siacs.conversations.ui.UiCallback; +import eu.siacs.conversations.utils.Android360pFormatStrategy; +import eu.siacs.conversations.utils.Android480pFormatStrategy; import eu.siacs.conversations.utils.MimeUtils; public class AttachFileToConversationRunnable implements Runnable, MediaTranscoder.Listener { @@ -90,7 +92,7 @@ public class AttachFileToConversationRunnable implements Runnable, MediaTranscod message.setRelativeFilePath(message.getUuid() + ".mp4"); final DownloadableFile file = mXmppConnectionService.getFileBackend().getFile(message); final int runtime = mXmppConnectionService.getFileBackend().getMediaRuntime(uri); - MediaFormatStrategy formatStrategy = runtime >= 8000 ? MediaFormatStrategyPresets.createExportPreset960x540Strategy() : MediaFormatStrategyPresets.createAndroid720pStrategy(); + final MediaFormatStrategy formatStrategy = runtime >= 20000 ? new Android360pFormatStrategy() : new Android480pFormatStrategy(); file.getParentFile().mkdirs(); final ParcelFileDescriptor parcelFileDescriptor = mXmppConnectionService.getContentResolver().openFileDescriptor(uri, "r"); if (parcelFileDescriptor == null) { diff --git a/src/main/java/eu/siacs/conversations/utils/Android360pFormatStrategy.java b/src/main/java/eu/siacs/conversations/utils/Android360pFormatStrategy.java new file mode 100644 index 000000000..a931136a6 --- /dev/null +++ b/src/main/java/eu/siacs/conversations/utils/Android360pFormatStrategy.java @@ -0,0 +1,69 @@ +package eu.siacs.conversations.utils; + +import android.media.MediaCodecInfo; +import android.media.MediaFormat; +import android.util.Log; + +import net.ypresto.androidtranscoder.format.MediaFormatExtraConstants; +import net.ypresto.androidtranscoder.format.MediaFormatStrategy; +import net.ypresto.androidtranscoder.format.OutputFormatUnavailableException; + +import eu.siacs.conversations.Config; + +public class Android360pFormatStrategy implements MediaFormatStrategy { + + private static final int LONGER_LENGTH = 640; + private static final int SHORTER_LENGTH = 360; + private static final int DEFAULT_VIDEO_BITRATE = 1000 * 1000; // 1000 kbit/s upper range of what YouTube recommends + private static final int DEFAULT_AUDIO_BITRATE = 96 * 1000; + private final int mVideoBitrate; + private final int mAudioBitrate; + private final int mAudioChannels; + + public Android360pFormatStrategy() { + mVideoBitrate = DEFAULT_VIDEO_BITRATE; + mAudioBitrate = DEFAULT_AUDIO_BITRATE; + mAudioChannels = 2; + } + + @Override + public MediaFormat createVideoOutputFormat(MediaFormat inputFormat) { + int width = inputFormat.getInteger(MediaFormat.KEY_WIDTH); + int height = inputFormat.getInteger(MediaFormat.KEY_HEIGHT); + int longer, shorter, outWidth, outHeight; + if (width >= height) { + longer = width; + shorter = height; + outWidth = LONGER_LENGTH; + outHeight = SHORTER_LENGTH; + } else { + shorter = width; + longer = height; + outWidth = SHORTER_LENGTH; + outHeight = LONGER_LENGTH; + } + if (longer * 9 != shorter * 16) { + throw new OutputFormatUnavailableException("This video is not 16:9, and is not able to transcode. (" + width + "x" + height + ")"); + } + if (shorter <= SHORTER_LENGTH) { + Log.d(Config.LOGTAG, "This video is less or equal to 360p, pass-through. (" + width + "x" + height + ")"); + return null; + } + MediaFormat format = MediaFormat.createVideoFormat("video/avc", outWidth, outHeight); + // From Nexus 4 Camera in 720p + format.setInteger(MediaFormat.KEY_BIT_RATE, mVideoBitrate); + format.setInteger(MediaFormat.KEY_FRAME_RATE, 30); + format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 3); + format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface); + return format; + } + + @Override + public MediaFormat createAudioOutputFormat(MediaFormat inputFormat) { + final MediaFormat format = MediaFormat.createAudioFormat(MediaFormatExtraConstants.MIMETYPE_AUDIO_AAC, inputFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE), mAudioChannels); + format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC); + format.setInteger(MediaFormat.KEY_BIT_RATE, mAudioBitrate); + return format; + } + +} diff --git a/src/main/java/eu/siacs/conversations/utils/Android480pFormatStrategy.java b/src/main/java/eu/siacs/conversations/utils/Android480pFormatStrategy.java new file mode 100644 index 000000000..197cc573a --- /dev/null +++ b/src/main/java/eu/siacs/conversations/utils/Android480pFormatStrategy.java @@ -0,0 +1,69 @@ +package eu.siacs.conversations.utils; + +import android.media.MediaCodecInfo; +import android.media.MediaFormat; +import android.util.Log; + +import net.ypresto.androidtranscoder.format.MediaFormatExtraConstants; +import net.ypresto.androidtranscoder.format.MediaFormatStrategy; +import net.ypresto.androidtranscoder.format.OutputFormatUnavailableException; + +import eu.siacs.conversations.Config; + +public class Android480pFormatStrategy implements MediaFormatStrategy { + + private static final int LONGER_LENGTH = 854; + private static final int SHORTER_LENGTH = 480; + private static final int DEFAULT_VIDEO_BITRATE = 2000 * 1000; // 2000 kbit/s upper range of what YouTube recommends + private static final int DEFAULT_AUDIO_BITRATE = 96 * 1000; + private final int mVideoBitrate; + private final int mAudioBitrate; + private final int mAudioChannels; + + public Android480pFormatStrategy() { + mVideoBitrate = DEFAULT_VIDEO_BITRATE; + mAudioBitrate = DEFAULT_AUDIO_BITRATE; + mAudioChannels = 2; + } + + @Override + public MediaFormat createVideoOutputFormat(MediaFormat inputFormat) { + int width = inputFormat.getInteger(MediaFormat.KEY_WIDTH); + int height = inputFormat.getInteger(MediaFormat.KEY_HEIGHT); + int longer, shorter, outWidth, outHeight; + if (width >= height) { + longer = width; + shorter = height; + outWidth = LONGER_LENGTH; + outHeight = SHORTER_LENGTH; + } else { + shorter = width; + longer = height; + outWidth = SHORTER_LENGTH; + outHeight = LONGER_LENGTH; + } + if (longer * 9 != shorter * 16) { + throw new OutputFormatUnavailableException("This video is not 16:9, and is not able to transcode. (" + width + "x" + height + ")"); + } + if (shorter <= SHORTER_LENGTH) { + Log.d(Config.LOGTAG, "This video is less or equal to 360p, pass-through. (" + width + "x" + height + ")"); + return null; + } + MediaFormat format = MediaFormat.createVideoFormat("video/avc", outWidth, outHeight); + // From Nexus 4 Camera in 720p + format.setInteger(MediaFormat.KEY_BIT_RATE, mVideoBitrate); + format.setInteger(MediaFormat.KEY_FRAME_RATE, 30); + format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 3); + format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface); + return format; + } + + @Override + public MediaFormat createAudioOutputFormat(MediaFormat inputFormat) { + final MediaFormat format = MediaFormat.createAudioFormat(MediaFormatExtraConstants.MIMETYPE_AUDIO_AAC, inputFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE), mAudioChannels); + format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC); + format.setInteger(MediaFormat.KEY_BIT_RATE, mAudioBitrate); + return format; + } + +}