Skip to content

Commit

Permalink
- add percent progress
Browse files Browse the repository at this point in the history
- using min-gpl library
  • Loading branch information
bayu committed Dec 30, 2021
1 parent daa547f commit 966958c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 57 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class MainActivity : AppCompatActivity() {

private fun initInnoVideoConverter() {
innoVideoConverter = InnoVideoConverter(this, object : InnoVideoConverterCallback {
override fun onProgress(progress: Boolean) {
override fun onProgress(progress: Boolean, percent: Double) {

}
override fun onSuccessConverted(message: String, newUriFileConverted: String) {
Expand All @@ -78,7 +78,11 @@ class MainActivity : AppCompatActivity() {
2. Call the function of convert filter as you need from object innoVideoConverter.

```kotlin
innoVideoConverter.convertFilterPixelFormat(fileUriVideo, "yuv444p")
innoVideoConverter.compressVideoQuality(
fileUriVideo,
QualityOption.LOW,
InnoVideoScale(-2, 720)
)
```

## License
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class MainActivity : AppCompatActivity() {
innoVideoConverter.compressVideoQuality(
fileUriVideo,
QualityOption.LOW,
InnoVideoScale(-1, 720)
InnoVideoScale(-2, 720)
)
}
}
Expand All @@ -135,7 +135,8 @@ class MainActivity : AppCompatActivity() {

private fun initInnoVideoConverter() {
innoVideoConverter = InnoVideoConverter(this, object : InnoVideoConverterCallback {
override fun onProgress(progress: Boolean) {
override fun onProgress(progress: Boolean, percent : Double) {
Log.e(InnoVideoConverter.TAG, "percentProgress $percent")
if (progress) {
binding.btnCancel.visibility = View.VISIBLE
binding.tvStateProcess.text = "Compressing file"
Expand Down
3 changes: 2 additions & 1 deletion inno-video-converter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ android {
}

dependencies {
implementation 'com.arthenica:ffmpeg-kit-full:4.5.LTS'
implementation 'com.arthenica:ffmpeg-kit-min-gpl:4.5.LTS'

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mncgroup.innovideoconverter

import android.app.Activity
import android.media.MediaPlayer
import android.net.Uri
import android.util.Log
import com.arthenica.ffmpegkit.FFmpegKit
Expand Down Expand Up @@ -30,16 +31,15 @@ class InnoVideoConverter(
val inputFile = FFmpegKitConfig.getSafParameterForRead(activity, fileUriVideo)
val file = getFileCacheDir()
val crf = when (qualityOption) {
QualityOption.ULTRA_HIGH -> "0"
QualityOption.VERY_HIGH -> "10"
QualityOption.HIGH -> "18"
QualityOption.VERY_HIGH -> "17"
QualityOption.HIGH -> "20"
QualityOption.MEDIUM -> "23"
QualityOption.LOW -> "28"
QualityOption.VERY_LOW -> "33"
QualityOption.LOW -> "25"
QualityOption.VERY_LOW -> "28"
}
val exe =
"-y -i " + inputFile + " -vf scale=${scale.width}:${scale.height} -preset veryfast -crf $crf " + file.absolutePath
executeCommandAsync(exe, file.absolutePath)
executeCommandAsync(fileUriVideo, exe, file.absolutePath)
}

/**
Expand All @@ -54,60 +54,72 @@ class InnoVideoConverter(
return File(folder, System.currentTimeMillis().toString() + ".mp4")
}

private fun executeCommandAsync(command: String, filePath: String) {
callback.onProgress(true)
FFmpegKit.executeAsync(command,
{ session ->
val state = session.state
val returnCode = session.returnCode
// CALLED WHEN SESSION IS EXECUTED
Log.d(
TAG,
java.lang.String.format(
"FFmpeg process exited with state %s and rc %s.%s",
state,
returnCode,
session.failStackTrace
private fun executeCommandAsync(fileUriVideo: Uri, command: String, filePath: String) {
try {
val duration = MediaPlayer.create(activity, fileUriVideo).duration.toDouble()

callback.onProgress(true, 0.0)
FFmpegKit.executeAsync(command,
{ session ->
val state = session.state
val returnCode = session.returnCode
// CALLED WHEN SESSION IS EXECUTED
Log.d(
TAG,
java.lang.String.format(
"FFmpeg process exited with state %s and rc %s.%s",
state,
returnCode,
session.failStackTrace
)
)
)
activity.runOnUiThread {
callback.onProgress(false)
activity.runOnUiThread {
callback.onProgress(false, 100.0)

when {
returnCode.isSuccess -> {
callback.onSuccessConverted(
"Success compressed",
filePath
)
}
returnCode.isError -> {
callback.onErrorConvert(
"Error compress. ${session.logsAsString}"
)
}
else -> {
callback.onCanceledConvert(
"Canceled compress by user"
)
when {
returnCode.isSuccess -> {
callback.onSuccessConverted(
"Success compressed",
filePath
)
}
returnCode.isError -> {
callback.onErrorConvert(
"Error compress. ${session.logsAsString}"
)
}
else -> {
callback.onCanceledConvert(
"Canceled compress by user"
)
}
}
}
}, {
// CALLED WHEN SESSION PRINTS LOGS
if (BuildConfig.DEBUG) {
Log.i(TAG, "LOG : ${it.message}")
}
}) {
// CALLED WHEN SESSION GENERATES STATISTICS
activity.runOnUiThread {
val percent: Double = (it.time.toDouble() / duration) * 100
callback.onProgress(true, percent)
}
}, {
// CALLED WHEN SESSION PRINTS LOGS
if (BuildConfig.DEBUG) {
Log.i(TAG, "LOG : ${it.message}")
Log.i(TAG, "STATS : $it")
}
}) {
// CALLED WHEN SESSION GENERATES STATISTICS
if (BuildConfig.DEBUG) {
Log.i(TAG, "STATS : $it")
}
} catch (e: Exception) {
callback.onErrorConvert(e.message ?: e.localizedMessage ?: e.toString())
e.printStackTrace()
}

}
}

interface InnoVideoConverterCallback {
fun onProgress(progress: Boolean)
fun onProgress(progress: Boolean, percent: Double)
fun onSuccessConverted(message: String, newUriFileConverted: String)
fun onErrorConvert(message: String)
fun onCanceledConvert(message: String)
Expand All @@ -120,10 +132,8 @@ interface InnoVideoConverterCallback {
* [MEDIUM] for medium quality of video
* [HIGH] for high quality of video.
* [VERY_HIGH] for very high quality of video.
* [ULTRA_HIGH] for ultra high quality of video.
*/
enum class QualityOption {
ULTRA_HIGH,
VERY_HIGH,
HIGH,
MEDIUM,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package com.mncgroup.innovideoconverter

/**
* scale video
* @property width scale widht of video, set -1 will tell app to automatically choose the correct width.
* @property height scale height of video, set -1 will tell app to automatically choose the correct height.
* @property width scale widht of video, set -2 will tell app to automatically choose the correct width.
* @property height scale height of video, set -2 will tell app to automatically choose the correct height.
*
* Example scale:
* -1:720 : will tell app to scale height to 720 and thw width will automatically choose the correct width
* -2:720 : will tell app to scale height to 720 and thw width will automatically choose the correct width
*/
class InnoVideoScale(
var width: Int,
Expand Down

0 comments on commit 966958c

Please sign in to comment.