我正在尝试将一些视频合并为唯一的output.mov文件。导出的文件无法播放,我不知道原因。是否有人能帮助我?
func exportVideo2(path:String, outputPath:String, nMovie:Int) -> Bool{
var composition = AVMutableComposition()
let track:AVMutableCompositionTrack = composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())
var insertTime = kCMTimeZero
var movie = movieOfProject(path)
if movie.count == nMovie{
for (index,mov) in enumerate(movie){
let moviePath = path.stringByAppendingPathComponent(mov)
// moviePath = path to the .mov file
println(moviePath)
let moviePathUrl = NSURL(fileURLWithPath: moviePath)
let sourceAsset = AVURLAsset(URL: moviePathUrl, options: nil)
println(sourceAsset)
let tracks = sourceAsset.tracksWithMediaType(AVMediaTypeVideo)
println(sourceAsset.playable) // print true
println(sourceAsset.exportable) // print true
println(sourceAsset.readable) // print true
if tracks.count > 0{
let assetTrack:AVAssetTrack = tracks[0] as AVAssetTrack
track.insertTimeRange(CMTimeRangeMake(kCMTimeZero,sourceAsset.duration), ofTrack: assetTrack, atTime: insertTime, error: nil)
insertTime = CMTimeAdd(insertTime, sourceAsset.duration)
}
}
let completeMovie = outputPath.stringByAppendingPathComponent("movie.mov")
let completeMovieUrl = NSURL(fileURLWithPath: completeMovie)
var exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)
exporter.outputURL = completeMovieUrl
exporter.outputFileType = AVFileTypeMPEG4
exporter.exportAsynchronouslyWithCompletionHandler(nil)
let ass = AVURLAsset(URL: completeMovieUrl, options: nil)
println(ass.readable) // print false
println(ass.exportable) // print false
println(ass.playable) // print false
return true
}else{
return false
}
}
我需要合并的.mov文件都是可读的,所以我认为问题出在最后一部分,即导出输出视频的地方。