AVMutableVideoComposition有时不会播放视频

时间:2016-02-29 01:18:41

标签: objective-c swift avplayer avasset avvideocomposition

我正在使用这个简短的片段来设置我的视频。出于某种未知原因 - 有时视频根本不会显示,而对于其他视频,它将完美无缺。

let videoTrack: AVAssetTrack = asset.tracksWithMediaType(AVMediaTypeVideo)[0]
        let composition: AVMutableComposition = AVMutableComposition()

        let videoComposition: AVMutableVideoComposition = AVMutableVideoComposition()
        var videoSize: CGSize = videoTrack.naturalSize
        let isPortrait_: Bool = self.isVideoPortrait(asset)
        if isPortrait_ {
            NSLog("video is portrait ")
            videoSize = CGSizeMake(videoSize.height, videoSize.width)
        }
        composition.naturalSize = videoSize
        videoComposition.renderSize = videoSize
        // videoComposition.renderSize = videoTrack.naturalSize; //
        videoComposition.frameDuration = CMTimeMake(1, 30)

        let compositionVideoTrack = composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: 1)
        let timeRange = videoTrack.timeRange
        do {
            try compositionVideoTrack.insertTimeRange(timeRange, ofTrack: videoTrack, atTime: kCMTimeZero)
        } catch {
            print("error")

        }
        let layerInst = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack)

        layerInst.setTransform(videoTrack.preferredTransform, atTime: kCMTimeZero)
        let inst: AVMutableVideoCompositionInstruction = AVMutableVideoCompositionInstruction()
        inst.timeRange = timeRange

        inst.layerInstructions = [layerInst]
        videoComposition.instructions = [inst]

        let playerItem = AVPlayerItem(asset: composition)
        playerItem.videoComposition = videoComposition

对某些视频而言,它根本就不会显示出来。

有什么建议吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您好我有一个相对类似的代码希望这可以帮助您找出问题

class func MergeVideosSequentially(URLS : [NSURL], callback : (error : ErrorType? , outURL : NSURL?) -> Void){
    let composition = AVMutableComposition()

    //videoTrack
    let videoTrack = composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
    let audioTrack = composition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid)

    var cursorTime = kCMTimeZero
    for URL in URLS {           
        let asset = AVAsset(URL: URL)
        let assetVideoTrack = asset.tracksWithMediaType(AVMediaTypeVideo).first! as AVAssetTrack
        let assetAudioTrack = asset.tracksWithMediaType(AVMediaTypeAudio).first! as AVAssetTrack

        var duration : CMTimeRange? = nil
        duration = CMTimeRangeMake(kCMTimeZero, asset.duration)

        do {
            try videoTrack.insertTimeRange(duration!, ofTrack: assetVideoTrack, atTime: cursorTime)
            try audioTrack.insertTimeRange(duration!, ofTrack: assetAudioTrack, atTime: cursorTime)
        }catch {
            print(error)
        }
        cursorTime = CMTimeAdd(cursorTime, asset.duration)
    }

    let directory = NSTemporaryDirectory()
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateStyle = .LongStyle
    dateFormatter.timeStyle = .ShortStyle
    let date = dateFormatter.stringFromDate(NSDate())
    let savePath = "\(directory)/mergedVideo-\(date).mp4"
    let url = NSURL(fileURLWithPath: savePath)

    let exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)
    exporter!.outputURL = url
    exporter!.shouldOptimizeForNetworkUse = true
    exporter!.outputFileType = AVFileTypeMPEG4

    exporter!.exportAsynchronouslyWithCompletionHandler({ () -> Void in
        let outputURL = exporter!.outputURL

        switch exporter!.status {
        case .Completed :
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                callback(error: nil, outURL: outputURL)
            })
        default:
            callback(error: CCMovieWriterError.CannotMergeClips, outURL: nil)
        }

    })
}