gpt4 book ai didi

swift - 需要帮助使用 AVAssetExportSession 导出音频

转载 作者:行者123 更新时间:2023-12-03 01:13:56 29 4
gpt4 key购买 nike

我在将 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)"}}

这是我上传文件的代码:

enter image description here

这是我的调试器的图像,您可以在其中看到 file 变量引用了一个实际的 AVAsset

enter image description here

这是对失败的 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")
}
})

关于swift - 需要帮助使用 AVAssetExportSession 导出音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63066799/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com