【问题标题】:AVMutableVideoComposition sometimes won't play videoAVMutableVideoComposition 有时无法播放视频
【发布时间】:2016-02-29 01:18:40
【问题描述】:

我正在使用这个简短的 sn-p 来设置我的视频。由于某些未知原因 - 有时视频根本不会显示,而对于其他视频,它会完美运行。

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

对于某些视频,它根本不会显示它们。

有什么建议吗?谢谢!!

【问题讨论】:

    标签: objective-c swift avplayer avasset avvideocomposition


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-03
      • 1970-01-01
      • 1970-01-01
      • 2020-06-04
      • 2014-01-19
      • 2014-04-06
      • 1970-01-01
      相关资源
      最近更新 更多