我的应用程序从URL拍摄视频,并允许您向其中添加文本等。 当视频没有任何音频开始时似乎崩溃了,似乎无法弄清楚。
这是我在制作视频时所拥有的:
let asset = AVAsset(url: URL(string: self.videoURL)!)
let mixComposition = AVMutableComposition()
let videoTrack = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)
try! videoTrack?.insertTimeRange(CMTimeRangeMake(start: .zero, duration: asset.duration), of: asset.tracks(withMediaType: .video)[0], at: CMTime.zero)
let audioTrack = mixComposition.addMutableTrack(withMediaType: .audio, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
do {
try audioTrack!.insertTimeRange(CMTimeRangeMake(start: .zero, duration: asset.duration), of: asset.tracks(withMediaType: .audio)[0], at: CMTime.zero)
} catch {
print("error")
}
它抛出insertTimeRange
,表明indexPath
超出范围。
[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray
答案 0 :(得分:0)
我想出了当原始视频没有音频时如何导出视频。万一其他人遇到同样的问题。
let asset = AVAsset(url: URL(string: self.videoURL)!)
let mixComposition = AVMutableComposition()
let videoTrack = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)
try! videoTrack?.insertTimeRange(CMTimeRangeMake(start: .zero, duration: asset.duration), of: asset.tracks(withMediaType: .video)[0], at: CMTime.zero)
let audioTrack = mixComposition.addMutableTrack(withMediaType: .audio, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
if let track = asset.tracks(withMediaType: .audio).first {
do {
try audioTrack?.insertTimeRange(CMTimeRangeMake(start: .zero, duration: asset.duration), of: track, at: .zero)
} catch {
print("error")
}
} else {
mixComposition.removeTrack(audioTrack!)
print("no audio detected, removed the track")
}