如何在 Android 上使用 java/kotlin 缩小视频大小?
How to reduce video size with java/kotlin on Android?
我想减小 Android Studio 的视频大小,上传到 PlayStore 需要与 64 位架构兼容,我之前尝试过使用 ffmpeg,它成功地压缩了 mp4,但需要更长的时间,这个解决方案与3gp 不包括音频。还有其他选项或库可以压缩 mp4 和 3gp 音频和视频吗?
您可以使用此库执行此操作:https://github.com/tcking/GiraffeCompressor
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);
}
})
Here is 另一个压缩视频的库。这个库可以压缩低、中、高质量的视频。使用示例:
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.
}
});
您可能喜欢使用以下库,目前它正在积极维护并且可能有助于最新的 android api。
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.
我想减小 Android Studio 的视频大小,上传到 PlayStore 需要与 64 位架构兼容,我之前尝试过使用 ffmpeg,它成功地压缩了 mp4,但需要更长的时间,这个解决方案与3gp 不包括音频。还有其他选项或库可以压缩 mp4 和 3gp 音频和视频吗?
您可以使用此库执行此操作:https://github.com/tcking/GiraffeCompressor
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);
}
})
Here is 另一个压缩视频的库。这个库可以压缩低、中、高质量的视频。使用示例:
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.
}
});
您可能喜欢使用以下库,目前它正在积极维护并且可能有助于最新的 android api。
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.