gpt4 book ai didi

swift - 没有音频的视频会使 AVMutableComposition() 上的应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 10:41:21 26 4
gpt4 key购买 nike

我的应用程序从 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

最佳答案

我想出了当原始视频没有音频时如何导出视频。如果其他人遇到同样的问题。

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")
}

关于swift - 没有音频的视频会使 AVMutableComposition() 上的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56729461/

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