Helps to trim local videos with many customizations on Android applications using exoplayer2 and FFmpeg Demo app.
For a working implementation, please have a look at the Sample Project
- Include the library as local library project.
- Add the dependency to your app
build.gradle
file - Take a look at light weight version of this library Android-video-trimmer-litr
dependencies {
implementation 'com.github.a914-gowtham:android-video-trimmer:1.7.0'
}
- Add to project's root
build.gradle
file:
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
- Create a global variable for ActivityResultLauncher
//Java
ActivityResultLauncher<Intent> startForResult = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK &&
result.getData() != null) {
Uri uri = Uri.parse(TrimVideo.getTrimmedVideoPath(result.getData()));
Log.d(TAG, "Trimmed path:: " + uri);
} else
LogMessage.v("videoTrimResultLauncher data is null");
});
//Kotlin
val startForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
result: ActivityResult ->
if (result.resultCode == Activity.RESULT_OK &&
result.getData() != null) {
Uri uri = Uri.parse(TrimVideo.getTrimmedVideoPath(result.getData()))
Log.d(TAG, "Trimmed path:: " + uri)
}else
LogMessage.v("videoTrimResultLauncher data is null");
}
- Add the code for opening Trim Activity.
TrimVideo.activity(String.valueOf(videoUri))
// .setCompressOption(new CompressOption()) //empty constructor for default compress option
.setHideSeekBar(true)
.start(this,startForResult);
.setCompressOption(new CompressOption(frameRate,bitRate,width,height)) //pass empty constructor for default compressoption
FrameRate
Recommeded frameRate is 30BitRate
Bitrate Can be between 150k to 1000k or 1M to 10M.Lower bitrate can reduce the quality and size of the video. Use 1M for better quality outputWidth
Width of the video output video.Height
Height of the video output video.UseTrimmerUtils.getVideoWidthHeight(this,Uri.parse(videoUri));
method to get the width and height of the video
- No need to use accurateCut while using video compressOption
- Video compressing process will take more time
.setCompressOption(new CompressOption(30,"1M",460,320))
//You could divide the width and height by 2. when try to compress a large resolution videos ex:Taken from camera
/*int[] wAndh=TrimmerUtils.getVideoWidthHeight(this,Uri.parse(videoUri));
int width=wAndh[0];
int height=wAndh[1];
if(wAndh[0]>800){
width/=2;
width/=2;
.setCompressOption(new CompressOption(30,"1M",width,height))
}else
.setCompressOption(new CompressOption(30,"400k",width,height))
*/
.setAccurateCut(true) //default value is false
- AccurateCut false makes video trimming faster and less accuracy(approx. 1-3secs)
- AccurateCut true makes video trimming slower and high accuracy
.setHideSeekBar(true) //default value is false
TrimVideo.activity(videoUri)
.start(this,startForResult);
TrimVideo.activity(videoUri)
.setTrimType(TrimType.FIXED_DURATION)
.setFixedDuration(30) //seconds
.start(this,startForResult);
TrimVideo.activity(videoUri)
.setTrimType(TrimType.MIN_DURATION)
.setMinDuration(30) //seconds
.start(this,startForResult);
TrimVideo.activity(videoUri)
.setTrimType(TrimType.MIN_MAX_DURATION)
.setMinToMax(10, 30) //seconds
.start(this,startForResult);
-dontwarn com.gowtham.library**
-keep class com.gowtham.library** { *; }
-keep interface com.gowtham.library** { *; }
- Library - Android Kitkat 4.4+ (API 19)
- Sample - Android Kitkat 4.4+ (API 19)
- Video stretch issue 2 #46
- Video stretch issue #46
- java.lang.NoClassDefFoundError Issue fix #45 that only happens on samsung devices
- Arabic localization and custom toolbar title
- Compression issue fixed
- Ffmpeg-mobile version downgraded to 4.3.1-LTS to rectify release build issue
- Write permission and setDestination removed due to the android 11 issues
- Thumbnail loading optimized
- Controller seekbar bug fixed
- Audio focus added
- Bug fixes on Android 11
- ui improvements
- Exoplayer version updated to 2.12.1
- Toolbar title softcoded
- Can be started from fragment
- Added Custom fileName for the output video
- FFmpegMedia lib changed into Mobile-ffmpeg
- Library size reduced
- Library size : 12mb(release build)
- Constants class name changed into TrimmerConstants
- Added TrimmerUtils class which has getVideoDuration() method and some more VideoUtils methods
- Fixed issue on Android Q.
- Library size : 26mb(debug build).
- Small UI updates.
- Couple new things to configure.
- Initial Build
Show your support by giving a star to this repository.
There are many ways of improving and adding more features, so feel free to collaborate with ideas, issues and/or pull requests.