我想将AVCaptureSession()捕获的视频和AVAudioRecorder()录制的音频合并为一个mp4文件。
我试过这个解决方案 Swift Merge audio and video files into one video
和Merging audio and video Swift
但他们两个都没有用。他们都给了我同样的错误:Thread1:Signal SIGABRT
那么合并音频和视频的正确方法是什么?
更新: -
我发现每次应用崩溃的地方。它在试图从 AVAsset
获取 AVAssetTrack 的代码行崩溃func merge(audio:URL, withVideo video : URL){
// create composition
let mutableComposition = AVMutableComposition()
// Create the video composition track.
let mutableCompositionVideoTrack : AVMutableCompositionTrack = mutableComposition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
// Create the audio composition track.
let mutableCompositionAudioTrack : AVMutableCompositionTrack = mutableComposition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid)
// create media assets and tracks
let videoAsset = AVAsset(url: video)
let videoAssetTrack = videoAsset.tracks(withMediaType: AVMediaTypeVideo)[0] // ** always crashes here ** //
let audioAsset = AVAsset(url: audio)
let audioAssetTrack = audioAsset.tracks(withMediaType: AVMediaTypeAudio)[0] // ** always crashes here ** //
....
}