-
-
Notifications
You must be signed in to change notification settings - Fork 793
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
45 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 43 additions & 23 deletions
66
...Main/kotlin/com/shabinder/common/core_components/media_converter/AndroidMediaConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,68 @@ | ||
package com.shabinder.common.core_components.media_converter | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import com.shabinder.common.models.AudioQuality | ||
import com.shabinder.common.models.SpotiFlyerException | ||
import kotlinx.coroutines.delay | ||
import nl.bravobit.ffmpeg.ExecuteBinaryResponseHandler | ||
import nl.bravobit.ffmpeg.FFmpeg | ||
import org.koin.dsl.bind | ||
import org.koin.dsl.module | ||
|
||
|
||
class AndroidMediaConverter : MediaConverter() { | ||
class AndroidMediaConverter(private val appContext: Context) : MediaConverter() { | ||
|
||
override suspend fun convertAudioFile( | ||
inputFilePath: String, | ||
outputFilePath: String, | ||
audioQuality: AudioQuality, | ||
progressCallbacks: (Long) -> Unit, | ||
) = executeSafelyInPool { | ||
var progressing = true | ||
var timeout = 600_000L * 2 // 20 min | ||
val progressDelayCheck = 500L | ||
// 192 is Default | ||
val audioBitrate = | ||
if (audioQuality == AudioQuality.UNKNOWN) 192 else audioQuality.kbps.toIntOrNull() | ||
?: 192 | ||
FFmpeg.getInstance(appContext).execute( | ||
arrayOf( | ||
"-i", | ||
inputFilePath, | ||
"-y", /*"-acodec", "libmp3lame",*/ | ||
"-b:a", | ||
"${audioBitrate}k", | ||
"-vn", | ||
outputFilePath | ||
), object : ExecuteBinaryResponseHandler() { | ||
override fun onSuccess(message: String?) { | ||
//Log.d("FFmpeg Command", "Success $message") | ||
progressing = false | ||
} | ||
|
||
"" | ||
//runTranscode(inputFilePath,outputFilePath,audioBitrate).toString() | ||
/*val kbpsArg = if (audioQuality == AudioQuality.UNKNOWN) { | ||
val mediaInformation = FFprobeKit.getMediaInformation(inputFilePath) | ||
val bitrate = ((mediaInformation.mediaInformation.bitrate).toFloat()/1000).roundToInt() | ||
Log.d("MEDIA-INPUT Bit", bitrate.toString()) | ||
"-b:a ${bitrate}k" | ||
} else "-b:a ${audioQuality.kbps}k" | ||
// -acodec libmp3lame | ||
val session = FFmpegKit.execute( | ||
"-i $inputFilePath -y $kbpsArg -acodec libmp3lame -vn $outputFilePath" | ||
) | ||
override fun onProgress(message: String?) { | ||
super.onProgress(message) | ||
Log.d("FFmpeg Progress", "Progress $message --- $inputFilePath") | ||
} | ||
|
||
when (session.returnCode.value) { | ||
ReturnCode.SUCCESS -> { | ||
//FFMPEG task Completed | ||
outputFilePath | ||
} | ||
ReturnCode.CANCEL -> { | ||
throw SpotiFlyerException.MP3ConversionFailed("FFmpeg Conversion Canceled for $inputFilePath") | ||
override fun onFailure(message: String?) { | ||
Log.d("FFmpeg Command", "Failed $message") | ||
progressing = false | ||
throw SpotiFlyerException.MP3ConversionFailed(message = "Android FFmpeg Failed: $message") | ||
} | ||
} | ||
else -> throw SpotiFlyerException.MP3ConversionFailed("FFmpeg Conversion Failed for $inputFilePath") | ||
}*/ | ||
) | ||
while (progressing) { | ||
if (timeout < 0) throw SpotiFlyerException.MP3ConversionFailed("Conversion Timeout for $inputFilePath") | ||
delay(progressDelayCheck) | ||
timeout -= progressDelayCheck | ||
} | ||
// Return output file path after successful conversion | ||
outputFilePath | ||
} | ||
} | ||
|
||
internal actual fun mediaConverterModule() = module { | ||
single { AndroidMediaConverter() } bind MediaConverter::class | ||
single { AndroidMediaConverter(get()) } bind MediaConverter::class | ||
} |