此代码在所有IOS 13之前的设备上都有效(并且仍然有效)。目前,在exportAsynchronously调用运行后出现此错误:
Error Domain = AVFoundationErrorDomain代码= -11800“该操作可能未完成” UserInfo = {NSLocalizedFailureReason =未知错误发生(-12735),NSLocalizedDescription =无法执行该操作已完成,NSUnderlyingError = 0x282e194a0 {ErrorDomain = NSOSStatusErrorDomain代码= -12735“(null)”}}]
不确定IOS 13是否在AVAssetExportSession对象的基本设置中添加/更改了一些要求,或者是什么?可能是IOS错误吗?
这里是代码:
func compileAudioAndVideoToMovie(audioInputURL:URL, videoInputURL:URL) {
let docPath:String = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0];
let videoOutputURL:URL = URL(fileURLWithPath: docPath).appendingPathComponent("video.mp4");
do
{
try FileManager.default.removeItem(at: videoOutputURL);
}
catch {}
let mixComposition = AVMutableComposition();
let videoTrack = mixComposition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid);
let videoInputAsset = AVURLAsset(url: videoInputURL);
let audioTrack = mixComposition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid);
let audioInputAsset = AVURLAsset(url: audioInputURL);
do
{
try videoTrack?.insertTimeRange(CMTimeRangeMake(start: CMTimeMake(value: 0, timescale: 1000), duration: CMTimeMake(value: 3000, timescale: 1000)), of: videoInputAsset.tracks(withMediaType: AVMediaType.video)[0], at: CMTimeMake(value: 0, timescale: 1000));// Insert an 3-second video clip into the video track
try audioTrack?.insertTimeRange(CMTimeRangeMake(start: CMTimeMake(value: 0, timescale: 1000), duration: CMTimeMake(value: 3000, timescale: 1000)), of: audioInputAsset.tracks(withMediaType: AVMediaType.audio)[0], at: CMTimeMake(value: 0, timescale: 1000));// Insert an 3-second audio clip into the audio track
let assetExporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetPassthrough);
assetExporter?.outputFileType = AVFileType.mp4;
assetExporter?.outputURL = videoOutputURL;
assetExporter?.shouldOptimizeForNetworkUse = false;
assetExporter?.exportAsynchronously {
switch (assetExporter?.status)
{
case .cancelled:
print("Exporting cancelled");
case .completed:
print("Exporting completed");
case .exporting:
print("Exporting ...");
case .failed:
print("Exporting failed");
default:
print("Exporting with other result");
}
if let error = assetExporter?.error
{
print("Error:\n\(error)");
}
}
}
catch
{
print("Exception when compiling movie");
}
}
问题似乎与AVAssetExportPresetPassthrough有关(可能与处理AAC的组合有关]
更改为AVAssetExportPresetLowQuality或AVAssetExportPresetHighestQuality,然后将视频/音频正确混合为一个。同样,这只是IOS 13问题,并且可能是错误。