Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Bitrate , Custom Quality and Custom FrameRate to CompressVideo #219

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import com.otaliastudios.transcoder.strategy.TrackStrategy
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.BinaryMessenger
import com.otaliastudios.transcoder.internal.utils.Logger
import com.otaliastudios.transcoder.resize.AtMostResizer
import com.otaliastudios.transcoder.resize.ExactResizer
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
Expand Down Expand Up @@ -83,6 +85,9 @@ class VideoCompressPlugin : MethodCallHandler, FlutterPlugin {
val duration = call.argument<Int>("duration")
val includeAudio = call.argument<Boolean>("includeAudio") ?: true
val frameRate = if (call.argument<Int>("frameRate")==null) 30 else call.argument<Int>("frameRate")
val bitRate = call.argument<Int>("bitRate")
val outputWidth = call.argument<Int>("outputWidth")
val outputHeight = call.argument<Int>("outputHeight")

val tempDir: String = context.getExternalFilesDir("video_compress")!!.absolutePath
val out = SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(Date())
Expand All @@ -94,14 +99,59 @@ class VideoCompressPlugin : MethodCallHandler, FlutterPlugin {
when (quality) {

0 -> {
videoTrackStrategy = DefaultVideoStrategy.atMost(720).build()

assert(value = frameRate != null)
videoTrackStrategy = if (bitRate==null){
DefaultVideoStrategy.Builder()
.keyFrameInterval(3f)
.frameRate(frameRate!!) // will be capped to the input frameRate
.addResizer(AtMostResizer(720))
.build()
}else{
DefaultVideoStrategy.Builder()
.keyFrameInterval(3f)
.bitRate(bitRate.toLong())
.frameRate(frameRate!!) // will be capped to the input frameRate
.addResizer(AtMostResizer(720))
.build()
}
}

1 -> {
videoTrackStrategy = DefaultVideoStrategy.atMost(360).build()

assert(value = frameRate != null)
videoTrackStrategy = if (bitRate==null){
DefaultVideoStrategy.Builder()
.keyFrameInterval(3f)
.frameRate(frameRate!!) // will be capped to the input frameRate
.addResizer(AtMostResizer(360))
.build()
}else{
DefaultVideoStrategy.Builder()
.keyFrameInterval(3f)
.bitRate(bitRate.toLong())
.frameRate(frameRate!!) // will be capped to the input frameRate
.addResizer(AtMostResizer(360))
.build()
}
}
2 -> {
videoTrackStrategy = DefaultVideoStrategy.atMost(640).build()

assert(value = frameRate != null)
videoTrackStrategy = if (bitRate==null){
DefaultVideoStrategy.Builder()
.keyFrameInterval(3f)
.frameRate(frameRate!!) // will be capped to the input frameRate
.addResizer(AtMostResizer(640))
.build()
}else{
DefaultVideoStrategy.Builder()
.keyFrameInterval(3f)
.bitRate(bitRate.toLong())
.frameRate(frameRate!!) // will be capped to the input frameRate
.addResizer(AtMostResizer(640))
.build()
}
}
3 -> {

Expand All @@ -113,17 +163,112 @@ class VideoCompressPlugin : MethodCallHandler, FlutterPlugin {
.build()
}
4 -> {
videoTrackStrategy = DefaultVideoStrategy.atMost(480, 640).build()

assert(value = frameRate != null)
videoTrackStrategy = if (bitRate==null){
DefaultVideoStrategy.Builder()
.keyFrameInterval(3f)
.frameRate(frameRate!!) // will be capped to the input frameRate
.addResizer(AtMostResizer(480,640))
.build()
}else{
DefaultVideoStrategy.Builder()
.keyFrameInterval(3f)
.bitRate(bitRate.toLong())
.frameRate(frameRate!!) // will be capped to the input frameRate
.addResizer(AtMostResizer(480,640))
.build()
}
}
5 -> {
videoTrackStrategy = DefaultVideoStrategy.atMost(540, 960).build()

assert(value = frameRate != null)
videoTrackStrategy = if (bitRate==null){
DefaultVideoStrategy.Builder()
.keyFrameInterval(3f)
.frameRate(frameRate!!) // will be capped to the input frameRate
.addResizer(AtMostResizer(540,960))
.build()
}else{
DefaultVideoStrategy.Builder()
.keyFrameInterval(3f)
.bitRate(bitRate.toLong())
.frameRate(frameRate!!) // will be capped to the input frameRate
.addResizer(AtMostResizer(540,960))
.build()
}
}
6 -> {
videoTrackStrategy = DefaultVideoStrategy.atMost(720, 1280).build()

assert(value = frameRate != null)
videoTrackStrategy = if (bitRate==null){
DefaultVideoStrategy.Builder()
.keyFrameInterval(3f)
.frameRate(frameRate!!) // will be capped to the input frameRate
.addResizer(AtMostResizer(720, 1280))
.build()
}else{
DefaultVideoStrategy.Builder()
.keyFrameInterval(3f)
.bitRate(bitRate.toLong())
.frameRate(frameRate!!) // will be capped to the input frameRate
.addResizer(AtMostResizer(720,1280))
.build()
}
}
7 -> {
videoTrackStrategy = DefaultVideoStrategy.atMost(1080, 1920).build()
}

assert(value = frameRate != null)
videoTrackStrategy = if (bitRate==null){
DefaultVideoStrategy.Builder()
.keyFrameInterval(3f)
.frameRate(frameRate!!) // will be capped to the input frameRate
.addResizer(AtMostResizer(1080,1920))
.build()
}else{
DefaultVideoStrategy.Builder()
.keyFrameInterval(3f)
.bitRate(bitRate.toLong())
.frameRate(frameRate!!) // will be capped to the input frameRate
.addResizer(AtMostResizer(1080,1920))
.build()
}
}
8 -> {

assert(value = frameRate != null)
if(outputWidth != null && outputHeight != null) {
videoTrackStrategy = if (bitRate == null) {
DefaultVideoStrategy.Builder()
.keyFrameInterval(3f)
.frameRate(frameRate!!) // will be capped to the input frameRate
.addResizer(ExactResizer(outputWidth, outputHeight))
.build()
} else {
DefaultVideoStrategy.Builder()
.keyFrameInterval(3f)
.bitRate(bitRate.toLong())
.frameRate(frameRate!!) // will be capped to the input frameRate
.addResizer(ExactResizer(outputWidth, outputHeight))
.build()
}
}else{
videoTrackStrategy = if (bitRate==null){
DefaultVideoStrategy.Builder()
.keyFrameInterval(3f)
.frameRate(frameRate!!) // will be capped to the input frameRate
.addResizer(AtMostResizer(720, 1280))
.build()
}else{
DefaultVideoStrategy.Builder()
.keyFrameInterval(3f)
.bitRate(bitRate.toLong())
.frameRate(frameRate!!) // will be capped to the input frameRate
.addResizer(AtMostResizer(720,1280))
.build()
}
}
}
}

audioTrackStrategy = if (includeAudio) {
Expand Down
16 changes: 15 additions & 1 deletion lib/src/video_compress/video_compressor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,16 @@ extension Compress on IVideoCompress {
/// compress video from [path] return [Future<MediaInfo>]
///
/// you can choose its quality by [quality],
///
/// Note : VideoQuality.CustomQuality is available only in Android platform.
///
/// determine whether to delete his source file by [deleteOrigin]
/// optional parameters [startTime] [duration] [includeAudio] [frameRate]
///
/// optional parameters : [startTime] , [duration] , [includeAudio] , [frameRate]
///
/// Android specific parameters : [bitRate] , [outputWidth] , [outputHeight]
///
/// Note : if you don't choose VideoQuality.CustomQuality , [outputWidth] and [outputHeight] will be ignored.
///
/// ## example
/// ```dart
Expand All @@ -128,6 +136,9 @@ extension Compress on IVideoCompress {
int? duration,
bool? includeAudio,
int frameRate = 30,
int? bitRate,
int? outputWidth,
int? outputHeight
}) async {
if (isCompressing) {
throw StateError('''VideoCompress Error:
Expand All @@ -150,6 +161,9 @@ extension Compress on IVideoCompress {
'duration': duration,
'includeAudio': includeAudio,
'frameRate': frameRate,
'bitRate': bitRate,
'outputWidth': outputWidth,
'outputHeight': outputHeight,
});

// ignore: invalid_use_of_protected_member
Expand Down
3 changes: 2 additions & 1 deletion lib/src/video_compress/video_quality.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ enum VideoQuality {
Res640x480Quality,
Res960x540Quality,
Res1280x720Quality,
Res1920x1080Quality
Res1920x1080Quality,
CustomQuality
}