Audio Swift如何将视频和音频合并为一个mp4文件
我想将AVCaptureSession()捕获的视频和AVAudioRecorder()录制的音频合并到一个mp4文件中 我试过这个办法 还有这个 但他们两个都不起作用。他们都给了我同样的错误:线程1:信号SIGABRT 那么,合并音频和视频的正确方法是什么 更新:- 我找到了每次应用程序崩溃的地方。它在试图从AVAsset获取AVAssetTrack的代码行上崩溃Audio Swift如何将视频和音频合并为一个mp4文件,audio,video,swift3,avfoundation,avasset,Audio,Video,Swift3,Avfoundation,Avasset,我想将AVCaptureSession()捕获的视频和AVAudioRecorder()录制的音频合并到一个mp4文件中 我试过这个办法 还有这个 但他们两个都不起作用。他们都给了我同样的错误:线程1:信号SIGABRT 那么,合并音频和视频的正确方法是什么 更新:- 我找到了每次应用程序崩溃的地方。它在试图从AVAsset获取AVAssetTrack的代码行上崩溃 func merge(audio:URL, withVideo video : URL){ // create com
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 ** //
....
}
在为项下标之前,始终检查数组中的元素计数。例如:如果videoAsset.tracks(withMediaType:AVMediaTypeVideo).count>0{让videoAssetTrack=videoAsset.tracks(withMediaType:AVMediaTypeVideo)[0]}您还可以实现一个安全下标,它返回一个可选()