Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

ios - Swift: Crop and Export Video

I want to crop a video to square, but i couldn't do that.

I converted this code to swift but i only get black screen after exporting the video

        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

What am i missing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're missing the exporter block at the very end to perform the export operation with the AVAssetExportSession you've created:

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

})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...