我想在Android Studio中减小视频大小,并且为了上传到PlayStore需要兼容64位架构,之前尝试过使用ffmpeg成功地压缩了mp4,但花费的时间较长,而使用3gp格式的解决方案则不包括音频。是否有其他选项或库可以同时压缩带有音视频的mp4和3gp呢?
我想在Android Studio中减小视频大小,并且为了上传到PlayStore需要兼容64位架构,之前尝试过使用ffmpeg成功地压缩了mp4,但花费的时间较长,而使用3gp格式的解决方案则不包括音频。是否有其他选项或库可以同时压缩带有音视频的mp4和3gp呢?
https://github.com/AbedElazizShe/LightCompressor
使用方法如下(Java)
implementation 'com.github.AbedElazizShe:LightCompressor:1.1.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.3'
public static void compressVideo(Context context,
Activity activity,
ArrayList<Uri> videoUris) {
VideoCompressor.start(context,
// => This is required
videoUris,
// => Source can be provided as content uris
true,
// => isStreamable
Environment.DIRECTORY_MOVIES,
// => the directory to save the compressed video(s)
new CompressionListener() {
@Override
public void onSuccess(int i,
long l,
@org.jetbrains.annotations.Nullable String s) {
// On Compression success
Log.d("TAG",
"videoCompress i: " +i);
Log.d("TAG",
"videoCompress l: " +l);
Log.d("TAG",
"videoCompress s: " +s);
}
@Override
public void onStart(int i) {
// Compression start
}
@Override
public void onFailure(int index,
String failureMessage) {
// On Failure
}
@Override
public void onProgress(int index,
float progressPercent) {
// Update UI with progress value
activity.runOnUiThread(new Runnable() {
public void run() {
}
});
}
@Override
public void onCancelled(int index) {
// On Cancelled
}
},
new Configuration(VideoQuality.LOW,
24, /*frameRate: int, or null*/
false, /*isMinBitrateCheckEnabled*/
null, /*videoBitrate: int, or null*/
false, /*disableAudio: Boolean, or null*/
false, /*keepOriginalResolution: Boolean, or null*/
360.0, /*videoWidth: Double, or null*/
480.0 /*videoHeight: Double, or null*/));
}
配置值
VideoQuality: VERY_HIGH (original-bitrate * 0.6) , HIGH (original-bitrate * 0.4), MEDIUM (original-bitrate * 0.3), LOW (original-bitrate * 0.2), OR VERY_LOW (original-bitrate * 0.1)
isMinBitrateCheckEnabled: this means, don't compress if bitrate is less than 2mbps
frameRate: any fps value
videoBitrate: any custom bitrate value
disableAudio: true/false to generate a video without audio. False by default.
keepOriginalResolution: true/false to tell the library not to change the resolution.
videoWidth: custom video width.
videoHeight: custom video height.
这里有另一个视频压缩库。该库可以在低、中和高质量下压缩视频。以下是使用示例:
VideoCompress.compressVideoMedium("/storage/emulated/0/Movies/source.mp4",
"/storage/emulated/0/Movies/Compressed/compressed.mp4",
new VideoCompress.CompressListener() {
@Override
public void onStart() {
// Compression is started.
}
@Override
public void onSuccess() {
// Compression is successfully finished.
}
@Override
public void onFail() {
// Compression is failed.
}
@Override
public void onProgress(float percent) {
// Compression is in progress.
}
});
videocompressor
的Gradle模块,它就是库本身。您可以从那里复制源代码。这里是源代码的路径。 - Yamashiro Rion
GiraffeCompressor.init(context);
//step 4: using compressor
GiraffeCompressor.create() //two implementations: mediacodec and ffmpeg,default is mediacodec
.input(inputFile) //set video to be compressed
.output(outputFile) //set compressed video output
.bitRate(bitRate)//set bitrate 码率
.resizeFactor(Float.parseFloat($.id(R.id.et_resize_factor).text()))//set video resize factor 分辨率缩放,默认保持原分辨率
.watermark("/sdcard/videoCompressor/watermarker.png")//add watermark(take a long time) 水印图片(需要长时间处理)
.ready()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<GiraffeCompressor.Result>() {
@Override
public void onCompleted() {
$.id(R.id.btn_start).enabled(true).text("start compress");
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
$.id(R.id.btn_start).enabled(true).text("start compress");
$.id(R.id.tv_console).text("error:"+e.getMessage());
}
@Override
public void onNext(GiraffeCompressor.Result s) {
String msg = String.format("compress completed \ntake time:%s \nout put file:%s", s.getCostTime(), s.getOutput());
msg = msg + "\ninput file size:"+ Formatter.formatFileSize(getApplication(),inputFile.length());
msg = msg + "\nout file size:"+ Formatter.formatFileSize(getApplication(),new File(s.getOutput()).length());
System.out.println(msg);
$.id(R.id.tv_console).text(msg);
}
})