mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-07-09 06:08:09 +02:00
feat(download): AC-4 passthrough support
Decrypt AC-4 via the FFmpeg mov muxer with a -f mov fallback, then repair the output to a standards-compliant ISO MP4: inject the dac4 config box from the encrypted source, normalize the QuickTime container/sample entry, and write iTunes metadata (incl. cover and lyrics) natively. Codec-keyed and generic, so it applies to any extension that returns AC-4 streams. Wired through PlatformBridge/MainActivity for both SAF and local decrypt paths.
This commit is contained in:
@@ -2680,6 +2680,33 @@ class MainActivity: FlutterFragmentActivity() {
|
||||
}
|
||||
result.success(response)
|
||||
}
|
||||
"ensureAC4Config" -> {
|
||||
val filePath = call.argument<String>("file_path") ?: ""
|
||||
val sourcePath = call.argument<String>("source_path") ?: ""
|
||||
val response = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
Gobackend.ensureAC4Config(filePath, sourcePath)
|
||||
} catch (e: Exception) {
|
||||
android.util.Log.e("SpotiFLAC", "ensureAC4Config failed: ${e.message}", e)
|
||||
"""{"error":"${e.message?.replace("\"", "'")}"}"""
|
||||
}
|
||||
}
|
||||
result.success(response)
|
||||
}
|
||||
"writeAC4Metadata" -> {
|
||||
val filePath = call.argument<String>("file_path") ?: ""
|
||||
val metadataJson = call.argument<String>("metadata_json") ?: "{}"
|
||||
val coverPath = call.argument<String>("cover_path") ?: ""
|
||||
val response = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
Gobackend.writeAC4Metadata(filePath, metadataJson, coverPath)
|
||||
} catch (e: Exception) {
|
||||
android.util.Log.e("SpotiFLAC", "writeAC4Metadata failed: ${e.message}", e)
|
||||
"""{"error":"${e.message?.replace("\"", "'")}"}"""
|
||||
}
|
||||
}
|
||||
result.success(response)
|
||||
}
|
||||
"writeTempToSaf" -> {
|
||||
val tempPath = call.argument<String>("temp_path") ?: ""
|
||||
val safUri = call.argument<String>("saf_uri") ?: ""
|
||||
|
||||
@@ -422,16 +422,19 @@ object NativeDownloadFinalizer {
|
||||
try {
|
||||
for (candidate in decryptionKeyCandidates(key)) {
|
||||
checkCancelled(shouldCancel)
|
||||
val attempts = mutableListOf<Pair<String, Boolean>>()
|
||||
attempts.add(outputPath to (preferredExt == ".flac"))
|
||||
val attempts = mutableListOf<Triple<String, Boolean, Boolean>>()
|
||||
attempts.add(Triple(outputPath, preferredExt == ".flac", false))
|
||||
if (preferredExt == ".flac") {
|
||||
attempts.add(buildOutputPath(localInput, ".m4a") to false)
|
||||
attempts.add(Triple(buildOutputPath(localInput, ".m4a"), false, false))
|
||||
}
|
||||
if (preferredExt == ".flac" || preferredExt == ".m4a") {
|
||||
attempts.add(buildOutputPath(localInput, ".mp4") to false)
|
||||
attempts.add(Triple(buildOutputPath(localInput, ".mp4"), false, false))
|
||||
}
|
||||
// MOV muxer fallback for codecs the MP4 muxer rejects (e.g. AC-4):
|
||||
// keeps the .mp4 filename but stores the codec params.
|
||||
attempts.add(Triple(buildOutputPath(localInput, ".mp4"), false, true))
|
||||
|
||||
for ((candidateOutput, mapAudioOnly) in attempts) {
|
||||
for ((candidateOutput, mapAudioOnly, forceMov) in attempts) {
|
||||
try {
|
||||
val audioMap = if (mapAudioOnly) "-map 0:a " else ""
|
||||
// Force the flac muxer when the target extension is
|
||||
@@ -439,7 +442,11 @@ object NativeDownloadFinalizer {
|
||||
// stream layout, producing FLAC-in-MP4 under a .flac
|
||||
// filename which downstream native FLAC tag writers
|
||||
// cannot read.
|
||||
val muxerOverride = if (candidateOutput.lowercase(Locale.ROOT).endsWith(".flac")) "-f flac " else ""
|
||||
val muxerOverride = when {
|
||||
forceMov -> "-f mov "
|
||||
candidateOutput.lowercase(Locale.ROOT).endsWith(".flac") -> "-f flac "
|
||||
else -> ""
|
||||
}
|
||||
val command = "-v error -decryption_key ${q(candidate)} -f $inputFormat -i ${q(localInput)} ${audioMap}-c copy ${muxerOverride}${q(candidateOutput)} -y"
|
||||
val result = runFFmpeg(command, shouldCancel)
|
||||
lastOutput = result.second
|
||||
@@ -1159,18 +1166,28 @@ object NativeDownloadFinalizer {
|
||||
val mp3Flags = if (format == "mp3") "-id3v2_version 3 " else ""
|
||||
var adoptedTemp = false
|
||||
var originalDeleted = false
|
||||
try {
|
||||
val command = if (isM4a && coverFile != null) {
|
||||
|
||||
fun buildEmbedCommand(forceMov: Boolean): String {
|
||||
return if (isM4a && coverFile != null) {
|
||||
"-v error -hide_banner -i ${q(path)} -i ${q(coverFile.absolutePath)} " +
|
||||
"-map 0:a -c:a copy -map_metadata 0 -map 1:v -c:v copy " +
|
||||
"-disposition:v:0 attached_pic " +
|
||||
"-metadata:s:v ${q("title=Album cover")} " +
|
||||
"-metadata:s:v ${q("comment=Cover (front)")} " +
|
||||
"$metadataArgs -f mp4 ${q(temp.absolutePath)} -y"
|
||||
"$metadataArgs -f ${if (forceMov) "mov" else "mp4"} ${q(temp.absolutePath)} -y"
|
||||
} else {
|
||||
"-v error -hide_banner -i ${q(path)} -map 0 -c copy -map_metadata 0 $metadataArgs $mp3Flags${q(temp.absolutePath)} -y"
|
||||
val movFlag = if (forceMov) "-f mov " else ""
|
||||
"-v error -hide_banner -i ${q(path)} -map 0 -c copy -map_metadata 0 $metadataArgs $mp3Flags$movFlag${q(temp.absolutePath)} -y"
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
var result = runFFmpeg(buildEmbedCommand(false))
|
||||
// MOV muxer fallback for codecs the MP4 muxer rejects (e.g. AC-4).
|
||||
if (!result.first && (isM4a || ext.equals(".mp4", ignoreCase = true))) {
|
||||
temp.delete()
|
||||
result = runFFmpeg(buildEmbedCommand(true))
|
||||
}
|
||||
val result = runFFmpeg(command)
|
||||
if (result.first && temp.exists()) {
|
||||
if (inputFile.delete()) {
|
||||
originalDeleted = true
|
||||
|
||||
Reference in New Issue
Block a user