mirror of
https://github.com/Myzel394/Alibi.git
synced 2025-06-18 23:05:26 +02:00
refactor: Improve BatchesFolder
This commit is contained in:
parent
5416fcb046
commit
28864ca264
@ -2,9 +2,12 @@ package app.myzel394.alibi.helpers
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.os.ParcelFileDescriptor
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import app.myzel394.alibi.helpers.MediaConverter.Companion.concatenateAudioFiles
|
||||
import app.myzel394.alibi.helpers.MediaConverter.Companion.concatenateVideoFiles
|
||||
import com.arthenica.ffmpegkit.FFmpegKitConfig
|
||||
import java.io.FileDescriptor
|
||||
import java.time.LocalDateTime
|
||||
|
||||
class AudioBatchesFolder(
|
||||
@ -18,9 +21,11 @@ class AudioBatchesFolder(
|
||||
customFolder,
|
||||
subfolderName,
|
||||
) {
|
||||
override val concatenateFunction = ::concatenateAudioFiles
|
||||
override val concatenationFunction = ::concatenateVideoFiles
|
||||
override val ffmpegParameters = FFMPEG_PARAMETERS
|
||||
|
||||
private var customFileFileDescriptor: ParcelFileDescriptor? = null
|
||||
|
||||
override fun getOutputFileForFFmpeg(
|
||||
date: LocalDateTime,
|
||||
extension: String,
|
||||
@ -37,6 +42,28 @@ class AudioBatchesFolder(
|
||||
}
|
||||
}
|
||||
|
||||
override fun cleanup() {
|
||||
runCatching {
|
||||
customFileFileDescriptor?.close()
|
||||
}
|
||||
}
|
||||
|
||||
fun asCustomGetFileDescriptor(
|
||||
counter: Long,
|
||||
fileExtension: String,
|
||||
): FileDescriptor {
|
||||
runCatching {
|
||||
customFileFileDescriptor?.close()
|
||||
}
|
||||
|
||||
val file =
|
||||
getCustomDefinedFolder().createFile("audio/$fileExtension", "$counter.$fileExtension")!!
|
||||
|
||||
customFileFileDescriptor = context.contentResolver.openFileDescriptor(file.uri, "w")!!
|
||||
|
||||
return customFileFileDescriptor!!.fileDescriptor
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun viaInternalFolder(context: Context) = AudioBatchesFolder(context, BatchType.INTERNAL)
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package app.myzel394.alibi.helpers
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import java.io.File
|
||||
import java.time.LocalDateTime
|
||||
@ -18,9 +19,7 @@ abstract class BatchesFolder(
|
||||
open val customFolder: DocumentFile? = null,
|
||||
open val subfolderName: String = ".recordings",
|
||||
) {
|
||||
private var customFileFileDescriptor: ParcelFileDescriptor? = null
|
||||
|
||||
abstract val concatenateFunction: KFunction3<Iterable<String>, String, String, CompletableDeferred<Unit>>
|
||||
abstract val concatenationFunction: KFunction3<Iterable<String>, String, String, CompletableDeferred<Unit>>
|
||||
abstract val ffmpegParameters: Array<String>
|
||||
|
||||
fun initFolders() {
|
||||
@ -34,10 +33,6 @@ abstract class BatchesFolder(
|
||||
}
|
||||
}
|
||||
|
||||
fun cleanup() {
|
||||
customFileFileDescriptor?.close()
|
||||
}
|
||||
|
||||
fun getInternalFolder(): File {
|
||||
return File(context.filesDir, subfolderName)
|
||||
}
|
||||
@ -115,29 +110,33 @@ abstract class BatchesFolder(
|
||||
extension: String,
|
||||
): String
|
||||
|
||||
abstract fun cleanup()
|
||||
|
||||
open suspend fun concatenate(
|
||||
recordingStart: LocalDateTime,
|
||||
extension: String,
|
||||
disableCache: Boolean = false,
|
||||
onNextParameterTry: (String) -> Unit = {},
|
||||
): String {
|
||||
val outputFile = getOutputFileForFFmpeg(
|
||||
date = recordingStart,
|
||||
extension = extension,
|
||||
)
|
||||
|
||||
if (!disableCache && checkIfOutputAlreadyExists(recordingStart, extension)) {
|
||||
return outputFile
|
||||
return getOutputFileForFFmpeg(
|
||||
date = recordingStart,
|
||||
extension = extension,
|
||||
)
|
||||
}
|
||||
|
||||
val filePaths = getBatchesForFFmpeg()
|
||||
|
||||
for (parameter in ffmpegParameters) {
|
||||
Log.i("Concatenation", "Trying parameter $parameter")
|
||||
onNextParameterTry(parameter)
|
||||
|
||||
try {
|
||||
concatenateFunction(
|
||||
val filePaths = getBatchesForFFmpeg()
|
||||
val outputFile = getOutputFileForFFmpeg(
|
||||
date = recordingStart,
|
||||
extension = extension,
|
||||
)
|
||||
|
||||
concatenationFunction(
|
||||
filePaths,
|
||||
outputFile,
|
||||
parameter,
|
||||
@ -203,20 +202,8 @@ abstract class BatchesFolder(
|
||||
}
|
||||
}
|
||||
|
||||
fun asInternalGetOutputPath(counter: Long, fileExtension: String): String {
|
||||
return getInternalFolder().absolutePath + "/$counter.$fileExtension"
|
||||
}
|
||||
|
||||
fun asCustomGetFileDescriptor(
|
||||
counter: Long,
|
||||
fileExtension: String,
|
||||
): FileDescriptor {
|
||||
val file =
|
||||
getCustomDefinedFolder().createFile("audio/$fileExtension", "$counter.$fileExtension")!!
|
||||
|
||||
customFileFileDescriptor = context.contentResolver.openFileDescriptor(file.uri, "w")!!
|
||||
|
||||
return customFileFileDescriptor!!.fileDescriptor
|
||||
fun asInternalGetFile(counter: Long, fileExtension: String): File {
|
||||
return File(getInternalFolder(), "$counter.$fileExtension")
|
||||
}
|
||||
|
||||
enum class BatchType {
|
||||
|
@ -2,6 +2,7 @@ package app.myzel394.alibi.helpers
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.os.ParcelFileDescriptor
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import app.myzel394.alibi.helpers.MediaConverter.Companion.concatenateVideoFiles
|
||||
import com.arthenica.ffmpegkit.FFmpegKitConfig
|
||||
@ -19,9 +20,11 @@ class VideoBatchesFolder(
|
||||
customFolder,
|
||||
subfolderName,
|
||||
) {
|
||||
override val concatenateFunction = ::concatenateVideoFiles
|
||||
override val concatenationFunction = ::concatenateVideoFiles
|
||||
override val ffmpegParameters = FFMPEG_PARAMETERS
|
||||
|
||||
private var customParcelFileDescriptor: ParcelFileDescriptor? = null
|
||||
|
||||
override fun getOutputFileForFFmpeg(date: LocalDateTime, extension: String): String {
|
||||
return when (type) {
|
||||
BatchType.INTERNAL -> asInternalGetOutputFile(date, extension).absolutePath
|
||||
@ -35,6 +38,34 @@ class VideoBatchesFolder(
|
||||
}
|
||||
}
|
||||
|
||||
override fun cleanup() {
|
||||
runCatching {
|
||||
customParcelFileDescriptor?.close()
|
||||
}
|
||||
}
|
||||
|
||||
fun asCustomGetParcelFileDescriptor(
|
||||
counter: Long,
|
||||
fileExtension: String,
|
||||
): ParcelFileDescriptor {
|
||||
runCatching {
|
||||
customParcelFileDescriptor?.close()
|
||||
}
|
||||
|
||||
val file =
|
||||
getCustomDefinedFolder().createFile(
|
||||
"video/$fileExtension",
|
||||
"$counter.$fileExtension"
|
||||
)!!
|
||||
val resolver = context.contentResolver.acquireContentProviderClient(file.uri)!!
|
||||
|
||||
resolver.use {
|
||||
customParcelFileDescriptor = it.openFile(file.uri, "w")!!
|
||||
|
||||
return customParcelFileDescriptor!!
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun viaInternalFolder(context: Context) = VideoBatchesFolder(context, BatchType.INTERNAL)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user