mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-20 00:24:43 +02:00
Revert "Replace ffmpeg with switf-ogg"
This commit is contained in:
committed by
aringenbach
parent
f5c562b5a5
commit
93a9ec223a
@@ -15,68 +15,85 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftOGG
|
||||
import ffmpegkit
|
||||
|
||||
enum VoiceMessageAudioConverterError: Error {
|
||||
case conversionFailed(Error?)
|
||||
case getDurationFailed(Error?)
|
||||
case generic(String)
|
||||
case cancelled
|
||||
}
|
||||
|
||||
struct VoiceMessageAudioConverter {
|
||||
static func convertToOpusOgg(sourceURL: URL, destinationURL: URL, completion: @escaping (Result<Void, VoiceMessageAudioConverterError>) -> Void) {
|
||||
DispatchQueue.global(qos: .userInitiated).async {
|
||||
do {
|
||||
try OGGConverter.convertM4aFileToOpusOGG(src: sourceURL, dest: destinationURL)
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(()))
|
||||
}
|
||||
} catch {
|
||||
DispatchQueue.main.async {
|
||||
completion(.failure(.conversionFailed(error)))
|
||||
}
|
||||
}
|
||||
}
|
||||
let command = "-hide_banner -y -i \"\(sourceURL.path)\" -c:a libopus -b:a 24k \"\(destinationURL.path)\""
|
||||
executeCommand(command, completion: completion)
|
||||
}
|
||||
|
||||
static func convertToMPEG4AAC(sourceURL: URL, destinationURL: URL, completion: @escaping (Result<Void, VoiceMessageAudioConverterError>) -> Void) {
|
||||
DispatchQueue.global(qos: .userInitiated).async {
|
||||
do {
|
||||
try OGGConverter.convertOpusOGGToM4aFile(src: sourceURL, dest: destinationURL)
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(()))
|
||||
}
|
||||
} catch {
|
||||
DispatchQueue.main.async {
|
||||
completion(.failure(.conversionFailed(error)))
|
||||
let command = "-hide_banner -y -i \"\(sourceURL.path)\" -c:a aac_at \"\(destinationURL.path)\""
|
||||
executeCommand(command, completion: completion)
|
||||
}
|
||||
|
||||
static func mediaDurationAt(_ sourceURL: URL, completion: @escaping (Result<TimeInterval, VoiceMessageAudioConverterError>) -> Void) {
|
||||
FFprobeKit.getMediaInformationAsync(sourceURL.path) { session in
|
||||
guard let session = session else {
|
||||
completion(.failure(.generic("Invalid session")))
|
||||
return
|
||||
}
|
||||
|
||||
guard let returnCode = session.getReturnCode() else {
|
||||
completion(.failure(.generic("Invalid return code")))
|
||||
return
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
if returnCode.isValueSuccess() {
|
||||
let mediaInfo = session.getMediaInformation()
|
||||
if let duration = try? TimeInterval(value: mediaInfo?.getDuration() ?? "0") {
|
||||
completion(.success(duration))
|
||||
} else {
|
||||
completion(.failure(.generic("Failed to get media duration")))
|
||||
}
|
||||
} else if returnCode.isValueCancel() {
|
||||
completion(.failure(.cancelled))
|
||||
} else {
|
||||
completion(.failure(.generic(String(returnCode.getValue()))))
|
||||
MXLog.error("""
|
||||
getMediaInformationAsync failed with state: \(String(describing: FFmpegKitConfig.sessionState(toString: session.getState()))), \
|
||||
returnCode: \(String(describing: returnCode)), \
|
||||
stackTrace: \(String(describing: session.getFailStackTrace()))
|
||||
""")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static func mediaDurationAt(_ sourceURL: URL, completion: @escaping (Result<TimeInterval, VoiceMessageAudioConverterError>) -> Void) {
|
||||
let audioAsset = AVURLAsset(url: sourceURL, options: nil)
|
||||
|
||||
audioAsset.loadValuesAsynchronously(forKeys: ["duration"]) {
|
||||
var error: NSError?
|
||||
let status = audioAsset.statusOfValue(forKey: "duration", error: &error)
|
||||
static private func executeCommand(_ command: String, completion: @escaping (Result<Void, VoiceMessageAudioConverterError>) -> Void) {
|
||||
FFmpegKitConfig.setLogLevel(0)
|
||||
|
||||
FFmpegKit.executeAsync(command) { session in
|
||||
guard let session = session else {
|
||||
completion(.failure(.generic("Invalid session")))
|
||||
return
|
||||
}
|
||||
|
||||
switch status {
|
||||
case .loaded:
|
||||
let duration = audioAsset.duration
|
||||
let durationInSeconds = CMTimeGetSeconds(duration)
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(durationInSeconds))
|
||||
}
|
||||
case .failed:
|
||||
DispatchQueue.main.async {
|
||||
completion(.failure(.getDurationFailed(error)))
|
||||
}
|
||||
case .cancelled:
|
||||
DispatchQueue.main.async {
|
||||
guard let returnCode = session.getReturnCode() else {
|
||||
completion(.failure(.generic("Invalid return code")))
|
||||
return
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
if returnCode.isValueSuccess() {
|
||||
completion(.success(()))
|
||||
} else if returnCode.isValueCancel() {
|
||||
completion(.failure(.cancelled))
|
||||
} else {
|
||||
completion(.failure(.generic(String(returnCode.getValue()))))
|
||||
MXLog.error("""
|
||||
Failed converting voice message with state: \(String(describing: FFmpegKitConfig.sessionState(toString: session.getState()))), \
|
||||
returnCode: \(String(describing: returnCode)), \
|
||||
stackTrace: \(String(describing: session.getFailStackTrace()))
|
||||
""")
|
||||
}
|
||||
default: break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user