一尘不染

Swift:裁剪和导出视频

swift

我想将视频裁剪为正方形,但是我做不到。

将此代码转换为swift,但在导出视频后仅出现黑屏

        var videoComposition: AVMutableVideoComposition = AVMutableVideoComposition()
        videoComposition.frameDuration = CMTimeMake(1, 60)
        videoComposition.renderSize = CGSizeMake(clipVideoTrack.naturalSize.height, clipVideoTrack.naturalSize.height)

        var instruction: AVMutableVideoCompositionInstruction = AVMutableVideoCompositionInstruction()
        instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30))

        var transformer: AVMutableVideoCompositionLayerInstruction =
        AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)

        var t1: CGAffineTransform = CGAffineTransformMakeTranslation(clipVideoTrack.naturalSize.height, 0)
        var t2: CGAffineTransform = CGAffineTransformRotate(t1, CGFloat(M_PI_2))

        var finalTransform: CGAffineTransform = t2

        transformer.setTransform(finalTransform, atTime: kCMTimeZero)

        instruction.layerInstructions = NSArray(object: transformer)
        videoComposition.instructions = NSArray(object: instruction)

        var documentsPath: NSString = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0] as NSString

        var exportPath: NSString = documentsPath.stringByAppendingFormat("/xvideo.mov")
        var exportUrl: NSURL = NSURL.fileURLWithPath(exportPath)!


        var exporter = AVAssetExportSession(asset: asset!, presetName: AVAssetExportPresetHighestQuality)
        exporter.videoComposition = videoComposition
        exporter.outputFileType = AVFileTypeQuickTimeMovie
        exporter.outputURL = exportUrl

我想念什么?


阅读 355

收藏
2020-07-07

共1个答案

一尘不染

您最终将缺少exporter块,无法AVAssetExportSession对创建的对象执行导出操作:

exporter.exportAsynchronouslyWithCompletionHandler({

    //display video after export is complete, for example...
    let outputURL:NSURL = exporter.outputURL;
    let asset:AVURLAsset = AVURLAsset(URL: outputURL, options: nil)
    let newPlayerItem:AVPlayerItem = AVPlayerItem(asset: asset)

    self.mPlayer = AVPlayer.playerWithPlayerItem(newPlayerItem) as AVPlayer

})
2020-07-07