mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
Force video-recoding for videos which size is okay but the bitrate is too high. Correct maximum bitrate handing (was checked too son before and scaled down afterwards, on typical devices, this resulted in a bitrate of about 500 kbit which we use as a maximum now; everyone is welcome to improve all this stuff).
This commit is contained in:
@@ -2614,7 +2614,8 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
|
||||
int rotationValue = messageObject.videoEditedInfo.rotationValue;
|
||||
int originalWidth = messageObject.videoEditedInfo.originalWidth;
|
||||
int originalHeight = messageObject.videoEditedInfo.originalHeight;
|
||||
int bitrate = messageObject.videoEditedInfo.bitrate;
|
||||
int originalBitrate = messageObject.videoEditedInfo.originalBitrate;
|
||||
int resultBitrate = messageObject.videoEditedInfo.resultBitrate;
|
||||
int rotateRender = 0;
|
||||
File cacheFile = new File(messageObject.messageOwner.attachPath);
|
||||
|
||||
@@ -2676,7 +2677,7 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
|
||||
|
||||
checkConversionCanceled();
|
||||
|
||||
if (resultWidth != originalWidth || resultHeight != originalHeight || rotateRender != 0) {
|
||||
if (resultBitrate<originalBitrate || resultWidth != originalWidth || resultHeight != originalHeight || rotateRender != 0) {
|
||||
int videoIndex;
|
||||
videoIndex = selectTrack(extractor, false);
|
||||
if (videoIndex >= 0) {
|
||||
@@ -2765,7 +2766,7 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
|
||||
|
||||
MediaFormat outputFormat = MediaFormat.createVideoFormat(MIME_TYPE, resultWidth, resultHeight);
|
||||
outputFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, colorFormat);
|
||||
outputFormat.setInteger(MediaFormat.KEY_BIT_RATE, bitrate != 0 ? bitrate : 921600);
|
||||
outputFormat.setInteger(MediaFormat.KEY_BIT_RATE, resultBitrate != 0 ? resultBitrate : 921600);
|
||||
outputFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 25);
|
||||
outputFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 10);
|
||||
if (Build.VERSION.SDK_INT < 18) {
|
||||
|
||||
@@ -106,13 +106,6 @@ public class MessageObject {
|
||||
|
||||
setType();
|
||||
|
||||
if (messageOwner.message != null && messageOwner.id < 0 && messageOwner.message.length() > 6 && isVideo()) {
|
||||
videoEditedInfo = new VideoEditedInfo();
|
||||
if (!videoEditedInfo.parseString(messageOwner.message)) {
|
||||
videoEditedInfo = null;
|
||||
}
|
||||
}
|
||||
|
||||
generateCaption();
|
||||
if (generateLayout) {
|
||||
messageText = Emoji.replaceEmoji(messageText, textPaint.getFontMetricsInt(), AndroidUtilities.dp(20), false);
|
||||
|
||||
@@ -23,50 +23,15 @@
|
||||
|
||||
package com.b44t.messenger;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public class VideoEditedInfo {
|
||||
public long startTime;
|
||||
public long endTime;
|
||||
public int rotationValue;
|
||||
public int originalWidth;
|
||||
public int originalHeight;
|
||||
public int originalBitrate;
|
||||
public int resultWidth;
|
||||
public int resultHeight;
|
||||
public int bitrate;
|
||||
public int resultBitrate;
|
||||
public String originalPath;
|
||||
|
||||
public String getString() {
|
||||
return String.format(Locale.US, "-1_%d_%d_%d_%d_%d_%d_%d_%d_%s", startTime, endTime, rotationValue, originalWidth, originalHeight, bitrate, resultWidth, resultHeight, originalPath);
|
||||
}
|
||||
|
||||
public boolean parseString(String string) {
|
||||
if (string.length() < 6) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
String args[] = string.split("_");
|
||||
if (args.length >= 10) {
|
||||
startTime = Long.parseLong(args[1]);
|
||||
endTime = Long.parseLong(args[2]);
|
||||
rotationValue = Integer.parseInt(args[3]);
|
||||
originalWidth = Integer.parseInt(args[4]);
|
||||
originalHeight = Integer.parseInt(args[5]);
|
||||
bitrate = Integer.parseInt(args[6]);
|
||||
resultWidth = Integer.parseInt(args[7]);
|
||||
resultHeight = Integer.parseInt(args[8]);
|
||||
for (int a = 9; a < args.length; a++) {
|
||||
if (originalPath == null) {
|
||||
originalPath = args[a];
|
||||
} else {
|
||||
originalPath += "_" + args[a];
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
FileLog.e("messenger", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1648,18 +1648,8 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not
|
||||
VideoEditorActivity fragment = new VideoEditorActivity(args);
|
||||
fragment.setDelegate(new VideoEditorActivity.VideoEditorActivityDelegate() {
|
||||
@Override
|
||||
public void didFinishEditVideo(String videoPath, long startTime, long endTime, int resultWidth, int resultHeight, int rotationValue, int originalWidth, int originalHeight, int bitrate, long estimatedSize, long estimatedDuration) {
|
||||
VideoEditedInfo videoEditedInfo = new VideoEditedInfo();
|
||||
videoEditedInfo.startTime = startTime;
|
||||
videoEditedInfo.endTime = endTime;
|
||||
videoEditedInfo.rotationValue = rotationValue;
|
||||
videoEditedInfo.originalWidth = originalWidth;
|
||||
videoEditedInfo.originalHeight = originalHeight;
|
||||
videoEditedInfo.bitrate = bitrate;
|
||||
videoEditedInfo.resultWidth = resultWidth;
|
||||
videoEditedInfo.resultHeight = resultHeight;
|
||||
videoEditedInfo.originalPath = videoPath;
|
||||
SendMessagesHelper.prepareSendingVideo(videoPath, estimatedSize, estimatedDuration, resultWidth, resultHeight, videoEditedInfo, dialog_id);
|
||||
public void didFinishEditVideo(VideoEditedInfo vei, long estimatedSize, long estimatedDuration) {
|
||||
SendMessagesHelper.prepareSendingVideo(vei.originalPath, estimatedSize, estimatedDuration, vei.resultWidth, vei.resultHeight, vei, dialog_id);
|
||||
m_mrChat.cleanDraft();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -40,6 +40,7 @@ import android.widget.FrameLayout;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.b44t.messenger.VideoEditedInfo;
|
||||
import com.coremedia.iso.IsoFile;
|
||||
import com.coremedia.iso.boxes.Box;
|
||||
import com.coremedia.iso.boxes.MediaBox;
|
||||
@@ -97,7 +98,7 @@ public class VideoEditorActivity extends BaseFragment implements TextureView.Sur
|
||||
private int originalHeight = 0;
|
||||
private int resultWidth = 0;
|
||||
private int resultHeight = 0;
|
||||
private int bitrate = 0;
|
||||
private int resultBitrate = 0;
|
||||
private int originalBitrate = 0;
|
||||
private float videoDuration = 0;
|
||||
private long startTime = 0;
|
||||
@@ -109,7 +110,7 @@ public class VideoEditorActivity extends BaseFragment implements TextureView.Sur
|
||||
private long originalSize = 0;
|
||||
|
||||
public interface VideoEditorActivityDelegate {
|
||||
void didFinishEditVideo(String videoPath, long startTime, long endTime, int resultWidth, int resultHeight, int rotationValue, int originalWidth, int originalHeight, int bitrate, long estimatedSize, long estimatedDuration);
|
||||
void didFinishEditVideo(VideoEditedInfo vei, long estimatedSize, long estimatedDuration);
|
||||
}
|
||||
|
||||
private Runnable progressRunnable = new Runnable() {
|
||||
@@ -259,7 +260,18 @@ public class VideoEditorActivity extends BaseFragment implements TextureView.Sur
|
||||
/*if (compressVideo.getVisibility() == View.GONE || compressVideo.getVisibility() == View.VISIBLE && !compressVideo.isChecked()) {
|
||||
delegate.didFinishEditVideo(videoPath, startTime, endTime, originalWidth, originalHeight, rotationValue, originalWidth, originalHeight, originalBitrate, estimatedSize, esimatedDuration);
|
||||
} else*/ {
|
||||
delegate.didFinishEditVideo(videoPath, startTime, endTime, resultWidth, resultHeight, rotationValue, originalWidth, originalHeight, bitrate, estimatedSize, esimatedDuration);
|
||||
VideoEditedInfo vei = new VideoEditedInfo();
|
||||
vei.originalPath = videoPath;
|
||||
vei.startTime = startTime;
|
||||
vei.endTime = endTime;
|
||||
vei.rotationValue = rotationValue;
|
||||
vei.originalWidth = originalWidth;
|
||||
vei.originalHeight = originalHeight;
|
||||
vei.originalBitrate = originalBitrate;
|
||||
vei.resultWidth = resultWidth;
|
||||
vei.resultHeight = resultHeight;
|
||||
vei.resultBitrate = resultBitrate;
|
||||
delegate.didFinishEditVideo(vei, estimatedSize, esimatedDuration);
|
||||
}
|
||||
}
|
||||
finishFragment();
|
||||
@@ -738,10 +750,7 @@ public class VideoEditorActivity extends BaseFragment implements TextureView.Sur
|
||||
TrackHeaderBox headerBox = trackBox.getTrackHeaderBox();
|
||||
if (headerBox.getWidth() != 0 && headerBox.getHeight() != 0) {
|
||||
trackHeaderBox = headerBox;
|
||||
originalBitrate = bitrate = (int) (trackBitrate / 100000 * 100000);
|
||||
if (bitrate > 900000) {
|
||||
bitrate = 900000;
|
||||
}
|
||||
originalBitrate = resultBitrate = (int) (trackBitrate / 100000 * 100000);
|
||||
videoFramesSize += sampleSizes;
|
||||
} else {
|
||||
audioFramesSize += sampleSizes;
|
||||
@@ -766,12 +775,17 @@ public class VideoEditorActivity extends BaseFragment implements TextureView.Sur
|
||||
float scale = resultWidth > resultHeight ? 640.0f / resultWidth : 640.0f / resultHeight;
|
||||
resultWidth *= scale;
|
||||
resultHeight *= scale;
|
||||
if (bitrate != 0) {
|
||||
bitrate *= Math.max(0.5f, scale);
|
||||
videoFramesSize = (long) (bitrate / 8 * videoDuration);
|
||||
if (resultBitrate != 0) {
|
||||
resultBitrate *= Math.max(0.5f, scale);
|
||||
}
|
||||
}
|
||||
|
||||
if (resultBitrate > 500000) {
|
||||
resultBitrate = 500000; // ~ 3.7 MB/minute, plus Audio
|
||||
}
|
||||
|
||||
videoFramesSize = (long) (resultBitrate / 8 * videoDuration);
|
||||
|
||||
if (!isAvc && (resultWidth == originalWidth || resultHeight == originalHeight)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user