0

この短いスニペットを使用して、ビデオをセットアップしています。何らかの理由で、ビデオが表示されないことがありますが、他のビデオでは完全に機能します。

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

一部のビデオでは、単に表示されません。

助言がありますか?ありがとう!!

4

1 に答える 1

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)
        }

    })
}
于 2016-03-07T05:03:15.847 に答える