From 491a0740f1e6470b5038c0b29e5d54e2daa110e4 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sun, 6 Aug 2023 00:06:28 +0200 Subject: [PATCH] feat: Implement audio strip functionality --- .../locationtest/services/RecorderService.kt | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/app/myzel394/locationtest/services/RecorderService.kt b/app/src/main/java/app/myzel394/locationtest/services/RecorderService.kt index 14e9438..c0305cd 100644 --- a/app/src/main/java/app/myzel394/locationtest/services/RecorderService.kt +++ b/app/src/main/java/app/myzel394/locationtest/services/RecorderService.kt @@ -18,6 +18,7 @@ import androidx.core.content.ContextCompat import app.myzel394.locationtest.R import app.myzel394.locationtest.dataStore import app.myzel394.locationtest.db.AudioRecorderSettings +import app.myzel394.locationtest.ui.utils.formatDuration import com.arthenica.ffmpegkit.FFmpegKit import com.arthenica.ffmpegkit.ReturnCode import kotlinx.coroutines.CoroutineScope @@ -109,9 +110,34 @@ class RecorderService: Service() { } } + private fun stripConcatenatedFileToExactDuration( + outputFile: File + ) { + // Move the concatenated file to a temporary file + val rawFile = File("$fileFolder/${outputFile.nameWithoutExtension}-raw.${settings.fileExtension}") + outputFile.renameTo(rawFile) + + val command = "-sseof ${settings.maxDuration / -1000} -i $rawFile -y $outputFile" + + val session = FFmpegKit.execute(command) + + if (!ReturnCode.isSuccess(session.returnCode)) { + Log.d( + "Audio Concatenation", + String.format( + "Command failed with state %s and rc %s.%s", + session.getState(), + session.getReturnCode(), + session.getFailStackTrace() + ) + ) + + throw Exception("Failed to strip concatenated audio") + } + } + fun concatenateFiles(forceConcatenation: Boolean = false): File { val paths = filePaths.joinToString("|") - println("Concatenating files: $paths") val fileName = recordingStart!! .format(ISO_DATE_TIME) .toString() @@ -123,7 +149,7 @@ class RecorderService: Service() { return outputFile } - val command = "-i 'concat:$paths'" + + val command = "-i 'concat:$paths' -y" + " -acodec copy" + " -metadata title='$fileName' " + " -metadata date='${recordingStart!!.format(ISO_DATE_TIME)}'" + @@ -148,6 +174,11 @@ class RecorderService: Service() { throw Exception("Failed to concatenate audios") } + val minRequiredForPossibleInExactMaxDuration = settings.maxDuration / settings.intervalDuration + if (settings.forceExactMaxDuration && filePaths.size > minRequiredForPossibleInExactMaxDuration) { + stripConcatenatedFileToExactDuration(outputFile) + } + return outputFile } @@ -332,6 +363,7 @@ class RecorderService: Service() { data class Settings( val maxDuration: Long, val intervalDuration: Long, + val forceExactMaxDuration: Boolean, val bitRate: Int, val samplingRate: Int, val outputFormat: Int, @@ -359,6 +391,7 @@ data class Settings( outputFormat = audioRecorderSettings.getOutputFormat(), encoder = audioRecorderSettings.getEncoder(), maxDuration = audioRecorderSettings.maxDuration, + forceExactMaxDuration = audioRecorderSettings.forceExactMaxDuration, ) } }