视频以180度旋转,但视频未使用swift完全显示在帧中

时间:2016-02-09 08:35:06

标签: iphone xcode swift video

视频正在以180度旋转,但视频没有完全显示在框架中使用swift,videoPathURL是图像选取器拾取的视频的网址,视频是一些如何裁剪而不是完美适合中间的框架。 我想要的是将任何给定的视频旋转180度。

func rotateVideo()-> NSURL{

    let asset : AVAsset = AVURLAsset(URL: videoPathURL)
    let clipVideoTrack : AVAssetTrack = asset.tracksWithMediaType(AVMediaTypeVideo).first!

    let videoComposition : AVMutableVideoComposition = AVMutableVideoComposition()
    videoComposition.frameDuration = CMTimeMake(1, 30)


    let videoSizeMain : CGSize = CGSizeMake(clipVideoTrack.naturalSize.width, clipVideoTrack.naturalSize.height)

    let cropSquare : CGRect = CGRectMake(0, 0, videoSizeMain.width  , videoSizeMain.height)

    videoComposition.renderSize = CGSizeMake(cropSquare.size.height , cropSquare.size.width)

    let instruction : AVMutableVideoCompositionInstruction  = AVMutableVideoCompositionInstruction()
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration)

    let layerInstruction : AVMutableVideoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)

    var t1 : CGAffineTransform = CGAffineTransformIdentity
    var t2 : CGAffineTransform = CGAffineTransformIdentity

    t1 = CGAffineTransformMakeTranslation(clipVideoTrack.naturalSize.height*2, clipVideoTrack.naturalSize.height*1.5)
    t2 = CGAffineTransformRotate(t1, CGFloat(M_PI))

    let finalTranform : CGAffineTransform = t2
    layerInstruction.setTransform(finalTranform, atTime: kCMTimeZero)
    instruction.layerInstructions = NSArray(object: layerInstruction) as! [AVVideoCompositionLayerInstruction]
    videoComposition.instructions = NSArray(object: instruction) as! [AVVideoCompositionInstructionProtocol]

    **CODE FOR SAVING THE VIDEO**

    let components = self.calendar.components([ .Hour, .Minute, .Second], fromDate: self.date)
    let hour       = components.hour
    let minutes    = components.minute
    let seconds    = components.second

    let paths : NSArray                  = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let documentsDirectory : NSString    = paths.objectAtIndex(0) as! NSString
    let filePath                         = documentsDirectory.stringByAppendingPathComponent("\(hour)\(minutes)\(seconds)rotated.mov")
    self.latestFilePath                  = filePath
    finalFileURL2 = NSURL.fileURLWithPath(latestFilePath as String)

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

    exporter.exportAsynchronouslyWithCompletionHandler { () -> Void in
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.exportDidFinish(exporter)
        } )
    }
    return finalFileURL2
}

The video is not completely in the frame

1 个答案:

答案 0 :(得分:1)

问题在于设置videoComposition.renderSize = CGSizeMake(cropSquare.size.height , cropSquare.size.width)这使得视频在错误的帧中呈现。只需设置正确的框架videoComposition.renderSize = CGSizeMake(cropSquare.size.width , cropSquare.size.height),就可以了。 :)