gpt4 book ai didi

ios - AVAssetExportSession 未开始导出/等待

转载 作者:搜寻专家 更新时间:2023-10-30 22:17:11 25 4
gpt4 key购买 nike

我正在尝试使用 AVFoundation 加入 2 个视频剪辑。出于测试目的,我在这里尝试加载单个视频剪辑,将其视频和音频轨道添加到合成中,然后使用 AVAssetExportSession 将其导出。

当我运行下面的代码时,输​​出“Exporting”,但从未执行导出回调。此外,如果我定期检查导出进度 (print(exporter.progress)),我发现进度始终为 0.0,即使在几分钟后也是如此。如果我打印状态,我发现它正在“等待”某事。

// URL to video file
let fileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("video.mov")

// Create composition and tracks
let comp = AVMutableComposition()
let videoTrack = comp.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)
let audioTrack = comp.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid)

// Create asset from file
let asset = AVAsset(url: fileURL)

// Insert video
try! videoTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, asset.duration), of: asset.tracks(withMediaType: AVMediaType.video)[0] as AVAssetTrack, at: kCMTimeZero)

// Insert audio
try! audioTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, asset.duration), of: asset.tracks(withMediaType: AVMediaType.audio)[0] as AVAssetTrack, at: kCMTimeZero)

// Delete existing movie file if exists
let finalURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("FINAL.mov")
try? FileManager.default.removeItem(at: finalURL)

// Create the exporter
exporter = AVAssetExportSession(asset: comp, presetName: AVAssetExportPresetLowQuality)
exporter.outputURL = finalURL
exporter.outputFileType = AVFileType.mov

print("Exporting")
exporter.exportAsynchronously(completionHandler: {
// This statement is never reached
print(self.exporter.error)
})

不会抛出任何错误。似乎导出永远不会开始。我不确定它在等什么。

编辑: 发生了一些令人毛骨悚然的事情。如果我重新启动手机然后运行代码,它就会工作。但它只工作 1 次。如果我第二次运行它,则不会像往常一样发生任何事情。但是,当我重新启动手机并再次运行时,它又能正常工作了。

编辑 2:我在别人的手机上试过了,每次都运行可靠。我的手机到底出了什么问题?

最佳答案

这段代码基本上做同样的事情,向视频添加音轨。它看起来真的是你的,它目前正在 appstore 上开发一个应用程序。

func merge() -> AVComposition? {
guard let videoAsset = videoAsset, let audioAsset = audioAsset else {
return nil
}
let avMutableComposition = AVMutableComposition()
let audiotimeRange = CMTimeRange(start: kCMTimeZero, duration: audioAsset.duration)
let compositionVideoTrack = avMutableComposition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)
do {
try add(asset: videoAsset, withRemaining: audiotimeRange.duration, forComposition: compositionVideoTrack!, cursor: kCMTimeZero)
} catch let error {
print(error)
return nil
}

let compositionAudioTrack = avMutableComposition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid)
do {
try compositionAudioTrack?.insertTimeRange(audiotimeRange, of: audioAsset.tracks(withMediaType: AVMediaType.audio)[0], at: kCMTimeZero)
} catch let error {
print(error)
return nil
}


composition = (avMutableComposition.copy() as! AVComposition)
return composition
}

我看不出我们的代码有什么问题,除了我不确定 Assets 是否具有正确的持续时间这一事实,因为获取 Assets 是一项异步任务,除非您明确询问。
因此,当您创建 AVAsset 时,将 AVURLAssetPreferPreciseDurationAndTimingKey 作为选项传递给 true

AVURLAsset(url: <#T##URL#>, options: ["AVURLAssetPreferPreciseDurationAndTimingKey" : true])

关于ios - AVAssetExportSession 未开始导出/等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48200129/

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