我在将AVAsset导出到本地目录时遇到问题。我收到以下错误消息:
failed Error Domain=AVFoundationErrorDomain Code=-11838 "Operation Stopped" UserInfo={NSLocalizedFailureReason=The operation is not supported for this media., NSLocalizedDescription=Operation Stopped, NSUnderlyingError=0x2806d1740 {Error Domain=NSOSStatusErrorDomain Code=-12109 "(null)"}}
这是我上传文件的代码:这是调试器的图像,您可以看到
file
变量引用了实际的AVAsset
这是失败的AVAssetExportSession的描述
Optional<AVAssetExportSession>
- some : <AVAssetExportSession: 0x280791270, asset = <AVURLAsset: 0x2805ef600, URL = https://firebasestorage.googleapis.com/v0/b/camouflage-43fe0.appspot.com/o/eCg9SNmLVlRUacfUBO1CTWNQizO2%2F31983848-C493-469B-A7CD-F7B215C37526%2Fproject_audio%2F1308EB6E-0FB3-4538-AEEC-DF59945C4CF9%2F3826F259-1B18-4381-B893-E69ACC8D6DEE?alt=media&token=8a0725e0-f34c-4b90-b564-3a3ca52e58cd>, presetName = AVAssetExportPresetAppleM4A, outputFileType = com.apple.m4a-audio
在此先感谢您的帮助。编辑:我尝试将AVURLAsset包装在AVMutableComposition内,但是现在我收到此错误,而不是
failed Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-17508), NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x283ae5c50 {Error Domain=NSOSStatusErrorDomain Code=-17508 "(null)"}}
最佳答案
我认为您遇到了错误,因为文件扩展名与'presetName','outputFileType','fileURL'不匹配。
我尝试使用AVMutableComposition解决并以m4a格式导出音频。音频在远程服务器上。
let url = URL(string: "https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3")
let asset = AVAsset(url: url!)
let playerItem = AVPlayerItem(asset: asset)
let remoteFilePath = url
func downloadLocalCopyOfTrack(_ playerItem: AVPlayerItem, finished:@escaping (URL?) -> ()) {
guard playerItem.asset.isExportable else {
finished(nil)
return
}
let composition = AVMutableComposition()
let compositionAudioTrack = composition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: CMPersistentTrackID(kCMPersistentTrackID_Invalid))
let sourceAudioTrack = playerItem.asset.tracks(withMediaType: AVMediaType.audio).first!
do {
let durationInSec = playerItem.asset.duration.seconds
let duration = CMTime(seconds: durationInSec, preferredTimescale: 1)
try compositionAudioTrack?.insertTimeRange(CMTimeRange(start: CMTime.zero, duration: duration), of: sourceAudioTrack, at: CMTime.zero)
} catch {
print(error.localizedDescription)
finished(nil)
return
}
remoteFilePath.deletePathExtension()
let fileName = remoteFilePath.lastPathComponent
let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let outputFilePath = documentDirectory.appendingPathComponent("\(fileName).m4a")
try? FileManager.default.removeItem(at: outputFilePath)
let exportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
exportSession!.outputURL = outputFilePath
exportSession!.outputFileType = AVFileType.m4a
exportSession!.timeRange = CMTimeRange(start: CMTime.zero, duration: playerItem.duration)
//Timer for Progress of Export Session
var exportProgressBarTimer = Timer() // initialize timer
if #available(iOS 10.0, *) {
exportProgressBarTimer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { timer in
// Get Progress
let progress = Float((exportSession!.progress));
if (progress < 0.99) {
print("progress", progress * 100)
}
}
}
exportSession!.exportAsynchronously(completionHandler: {
switch exportSession!.status {
case .failed:
print("Export failed: \(exportSession!.error!)")
exportProgressBarTimer.invalidate()
case .cancelled:
print("Export canceled")
exportProgressBarTimer.invalidate()
default:
print("Successfully trimmed audio")
DispatchQueue.main.async(execute: {
finished(outputFilePath)
exportProgressBarTimer.invalidate()
})
}
})
}
用法:downloadLocalCopyOfTrack(playerItem, finished: { (url) in
if url != nil {
//use url or play here
}else {
print("URL is nil")
}
})