temp fix full audio ffmpeg-kit

This commit is contained in:
shabinder 2021-08-25 16:23:17 +05:30
parent 363f0663c7
commit 487a27c4d3
4 changed files with 60 additions and 37 deletions

View File

@ -18,9 +18,9 @@
object Versions {
// App's Version (To be bumped at each update)
const val versionName = "3.2.1"
const val versionName = "3.2.11"
const val versionCode = 22
const val versionCode = 23
// Kotlin
const val kotlinVersion = "1.5.21"

View File

@ -19,7 +19,7 @@ kotlin {
dependencies {
implementation(Extras.mp3agic)
implementation(Extras.Android.countly)
implementation(project(":ffmpeg-kit:android:ffmpeg-kit-android-lib"))
implementation("com.arthenica:ffmpeg-kit-audio:4.4.LTS")
//api(files("$rootDir/libs/mobile-ffmpeg.aar"))
}
}

View File

@ -65,7 +65,8 @@ class AndroidFileManager(
spotiFlyerDatabase: SpotiFlyerDatabase
) : FileManager {
@Suppress("DEPRECATION")
private val defaultBaseDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).toString()
private val defaultBaseDir =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).toString()
override fun fileSeparator(): String = File.separator
@ -124,7 +125,8 @@ class AndroidFileManager(
} catch (e: Exception) {
// Media File Isn't MP3 lets Convert It first
if (e is InvalidDataException) {
val convertedFilePath = songFile.absolutePath.substringBeforeLast('.') + ".temp.mp3"
val convertedFilePath =
songFile.absolutePath.substringBeforeLast('.') + ".temp.mp3"
val conversionResult = mediaConverter.convertAudioFile(
inputFilePath = songFile.absolutePath,
@ -138,9 +140,13 @@ class AndroidFileManager(
.setId3v2TagsAndSaveFile(trackDetails, trackDetails.outputFilePath)
addToLibrary(trackDetails.outputFilePath)
}.failure {
File(convertedFilePath).delete()
}.fold(
success = {},
failure = {
throw it
}
)
} else throw e
}
SuspendableEvent.success(trackDetails.outputFilePath)
@ -154,7 +160,8 @@ class AndroidFileManager(
override fun addToLibrary(path: String) = methods.value.platformActions.addToLibrary(path)
override suspend fun loadImage(url: String, reqWidth: Int, reqHeight: Int): Picture = withContext(dispatcherIO) {
override suspend fun loadImage(url: String, reqWidth: Int, reqHeight: Int): Picture =
withContext(dispatcherIO) {
val cachePath = imageCacheDir() + getNameURL(url)
Picture(
image = (loadCachedImage(cachePath, reqWidth, reqHeight) ?: freshImage(
@ -186,7 +193,8 @@ class AndroidFileManager(
}
@Suppress("BlockingMethodInNonBlockingContext")
private suspend fun freshImage(url: String, reqWidth: Int, reqHeight: Int): Bitmap? = withContext(dispatcherIO) {
private suspend fun freshImage(url: String, reqWidth: Int, reqHeight: Int): Bitmap? =
withContext(dispatcherIO) {
try {
val source = URL(url)
val connection: HttpURLConnection = source.openConnection() as HttpURLConnection
@ -200,7 +208,10 @@ class AndroidFileManager(
parallelExecutor.executeSuspending {
// Decode and Cache Full Sized Image in Background
cacheImage(BitmapFactory.decodeByteArray(input, 0, input.size), imageCacheDir() + getNameURL(url))
cacheImage(
BitmapFactory.decodeByteArray(input, 0, input.size),
imageCacheDir() + getNameURL(url)
)
}
bitmap // return Memory Efficient Bitmap
} catch (e: Exception) {

View File

@ -1,11 +1,18 @@
package com.shabinder.common.core_components.media_converter
import android.util.Log
import com.arthenica.ffmpegkit.FFmpegKit
import com.arthenica.ffmpegkit.ReturnCode
import com.shabinder.common.models.AudioQuality
import com.shabinder.common.models.SpotiFlyerException
import org.koin.dsl.bind
import org.koin.dsl.module
import com.arthenica.ffmpegkit.FFprobeKit
import com.arthenica.ffmpegkit.MediaInformationSession
import kotlin.math.ceil
import kotlin.math.roundToInt
class AndroidMediaConverter : MediaConverter() {
override suspend fun convertAudioFile(
@ -14,10 +21,15 @@ class AndroidMediaConverter : MediaConverter() {
audioQuality: AudioQuality,
progressCallbacks: (Long) -> Unit,
) = executeSafelyInPool {
val kbpsArg = if (audioQuality == AudioQuality.UNKNOWN) "" else "-b:a ${audioQuality.kbps}k"
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 -vn $outputFilePath"
"-i $inputFilePath -y $kbpsArg -acodec libmp3lame -vn $outputFilePath"
)
when (session.returnCode.value) {