A lightweight Android library for compressing video files using MediaCodec.
Based on the LightCompressor and uses some of its parts. The API is inspired by Android ImageDecoder.
- Compress video files using H.264 codecs via MediaCodec
- Inspect video metadata and apply settings before compression
- Coroutines and cancellation
- Compatible with Android 5.0+ (API 21+)
Add the JitPack repository to your project level build.gradle.kts:
allprojects {
repositories {
google()
maven(url = "https://jitpack.io")
}
}Add this to your app build.gradle.kts:
dependencies {
implementation("com.github.pachca:VideoCompressor:v2.0.3")
}scope.launch {
val result = VideoCompressor.compress(
context = context,
// Input File
input = input,
// Output file (make sure the app can actually write at this path)
output = output,
// Callback is fired before the actual compression.
// Allows setting parameters like video resolution and bitrate.
onMetadataDecoded = { compressor, metadata ->
// Return a CompressionSettings instance to proceed or null to cancel
CompressionSettings.Builder()
.setTargetSize(
width = metadata.actualWidth / 2,
height = metadata.actualHeight / 2
)
.setBitrate(2_000_000)
.setStreamable(true)
.allowSizeAdjustments(true)
.setEncoderSelectionMode(EncoderSelectionMode.TRY_ALL)
.build()
}
)
}Minimum Android SDK: VideoCompressor requires a minimum API level of 21.
- Improve logging
- Extend available metadata and encoding settings
- Switch to Asynchronous processing
- H.265 (Maybe?)
Telegram for Android.
LightCompressor - original library.